Product of Array Except Self
Product of Array Except Self is LeetCode 238 (Medium). Given an integer array nums, return an array answer where answer[i] is the product of every element of nums except nums[i].
- You must not use division, and the solution must run in O(n) time.
numshas up to 10⁵ values between −30 and 30, and every answer fits in a 32-bit integer.- Follow-up: use O(1) extra space; the output array does not count.
- 2 <= nums.length <= 10⁵
- -30 <= nums[i] <= 30
- The input is generated such that answer[i] is guaranteed to fit in a 32-bit integer.
Intuition
Everything except position i splits into two parts: the numbers to its left and the numbers to its right. So answer[i] is the product of the left part times the product of the right part.
Both parts can be built as running products. Walking left to right, one running product holds everything before i; walking right to left, another holds everything after i. Each step costs one multiplication, nothing is recomputed, and no division is needed.
An empty part (nothing left of index 0, nothing right of the last index) has product 1, which is why both running products start at 1.
When every output combines all elements except the current one, split it into a part before and a part after. Running products or sums from both ends give each part in one pass. Trapping Rain Water (42) and Candy (135) use the same left pass plus right pass idea.
Approach
Before reading on: for [1, 2, 3, 4], write the product of everything left of each index, then everything right of it. How do the two lists give the answer? Aim for O(n) time without division.
Two ways to solve it
The left products are written straight into answer, and the right side is carried in one variable.
- Passes: two.
- Extra state: two numbers,
prefixandsuffix. - Fits: the O(1)-space follow-up.
The version interviewers expect in the end.
Build left[i], the product before i, and right[i], the product after i, as full arrays, then multiply them.
- Passes: three.
- Extra state: two arrays of length n.
- Fits: a first version that shows both parts plainly.
The usual stepping stone to the best version.
Both run in O(n) without division, and the one-array version drops the two helper arrays, so it is the one to use. The steps, code and live demo follow it; the two-array code is further down.
Fill in the left products
Set prefix = 1. For i from left to right, write answer[i] = prefix, then multiply prefix by nums[i]. Writing first keeps nums[i] out of its own answer, so after this pass answer[i] holds the product of everything left of i.
Multiply in the right products
Set suffix = 1. For i from right to left, multiply answer[i] by suffix, then multiply suffix by nums[i]. Now answer[i] is left part × right part. The right side lives in one variable instead of a second array, which meets the O(1)-space follow-up.
Code in Python, C++ and Java
answer starts as all 1s and prefix starts at 1, the product of an empty list. Starting at 0 would make every answer 0.answer[0] gets the empty product 1. Only after writing, multiply prefix by nums[0] = 1, giving 1, so that nums[0] never lands in its own answer.prefix already holds exactly that, so write it into answer[1]. Only after writing, multiply prefix by nums[1] = 2, giving 2, so that nums[1] never lands in its own answer.prefix already holds exactly that, so write it into answer[2]. Only after writing, multiply prefix by nums[2] = 3, giving 6, so that nums[2] never lands in its own answer.prefix already holds exactly that, so write it into answer[3]. Only after writing, multiply prefix by nums[3] = 4, giving 24, so that nums[3] never lands in its own answer.answer[i] holds the product of everything to its left. Now walk from the right with suffix = 1 and multiply in everything to the right. One variable is enough, so no second array is needed.suffix is still 1 and answer[3] stays 6. Then multiply suffix by 4, giving 4.answer[2]: 2 × 4 = 8, the left part times the right part. Then multiply suffix by 3, giving 12.answer[1]: 1 × 12 = 12, the left part times the right part. Then multiply suffix by 2, giving 24.answer[0]: 1 × 24 = 24, the left part times the right part. Then multiply suffix by 1, giving 24.answer[i] is the product of the left part and the right part, which is every value except nums[i]. Two passes of 4 steps, and the only extra memory was prefix and suffix.answer starts as all 1s and prefix starts at 1, the product of an empty list. Starting at 0 would make every answer 0.answer[0] gets the empty product 1. Only after writing, multiply prefix by nums[0] = -1, giving -1, so that nums[0] never lands in its own answer.prefix already holds exactly that, so write it into answer[1]. Only after writing, multiply prefix by nums[1] = 1, giving -1, so that nums[1] never lands in its own answer.prefix already holds exactly that, so write it into answer[2]. Only after writing, multiply prefix by nums[2] = 0, giving 0, so that nums[2] never lands in its own answer.prefix already holds exactly that, so write it into answer[3]. Only after writing, multiply prefix by nums[3] = -3, giving 0, so that nums[3] never lands in its own answer.prefix already holds exactly that, so write it into answer[4]. Only after writing, multiply prefix by nums[4] = 3, giving 0, so that nums[4] never lands in its own answer.answer[i] holds the product of everything to its left. Now walk from the right with suffix = 1 and multiply in everything to the right. One variable is enough, so no second array is needed.suffix is still 1 and answer[4] stays 0. Then multiply suffix by 3, giving 3.answer[3]: 0 × 3 = 0, the left part times the right part. Then multiply suffix by -3, giving -9.answer[2]: -1 × -9 = 9, the left part times the right part. nums[2] is 0, so from here on suffix is 0 and every answer further left becomes 0, as it should: each of them has this zero on its right.answer[1]: -1 × 0 = 0, the left part times the right part. Then multiply suffix by 1, giving 0.answer[0]: 1 × 0 = 0, the left part times the right part. Then multiply suffix by -1, giving 0.answer[i] is the product of the left part and the right part, which is every value except nums[i]. Two passes of 5 steps, and the only extra memory was prefix and suffix.Prefix and suffix product arrays
left[i] is the product of everything before i, built from left to right; right[i] is the product of everything after i, built from right to left. Multiplying the two arrays element by element gives the answer.
Common pitfalls
Multiplying nums[i] in before writing
prefix *= nums[i] answer[i] = prefix
answer[i] = prefix prefix *= nums[i]
Updating first puts nums[i] into its own answer: for [1, 2, 3, 4] the first pass would write [1, 2, 6, 24] instead of [1, 1, 2, 6]. The right-to-left pass needs the same order with suffix.
Edge cases
In [-1, 1, 0, -3, 3] every index except the zero's has the 0 on its left or right, so it gets 0, and the zero's own index gets the product of the rest, 9. With two zeros every answer is 0. The same two passes do this with no special branch.
Complexity
answer, which the problem does not count, the code keeps only prefix and suffix.Product of Array Except Self FAQ
Why not divide the total product by nums[i]?
The problem forbids division, and it also breaks on zeros: dividing by 0 fails, so you would need separate cases for no zeros, one zero and two or more zeros. The two-pass method above handles all three with the same two passes.