Product of Array Except Self

Array
Medium

Approach

Diagrams

Analysis

Time

O(n)

Space

O(1)
class Solution:
    def productExceptSelf(self, nums):
        productsExceptSelf = [1 for num in range(len(nums))]

        productOfNumsToLeft = 1
        productOfNumsToRight = 1

        for idx in range(len(nums)):
            productsExceptSelf[idx] = productOfNumsToLeft
            productOfNumsToLeft *= nums[idx]

        for idx in reversed(range(len(nums))):
            productsExceptSelf[idx] *= productOfNumsToRight
            productOfNumsToRight *= nums[idx]

        return productsExceptSelf

Learn

#Array

Videos