Product of Array Except Self: Use Two Running Products
By Hourly DSA · · 3 min read
To find the product of every element except the current one, multiply the product strictly to its left by the product strictly to its right. Two passes compute these values without division and handle zeros without a special counting branch.
Assume an integer array with at least two elements. We'll also assume the intermediate products fit the numeric representation being used.
Split one answer into two pieces
For [2, 3, 4, 5], the answer at index 1 excludes 3. Its left product is 2, and its right product is 4 × 5 = 20. Multiplying them gives 40.
The complete output is [60, 40, 30, 24].
Computing each result independently repeats most of the multiplication. Instead, reuse the work as you move through the array, much as prefix sums reuse earlier additions.
Store the product before including the current value
Scan from left to right with a running product initialized to 1. Before multiplying by the current value, store that running product in the output.
For our example, the output after this pass is [1, 2, 6, 24]. Each position contains the product strictly before it.
The first position gets 1 because it has no elements to its left. One is the empty product: multiplying by it leaves the other side unchanged.
If you update the running product before storing it, you include the very value the answer must exclude.
Complete the answers from the right
Now scan from right to left. Keep another running product, initially 1, representing the elements strictly to the right.
At index 3, multiply the stored 24 by 1, then include 5 in the running right product. At index 2, multiply 6 by 5 to get 30. At index 1, multiply 2 by 20 to get 40. At index 0, multiply 1 by 60 to get 60.
The same output array first stores partial answers and then becomes the final result:
output = array of length n
left = 1
for i from 0 through n - 1:
output[i] = left
left *= values[i]
right = 1
for i from n - 1 down through 0:
output[i] *= right
right *= values[i]
return output
The input remains unchanged. In both loops, use the running product before adding the current value to it.
Let zeros follow the same rule
For [2, 0, 4], the answer is [0, 8, 0]. At the zero's position, neither side contains that zero, so their product is 8. At every other position, one side contains it, making the answer zero.
With two zeros, every answer is zero because excluding one position still leaves at least one zero in the product.
A total-product-and-division approach cannot directly divide by a zero. The two-pass method needs no division and naturally preserves the distinction between one zero and multiple zeros.
Negative values work too. For [-2, 3, 4], the result is [12, -8, -6]; the signs come from ordinary multiplication.
State the space bound precisely
Two linear passes take O(n) time under fixed-cost multiplication. Beyond the output array, the algorithm stores only running products and loop indices, so auxiliary space is O(1). The output itself still occupies O(n) space.
Products can grow much faster than sums. If using arbitrary-precision integers, multiplication cost depends on the size of those integers. If using fixed-width numbers, ensure every intermediate product fits, including the full product computed at the ends of these loops.
Check the update order
For [3, 7], what should the first pass store?
[1, 3]. The second pass turns that into [7, 3]. Storing [3, 21] would mean the current value was included too early.
Practice stating what a running value includes with Hourly DSA. “Strictly before this index” is a useful invariant to say out loud.
Read next
Apply the same habit of precise running-state definitions to contiguous sums.