AlgoMaster Logo

Product of Array Except Self

mediumFrequency6 min readUpdated June 23, 2026

Understanding the Problem

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:

  • The product of everything to the left of index i
  • The product of everything to the right of index i

This prefix-suffix decomposition drives every efficient solution below. Computing these left and right products for every index gives the answer without any division.

Key Constraints:

  • 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.
  • The cumulative product of any prefix or suffix is guaranteed to fit in a 32-bit integer, so int arithmetic is safe and no long is needed.
  • No division allowed → The answer must be built using only multiplication.
  • Follow-up: O(1) extra space → The output array does not count, so the goal is to avoid allocating additional arrays.

Approach 1: Brute Force

Intuition

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.

Algorithm

  1. Create a result array of the same length as nums.
  2. For each index i, iterate through the entire array.
  3. Multiply together every element where the index is not i.
  4. Store the product in result[i].
  5. Return the result array.

Example Walkthrough

1Initialize: result = [0, 0, 0, 0]
0
1
1
2
2
3
3
4
1/6

Code

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.

Approach 2: Prefix and Suffix Products

Intuition

The product of all elements except nums[i] splits into two parts:

  • Prefix product: The product of all elements before index i
  • Suffix product: The product of all elements after index i

Both 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].

Algorithm

  1. Create a prefix array where prefix[i] holds the product of all elements before index i.
  2. Build it left-to-right: start with prefix[0] = 1, then prefix[i] = prefix[i-1] * nums[i-1].
  3. Create a suffix array where suffix[i] holds the product of all elements after index i.
  4. Build it right-to-left: start with suffix[n-1] = 1, then suffix[i] = suffix[i+1] * nums[i+1].
  5. The answer at each index is prefix[i] * suffix[i].

Example Walkthrough

1Start with nums = [1, 2, 3, 4]
0
1
1
2
2
3
3
4
1/10

Code

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.

Approach 3: Optimized (Single Output Array)

Intuition

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.

Algorithm

  1. Store prefix products directly in the result array (left-to-right pass).
  2. Initialize a variable suffixProduct = 1.
  3. Traverse right-to-left, multiplying each result[i] by suffixProduct.
  4. After multiplying, update suffixProduct by multiplying it with nums[i].
  5. Return the result array.

Example Walkthrough

1nums = [-1, 1, 0, -3, 3]. Pass 1: build prefix products in result
0
-1
1
1
2
0
3
-3
4
3
1/7

Code