Problem 217979 · medium · Phase 02 Linear Data Structures

Product of the Others

arrays · prefix-products · two-pass

Given a list of integers nums, return a list out where out[i] is the product of all elements except nums[i]. Do it without division (zeros may be present).

Examples

Input:  nums = [1, 2, 3, 4]
Output: [24, 12, 8, 6]

Input:  nums = [2, 0, 3]
Output: [0, 6, 0]

Constraints

  • 2 <= len(nums) <= 2 * 10**4
  • -10 <= nums[i] <= 10
  • Target: O(n) time without division.

Goals

  • Combine a left-to-right and a right-to-left accumulation
  • Handle zeros correctly without using division
Starting Python…