Skip to main content

Product of Array Except Self

Question

None

Example 1
Input: [1,2,3,4]

Output: [24,12,8,6]

Solution

all//Product of Array Except Self.py


def productExceptSelf(nums):
# The length of the input array
length = len(nums)

# The answer array to be returned
answer = [0]*length

# answer[i] contains the product of all the elements to the left
# Note: for the element at index '0', there are no elements to the left,
# so the answer[0] would be 1
answer[0] = 1
for i in range(1, length):

# answer[i - 1] already contains the product of elements to the left of 'i - 1'
# Simply multiplying it with nums[i - 1] would give the product of all
# elements to the left of index 'i'
answer[i] = nums[i - 1] * answer[i - 1]

# R contains the product of all the elements to the right
# Note: for the element at index 'length - 1', there are no elements to the right,
# so the R would be 1
R = 1;
for i in reversed(range(length)):

# For the index 'i', R would contain the
# product of all elements to the right. We update R accordingly
answer[i] = answer[i] * R
R *= nums[i]

return answer