Problem 217979 · medium · Level 02 Linear Data Structures

Bypass One Stage

arrays · prefix-products · two-pass

A signal runs through a chain of amplifier stages, and stage i multiplies it by the whole number nums[i] (a stage with factor 0 blocks it entirely). For every stage, the engineer wants the overall factor of the chain if only that stage were bypassed. Return a list out where out[i] is the product of every factor except nums[i]. Work it out without dividing; zeros may appear.

Examples

Input:  nums = [2, 3, 1, 5]
Output: [15, 10, 30, 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…