Product of array except self
Given an array of integers nums, return an array in which each position holds the product of every other value — for [1, 2, 3, 4] that is [24, 12, 8, 6]. Division may not be used, and the array may contain a zero.
Examples
Example 1
- Input
- nums = [1, 2, 3, 4]
- Output
- Position 2 holds 1 × 2 × 4 = 8 — its own 3 is the one value left out.
[24,12,8,6]
Example 2
- Input
- nums = [2, 0, 3]
- Output
- A zero makes every other position 0, and its own position the product of the rest: 2 × 3 = 6.
[0,6,0]
The Code
function productExceptSelf(nums) {
const n = nums.length;
const result = new Array(n).fill(1);
let prefix = 1;
for (let i = 0; i < n; i++) {
result[i] = prefix;
prefix *= nums[i];
}
let suffix = 1;
for (let i = n - 1; i >= 0; i--) {
result[i] *= suffix;
suffix *= nums[i];
}
return result;
}
productExceptSelf([1, 2, 3, 4]);Explanation
Each answer is everything to the left of a position multiplied by everything to its right — for position 2 in [1, 2, 3, 4] that is (1 × 2) × 4 = 8. Neither half has to be worked out from scratch at each position: one walk from the left carries the left-hand product along in prefix, and one back from the right carries the other in suffix, which is why nothing ever has to be divided out.
- 1
prefixholds the product of everything to the left ofi, and starts at 1. At position 0:result[0]takes that 1, since nothing lies to its left — thennums[0], which is 1, multiplies intoprefix, leaving it at 1.nums1234resulti10111213prefix=1
result[0] = 1 · prefix stays 1
- 2
At position 1:
result[1]takes the currentprefix, which is 1 — everything to the left of position 1 is just that first 1. Thennums[1], the 2, multiplies intoprefix.nums1234result10i111213prefix=1
result[1] = 1 · prefix → 2
- 3
At position 2:
result[2]takesprefix, now 2 — the 1 and the 2 behind it. Thennums[2], the 3, multiplies intoprefix.nums1234result1011i2213prefix=2
result[2] = 2 · prefix → 6
- 4
At position 3:
result[3]takesprefix, now 6. Thennums[3], the 4, multiplies intoprefix— making 24, which nothing will read.nums1234result101122i63prefix=6
result[3] = 6 · prefix → 24
- 5
The first pass is finished. Every position holds the product of everything before it, and nothing yet of what comes after.
nums1234result101122i63prefix=24
result → [1, 1, 2, 6]
- 6
The second pass walks back, and
suffixholds the product of everything to the right ofi, starting at 1. At position 3:result[3]is multiplied bysuffix— 6 × 1, so it does not change. Thennums[3], the 4, multiplies intosuffix.nums1234result101122i63suffix=1
result[3] = 6 × 1 = 6 · suffix → 4
- 7
At position 2:
result[2]is multiplied bysuffix— 2 × 4 = 8. Thennums[2], the 3, multiplies intosuffix.nums1234result1011i8263suffix=4
result[2] = 2 × 4 = 8 · suffix → 12
- 8
At position 1:
result[1]is multiplied bysuffix— 1 × 12 = 12. Thennums[1], the 2, multiplies intosuffix.nums1234result10i1218263suffix=12
result[1] = 1 × 12 = 12 · suffix → 24
- 9
At position 0:
result[0]is multiplied bysuffix— 1 × 24 = 24. That is the last position, and the pass ends.nums1234resulti2401218263suffix=24
result[0] = 1 × 24 = 24
- 10
Each position has been written once with everything to its left and multiplied once by everything to its right, and never by the value sitting on it.
nums1234result2401218263[24, 12, 8, 6]
More like this
All arrays & loops examples (10) →- FizzBuzz The classic warm-up — Fizz, Buzz, or the number.
- Array maximum Track the largest seen so far in a single pass.
- Two sum Remember every number you’ve seen; check for its complement.
- Majority element Pair off disagreeing votes; the majority always survives.
- Merge intervals Sort by start, then either extend the last interval or begin a new one.
- Top K frequent Bucket values by their count, then read the buckets from the top.