1. Left product
Store the product of everything to the left of i.
result[i] = prefixTyporithm · medium
Step 1 of 8 · intro card
Prefix Sums
Two passes: fill result with left products, then multiply each by the running right product — no division needed.
Each output is the product of all elements except the one at that index.
A forward pass stores the product of everything to the left of each index.
A backward pass multiplies in the product of everything to the right, avoiding division entirely.
Left products in a forward pass.
Right products in a backward pass.
No division — handles zeros safely.
function productExceptSelf(nums) {
const n = nums.length;
const result = new Array(n).fill(1);
let prefix = 1;
for (let i = 0; i < n; i += 1) {
result[i] = prefix;
prefix *= nums[i];
}
let suffix = 1;
for (let i = n - 1; i >= 0; i -= 1) {
result[i] *= suffix;
suffix *= nums[i];
}
return result;
}Connect each part of the code to the concept before you type.
Store the product of everything to the left of i.
result[i] = prefixFold nums[i] into the running left product.
prefix *= nums[i]Multiply by the product of everything to the right.
result[i] *= suffixFold nums[i] into the running right product.
suffix *= nums[i]Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.