educative.io

Thoughts about 3rd solution?


#1

Hi,

I came up with the following solution (O(n) as well):

def findProduct(arr):
  
  product = 1
  for x in arr:
    product *= x

  for i in range(len(arr)):
    arr[i] = product / arr[i]
  
  return arr

Sure, there’s an extra multiplication/division here, but do you think it is appropriate? If not, why?


#2

Hey Dennis,

Thanks for reaching out! This is actually a good solution for Python in my opinion. It takes O(1) space and O(n) time.

If you have any other questions/comments/suggestions, let us know!


#3

This fails on any input with 0.


#4

Thank you. Need to double-check everything…