We start with an input array [1, 2, 3, 4] and initialize our result array with all 1's: [1, 1, 1, 1]
Input Array
Index 0
Index 1
Index 2
Index 3
Result Array (Initialized)
Index 0
Index 1
Index 2
Index 3
First Pass: Computing left products from left to right.
For each position, we multiply all numbers to the left of it.
For index 0, there are no elements to the left, so we keep result[0] = 1.
Input Array
Index 0
Index 1
Index 2
Index 3
Result Array (First Pass - Computing Left Products)
Index 0
Index 1
Index 2
Index 3
result[0] = 1 (no elements to the left)
First Pass: Computing left products from left to right.
For index 1, we multiply all numbers to the left: only nums[0] = 1.
Input Array
Index 0
Index 1
Index 2
Index 3
Result Array (First Pass - Computing Left Products)
Index 0
Index 1
Index 2
Index 3
result[1] = 1 × nums[0] = 1 × 1 = 1
First Pass: Computing left products from left to right.
For index 2, we multiply all numbers to the left: nums[0] × nums[1] = 1 × 2 = 2.
Input Array
Index 0
Index 1
Index 2
Index 3
Result Array (First Pass - Computing Left Products)
Index 0
Index 1
Index 2
Index 3
result[2] = 1 × nums[0] × nums[1] = 1 × 1 × 2 = 2
First Pass: Computing left products from left to right.
For index 3, we multiply all numbers to the left: nums[0] × nums[1] × nums[2] = 1 × 2 × 3 = 6.
Input Array
Index 0
Index 1
Index 2
Index 3
Result Array (First Pass - Computing Left Products)
Index 0
Index 1
Index 2
Index 3
result[3] = 1 × nums[0] × nums[1] × nums[2] = 1 × 1 × 2 × 3 = 6
First Pass Complete: We now have the products of all elements to the left of each index.
Result array after first pass: [1, 1, 2, 6]
Input Array
Index 0
Index 1
Index 2
Index 3
Result Array After First Pass
Index 0
Index 1
Index 2
Index 3
Second Pass: Computing right products from right to left.
We'll use a variable 'right' to keep track of the product of all elements to the right.
Initially, right = 1 (no elements to the right of the last element).
Input Array
Index 0
Index 1
Index 2
Index 3
Result Array (After First Pass)
Index 0
Index 1
Index 2
Index 3
Initialize right = 1
Second Pass: Starting from the rightmost element (index 3).
For the last index, we multiply the current result by the right product (which is 1).
Input Array
Index 0
Index 1
Index 2
Index 3
Result Array (Second Pass - Computing Right Products)
Index 0
Index 1
Index 2
Index 3
result[3] = result[3] × right = 6 × 1 = 6
Update right = right × nums[3] = 1 × 4 = 4
Second Pass: Moving to index 2.
We multiply the current result[2] by the right product (which is now 4).
Input Array
Index 0
Index 1
Index 2
Index 3
Result Array (Second Pass - Computing Right Products)
Index 0
Index 1
Index 2
Index 3
result[2] = result[2] × right = 2 × 4 = 8
Update right = right × nums[2] = 4 × 3 = 12
Second Pass: Moving to index 1.
We multiply the current result[1] by the right product (which is now 12).
Input Array
Index 0
Index 1
Index 2
Index 3
Result Array (Second Pass - Computing Right Products)
Index 0
Index 1
Index 2
Index 3
result[1] = result[1] × right = 1 × 12 = 12
Update right = right × nums[1] = 12 × 2 = 24
Second Pass: Finally at index 0.
We multiply the current result[0] by the right product (which is now 24).
Input Array
Index 0
Index 1
Index 2
Index 3
Result Array (Second Pass - Computing Right Products)
Index 0
Index 1
Index 2
Index 3
result[0] = result[0] × right = 1 × 24 = 24
Final Result: We have now computed the product of all elements except for itself at each index.
The final result array is [24, 12, 8, 6].
Input Array
Index 0
Index 1
Index 2
Index 3
Final Result Array
Index 0
Index 1
Index 2
Index 3
For each index, we've computed the product of all other elements:
result[0] = 2 × 3 × 4 = 24
result[1] = 1 × 3 × 4 = 12
result[2] = 1 × 2 × 4 = 8
result[3] = 1 × 2 × 3 = 6