At each index i, we need the product of every element in the array except the one at position i. The no-division constraint rules out computing the total product once and dividing by nums[i]. That shortcut also fails when the array contains a zero, since dividing by zero is undefined and a single zero would make the total product zero for every other index too.
The product of all elements except nums[i] is the product of two sub-ranges:
iiThis prefix-suffix decomposition drives every efficient solution below. Computing these left and right products for every index gives the answer without any division.
2 <= nums.length <= 10^5 → With up to 100,000 elements, an O(n^2) brute force performs around 10^10 operations and exceeds the time limit. The problem also explicitly requires O(n) time.int arithmetic is safe and no long is needed.Compute each answer independently. For each index i, loop through the entire array and multiply together every element whose index is not i. This follows directly from the definition of the problem and produces a correct result, but it repeats most of its work.
nums.i, iterate through the entire array.i.result[i].Computing answer[i] and answer[i+1] repeats almost the same multiplication, since the two products differ only by which single element they exclude. The next approach removes that repetition by precomputing the left and right products once.
The product of all elements except nums[i] splits into two parts:
iiBoth can be built in O(n) time. A left-to-right pass accumulates the prefix products, and a right-to-left pass accumulates the suffix products. The answer at each index is then prefix[i] * suffix[i].
prefix[i] is defined to cover indices 0..i-1 and suffix[i] to cover indices i+1..n-1. Neither includes nums[i], and together they cover every other index exactly once. Their product is therefore the product of all elements except nums[i]. The boundary values prefix[0] = 1 and suffix[n-1] = 1 represent an empty range, which correctly contributes a factor of 1.
prefix array where prefix[i] holds the product of all elements before index i.prefix[0] = 1, then prefix[i] = prefix[i-1] * nums[i-1].suffix array where suffix[i] holds the product of all elements after index i.suffix[n-1] = 1, then suffix[i] = suffix[i+1] * nums[i+1].prefix[i] * suffix[i].prefix and suffix) of size n. The output array doesn't count as extra space per the problem statement.This achieves O(n) time but uses two auxiliary arrays of size n. The next approach removes both by storing the prefix products directly in the output array and replacing the suffix array with a single running variable.
The two extra arrays from the previous approach are never both needed at full size. The output array can hold the prefix products after a first pass. A second pass then walks right to left, multiplying each entry by a running suffix product that is updated as it moves. This replaces the suffix array with a single variable, since only its current value is ever read.
suffixProduct = 1.result[i] by suffixProduct.suffixProduct by multiplying it with nums[i].After the first pass, result[i] holds the product of all elements to the left of index i. In the second pass, suffixProduct is multiplied into result[i] before it is updated with nums[i]. So at the moment of the multiply, suffixProduct equals the product of indices i+1..n-1 only, never including nums[i]. Combining the stored left product with this right product leaves each position holding the product of every element except itself. The ordering of the two lines inside the loop is what keeps nums[i] out of its own answer.
suffixProduct). The output array doesn't count as extra space per the problem statement.