Quant Memo
Coding/●●●●●

Product of array except self

Asked at Two Sigma

Given an integer array nums, return an array out where out[i] is the product of all elements except nums[i].

nums = [1, 2, 3, 4]  ->  [24, 12, 8, 6]
nums = [2, 0, 3, 0]  ->  [0, 0, 0, 0]

Solve it in O(n)O(n) time without using the division operator. (Division would break on zeros anyway.)

Show a hint

The product of everything except i is (product of everything to its left) times (product of everything to its right). Can you compute both directions in two sweeps?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions