The problem. Given an integer array nums, return an array answer where answer[i] is the product of every element of nums except nums[i]. It must run in O(n) time and you may not use division.
Input: nums = [1, 2, 3, 4]
Output: [24, 12, 8, 6]
Input: nums = [-1, 1, 0, -3, 3]
Output: [0, 0, 9, 0, 0]For each position you need the product of all the other numbers. If division were allowed, you could multiply everything once and divide by nums[i] — but that breaks as soon as the array contains a 0, which is exactly why the problem forbids it.
For each index i, loop over the whole array and multiply every element except nums[i].
function productExceptSelf(nums) { const answer = []; for (let i = 0; i < nums.length; i++) { let product = 1; for (let j = 0; j < nums.length; j++) { if (j !== i) product *= nums[j]; } answer.push(product); } return answer;}Time O(n²), space O(1) extra. Neighbouring positions share almost all of that multiplying — that's the waste to remove.
"Everything except nums[i]" splits neatly into two parts: everything to the left of i, and everything to the right of i.
Both parts can be built incrementally. The product of everything left of i + 1 is just the product left of i, times nums[i]. So one pass left-to-right gives every prefix product, and one pass right-to-left gives every suffix product. Multiply them together and you're done.
function productExceptSelf(nums) { const n = nums.length; const left = new Array(n).fill(1); // product of everything left of i const right = new Array(n).fill(1); // product of everything right of i for (let i = 1; i < n; i++) left[i] = left[i - 1] * nums[i - 1]; for (let i = n - 2; i >= 0; i--) right[i] = right[i + 1] * nums[i + 1]; return nums.map((_, i) => left[i] * right[i]);}Time O(n), space O(n). Three linear passes — but two whole extra arrays just to hold the left and right products.
We don't need separate arrays. Write the left products straight into answer on the first pass. On the second pass, walk backwards carrying the right product in a single variable suffix, and multiply it in.
The first pass fills answer with left products; the second multiplies in the right products. Try an input with a 0, and one with two 0s.
Time O(n), space O(1) extra. Two passes, and apart from the answer array the problem asks for, only two variables.
O(n²)For each of the n positions, multiply the other n − 1 values.
O(1)Nothing extra beyond the required answer array.
Space here means extra space — the output array doesn't count, because every approach must return one.
[a, b] gives [b, a].Prefix and suffix products answer "what does everything to my left (or right) look like?" in O(1) per position. The same trick solves Trapping rain water (the tallest bar on each side) and, with sums instead of products, Subarray sum equals k later in this chapter.
O(n)Three linear passes: left products, right products, then combine.
O(n)Two extra arrays for the left and right products.
O(n)Two passes: prefixes into answer, then a suffix sweep.
O(1)Apart from the answer array the problem asks for, only one running product.