7. Product of Array Except Self

Return an array answer where answer[i] is the product of every element of nums except nums[i]. Solve it in O(n) time without using division.

Example 1

Input: nums = [2,3,4,5]
Output: [60,40,30,24]

Example 2

Input: nums = [1,0,3]
Output: [0,3,0]

Constraints

  • 2 ≤ nums.length ≤ 10⁵
  • Products fit in a 32-bit integer
Hint 1

answer[i] = (product of everything to the left) × (product of everything to the right).

Hint 2

Build the left products in one pass and multiply in the right products in a second pass.

ArrayPrefix Sum