Problem statement

https://leetcode.com/problems/product-of-array-except-self/

Solution

We can use cumulative products for boths sides and then multipy them elementwise.

Complexity

It is O(n) both for time and space.

Code

class Solution:
    def productExceptSelf(self, nums):
        t1 = [1] + list(accumulate(nums, mul))[:-1]
        t2 = list(accumulate(nums[::-1], mul))[::-1][1:] + [1]
        return [x*y for x, y in zip(t1, t2)]

Remark

For O(1) space complexity we can first create empty output array and then traverse it twice from different ends.