Given an integer array nums, return an array answer where answer[i] is the product of every element except nums[i].
Do it in O(n) time and without division. Every answer is guaranteed to fit in a 32-bit integer.
For example, answer[0] = 3 × 5 × 4 = 60.
Only the slot of the 0 escapes it: answer[1] = 3 × (-2) = -6.
Constraints
2 <= nums.length <= 10⁵-30 <= nums[i] <= 30answer[i] fits in a 32-bit integer.Going further: can you use only O(1) extra space? The output array does not count.
answer[i] is the product of everything before i times the product of everything after i.
Stand at position i and look at what answer[i] needs: every number except nums[i]. Those numbers fall into two groups, the ones to the left of i and the ones to the right. So
Time O(n)Space O(1)
The full walkthrough and code are part of Premium