intermediate7 min read·Updated September 17, 2026

Product of Array Except Self Explained: Tracing [1, 2, 0, 4]

Master the product of array except self pattern without division. Walk through prefix and suffix passes on [1, 2, 0, 4], handle zeros, and build intuition.

By Learnisim AI·Published September 17, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
  • Basic array iteration
  • Understanding of prefix products
Product of Array Except Self — O(n) Time without Division Locked Worked Example: nums = [1, 2, 0, 4] → Expected Answer = [0, 0, 8, 0] Why Division Fails (The 0 Trap) Naive formula: total / nums[i] For [1, 2, 0, 4]: total = 0 Evaluates to: 0 / 0 Undefined / Division by Zero! Even worse with two zeros: [0, 2, 0, 4] The Solution Requirement Pure multiplication across passes Time: O(n) | Space: O(1) extra Core Insight Answer[i] = LeftPrefix[i] × RightSuffix[i] computed dynamically Two-Pass Prefix & Suffix Strategy on nums = [1, 2, 0, 4] 1. Input nums: 1 2 0 4 Pass 1: Left Prefix Products Accumulate running product of elements strictly to the left left = 1 1 2 0 Step breakdown: left[0]=1, left[1]=1×1=1, left[2]=1×2=2, left[3]=2×0=0 Pass 2: Right Suffix Products (Backward Sweep) Accumulate running product of elements strictly to the right right = 0 0 4 1 Step breakdown: right[3]=1, right[2]=4×1=4, right[1]=0×4=0, right[0]=2×0=0 Pass 3: Combine Left[i] × Right[i] (Final Answer) Multiply corresponding entries in-place or into answer array answer = 0 0 8 0 Result check at index 2 (the zero): left[2] (2) × right[2] (4) = 8 ! Correct without division. Index 0: 1×0=0 | Index 1: 1×0=0 | Index 3: 0×1=0. Robust against single & multiple zeros.
Product except self on [1, 2, 0, 4] and on [1, 2, 3, 4] overview diagram
Why

Why Division Fails Us: The Product Except Self Trap

Imagine you are handed the array . Your goal is to compute an output array where each element at index is the product of every number in except . For index 0, that means . For index 3, it means . The expected result for this entire array is . If you try the naive nested-loop approach, your runtime balloons to , which instantly fails large inputs. If you instead try the intuitive shortcut—find the total product of the array and divide by for each position—you run headfirst into a catastrophic math wall when a zero appears in the input. In our array , the total product is 0, meaning you would have to evaluate , which is undefined, or you might accidentally divide by zero at index 2. Even worse, what happens if the array contains two zeros, such as ? Every single position in the output must be 0, but division hides this structural reality behind floating-point errors and exceptions. We need a way to solve this purely through multiplication in linear time, without ever dividing.
Why Division Fails Us: The Product Except Self Trap on [1, 2, 0, 4] A zero in the input breaks division (0 / 0 undefined) and hides multiple-zero states. Input Array (nums): 1 idx 0 2 idx 1 0 idx 2 (TRAP) 4 idx 3 Try Division Shortcut Catastrophic Math Wall (Division Fails) 1. Total Product = 1 × 2 × 0 × 4 = 0 2. Divide by nums[i]: 0 / 0 at index 2 is undefined! 3. Two zeros (e.g. [0,2,0,4])? Division hides all zero states. Linear O(n) Solution: Pure Multiplication Step 1: Prefix Products (Left) Accumulate products from left to right 1 1 2 0 prefix[i] = product left of i ([1, 1, 2, 0]) Multiply Step 2: Multiply by Suffix Products (Right) Traverse right-to-left, multiply prefix by running suffix 0 2×0×4 0 1×0×4 8 1×2×4 0 1×2×0 Final Output Array: [0, 0, 8, 0] — O(n) Time, Zero Division!
Why Division Fails Us: The Product Except Self Trap diagram
Model

Two-Pass Prefix and Suffix Products Explained

When building an algorithm for the product of array except self explained approach, we must compute for every index the product of all elements to its left and all elements to its right, without ever including itself and without using division. Instead of nested loops that take time, we can maintain these two halves dynamically using two separate passes across our array. For our locked working example , the product at index 2 (which is 0) depends entirely on everything to its left () multiplied by everything to its right (4). In our first pass, we build a prefix products array that stores the running product of all elements strictly to the left of each index. In our second pass, we sweep backward from the right end of the array, maintaining a running suffix product that we multiply directly into our prefix results to form the final answer in-place.
Try this: Given nums = [1, 2, 0, 4], outline what the left prefix products array would contain before considering any elements to the right.
Product of Array Except Self — Two-Pass Prefix & Suffix Strategy Working Example: nums = [1, 2, 0, 4] | O(n) time, O(1) extra space (excluding result) Pass 1: Prefix Products (Left) i=0 1 i=1 2 i=2 0 i=3 4 Prefix Output (Left Products): 1 1 2 0 At i=2: left prod = 1 × 2 = 2 Pass 2: Suffix Sweep & Final Result Final Answer (Prefix × Suffix): Ans[0] 0 Ans[1] 0 Ans[2] 8 Ans[3] 0 Backward Suffix Sweep Deep Dive at Index 2 (num = 0): Left (2) × Right (4) = 8 Pass prefix data 1 No Division Rule Avoids division by zero (crucial when nums[i]=0) 2 O(n) Time Complexity Replaces slow O(n²) nested loops with linear sweeps 3 In-Place Suffix Accumulation Maintains running suffix in a single integer variable Quick Check [1, 2, 3, 4]: Prefix = [1, 1, 2, 6] Suffix = [24, 12, 4, 1] Result = [24, 12, 8, 6]
Two-Pass Prefix and Suffix Products Explained diagram
Worked example

Tracing the Prefix and Suffix Passes on [1, 2, 0, 4]

We take our locked example and execute the two-pass algorithm step by step. First, we compute the left prefix products where holds the product of all elements to the left of index . For the leftmost element at index 0, there are no elements to its left, so . Moving right, , , and , giving us a left prefix array of . Next, we compute the right suffix products in a reverse pass. For the rightmost element at index 3, . Moving left, , , and , yielding a right suffix array of . Finally, multiplying at each position gives .
python
def productExceptSelf(nums):
    n = len(nums)
    left = [1] * n
    right = [1] * n
    answer = [0] * n
    
    # Fill left prefix products
    for i in range(1, n):
        left[i] = left[i-1] * nums[i-1]
        
    # Fill right suffix products
    for i in range(n-2, -1, -1):
        right[i] = right[i+1] * nums[i+1]
        
    # Combine left and right
    for i in range(n):
        answer[i] = left[i] * right[i]
        
    return answer
Tracing Passes on nums = [1, 2, 0, 4] Prefix Pass → Suffix Pass → Final Answer Multiplication nums: 1 2 0 4 1. Left Prefix Pass (Left-to-Right) Accumulates product of all elements to the left (left[0] = 1) 1 i=0 1 i=1 2 i=2 0 i=3 [1, 1, 2, 0] 2. Right Suffix Pass (Right-to-Left) Accumulates product of all elements to the right (right[3] = 1) 0 i=0 0 i=1 4 i=2 1 i=3 [0, 0, 4, 1] 3. Multiply Left[i] × Right[i] Combining prefix and suffix products yields the final result array Calculation: [1×0, 1×0, 2×4, 0×1] 0 0 8 0 [0, 0, 8, 0]
Tracing the Prefix and Suffix Passes on [1, 2, 0, 4] diagram
Practice

Predicting the Zero Mutation: Testing [0, 2, 0, 4]

Now that you have traced the single-zero case for , it is time to test your mental model on a trickier mutation. Consider what happens when the input array contains multiple zeros, such as . Work through the prefix and suffix logic step by step without running code. Remember how the left-to-right pass and right-to-left pass accumulate running products, and consider what happens when a running product encounters a zero.
Try this: Predict the final output array for nums = [0, 2, 0, 4] using the two-pass method.
Apply

Transferring the Two-Pass Pattern to Related Array Problems

We have successfully tracked our running prefix and suffix products across and reasoned through multiple zeros in . The underlying pattern of the two-pass algorithm is not just a neat trick for array multiplication; it is a fundamental design template whenever every position in a collection needs global context excluding itself in time. When faced with a new neighbor problem—such as finding running bounds or prefix/suffix combinations without division—you no longer need a nested loop or a division operator. You isolate the left accumulation state, isolate the right accumulation state, and fuse them in a clean backward sweep just as we did when assembling our final answer array.
python
def productExceptSelf(nums):
    # Apply the two-pass prefix/suffix template
    pass

FAQ

What is the result of product of array except self on [1, 2, 0, 4]?
The result is [0, 0, 8, 0]. Because there is a single zero at index 2, every position except index 2 will multiply by that zero and become 0. At index 2, the product of all other elements (1 * 2 * 4) is 8.
Why can't we just compute the total product and divide by each element?
Division fails when the array contains a zero, resulting in division by zero errors. Even if there are no zeros, division can lead to floating-point precision issues or integer overflow in certain languages before division occurs.
What happens if an array has two or more zeros, like [0, 2, 0, 4]?
Every element in the output array will be 0. Since any product excluding a specific element will still include at least one remaining zero, the product for every index evaluates to 0.
What is the time and space complexity of the two-pass prefix-suffix approach?
The time complexity is O(n) because we iterate through the array twice (once for prefixes, once for suffixes). The space complexity is O(1) auxiliary space if we reuse the output array to store prefix and suffix products.

Keep learning