LeetCode 238 · Medium
Product of Array Except Self Explained (LeetCode 238)
Updated Nov 27, 2026 · 3 min read
Every answer is the product of everything to the left of a spot times everything to the right of it. Walk the list once from the left storing a running prefix product, then walk back from the right multiplying each spot by a running suffix product. That's two passes, O(n) time, and no division.
Key points
- ●answer[i] = (product of everything left of i) × (product of everything right of i).
- ●The brute force multiplies n - 1 numbers for every spot: O(n²).
- ●Store the running product first, then multiply in the current number, so a spot never counts itself.
- ●Two passes, O(n) time, and zeros are fine because nothing is divided.

The riddle: four kids, no dividing
Four kids stand in a line holding the numbers 1, 2, 3, 4. Each kid has to say the product of everyone else's number. Nobody is allowed to divide.
The answers should be 24, 12, 8, 6. The first kid needs 2 × 3 × 4, the second needs 1 × 3 × 4, and so on. That's LeetCode 238, Product of Array Except Self: given a list of integers, build a new list where each spot holds the product of every other number, in O(n) time and without division.
The slow way: everyone multiplies everyone else
The obvious approach has each kid multiply all the others. Four kids, three multiplications each: 12. With a thousand kids that's almost a million multiplications. It's O(n²), and the problem asks for O(n).
The idea: left side times right side
Look at any one kid. Everything they need sits either to their left or to their right:
answer[i] = (product of everything left of i) × (product of everything right of i)
So you don't rebuild each product from scratch. A teacher, Miss Lee, walks the line from the left, saying her running product to each kid and then multiplying in that kid's number. The kids hear 1, 1, 2, 6. Kid four hears 6, which is 1 × 2 × 3.
Then she walks back from the right with a fresh running product, starting at 1. Each kid multiplies what they heard by the new number, and she multiplies in their number after.
Trace it on [1, 2, 3, 4]
| Spot | Number | Left product | Right product | Answer |
|---|---|---|---|---|
| 0 | 1 | 1 | 2 × 3 × 4 = 24 | 24 |
| 1 | 2 | 1 | 3 × 4 = 12 | 12 |
| 2 | 3 | 1 × 2 = 2 | 4 | 8 |
| 3 | 4 | 1 × 2 × 3 = 6 | 1 | 6 |
The left column is the forward pass, the right column is the backward pass. Steps 3 and 4 of the visual sheet above show both passes, with step 6 checking each spot.
The code in Python
def product_except_self(nums):
result = [1] * len(nums)
prefix = 1
for i in range(len(nums)):
result[i] = prefix
prefix *= nums[i]
suffix = 1
for i in range(len(nums) - 1, -1, -1):
result[i] *= suffix
suffix *= nums[i]
return result
product_except_self([1, 2, 3, 4]) returns [24, 12, 8, 6]. After the first loop, result is [1, 1, 2, 6], exactly what the kids heard.
Complexity
- Time: O(n). Two passes, each touching every number once.
- Space: O(n) for the result list. Beyond the output you only keep
prefixandsuffix, so the extra space is O(1).
From O(n²) to O(n) by reusing running products instead of starting over. With 100,000 numbers, the problem's upper limit, that's the difference between about 10 billion multiplications and about 200,000.
The trap to watch for
Order inside each loop. Write first, then update. If you multiply nums[i] into prefix before storing it, every spot counts itself and the answers come out wrong.
Zeros are not a trap here. With [2, 0, 3] the forward pass stores [1, 2, 0], and the backward pass finishes it as [0, 6, 0]. Only the zero's own spot gets a non-zero answer, which is right, and you never had to divide.
Frequently asked questions
Why can't you just divide the total product by each number?
The problem forbids division, and division breaks on zeros anyway: with a 0 in the list the total is 0, and you can't divide by 0 to get back the other answers. The prefix and suffix passes never divide, so zeros need no special case.
What is the time complexity of Product of Array Except Self?
O(n) time, because each pass touches every number once. The result list is O(n), but with only two running variables on top of it, the extra space is O(1) if the output doesn't count.
Why do you store the prefix before multiplying?
Because the number at spot i must not appear in its own answer. Writing the prefix first means result[i] holds the product of everything before i. Then you multiply nums[i] into the prefix for the next spot.
Is this the same as a prefix sum?
It's the same idea with multiplication instead of addition. A prefix sum stores running totals; here you store running products, once from each side.