AlgoMaster Logo

Product of Array Except Self

Last Updated: April 2, 2026

medium
4 min read

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 the tempting approach of computing the total product and dividing by nums[i] (which would also break for zeros).

So how do we get the product of everything except nums[i] without division? Think about it this way: the product of all elements except nums[i] is really just two things multiplied together:

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

This prefix-suffix decomposition is the key insight. If we can efficiently compute these left and right products for every index, we're done.

Key Constraints:

  • 2 <= nums.length <= 10^5 → With up to 100,000 elements, we need O(n) time (the problem explicitly demands it). An O(n^2) brute force will TLE.
  • -30 <= nums[i] <= 30 → Small values, so individual products won't overflow. But the cumulative product across many elements is guaranteed to fit in a 32-bit integer by the problem statement.
  • No division allowed → This eliminates the obvious "compute total product and divide" shortcut. We must build the answer using only multiplication.
  • Follow-up: O(1) extra space → The output array doesn't count, so we need to avoid creating additional arrays.

Approach 1: Brute Force

Intuition

The most direct approach: for each index i, loop through the entire array and multiply every element except nums[i]. No tricks, no cleverness, just brute computation.

This is what most people think of first, and it works. It's just slow.

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

This approach works but recomputes nearly identical products for every index. What if we precomputed the product of all elements to the left and to the right of each index, so we could combine them in O(1)?

Approach 2: Prefix and Suffix Products

Intuition

The brute force recomputes almost the same product over and over. When computing answer[3], we multiply everything except nums[3]. When computing answer[4], we multiply everything except nums[4]. Those two products share almost all their terms.

Here's the observation that changes everything: the product of all elements except nums[i] can be split into two parts:

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

We can build both arrays in O(n) time with simple left-to-right and right-to-left passes. Then answer[i] = 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/7

Code

This approach achieves O(n) time but uses two auxiliary arrays of size n. What if we stored the prefix products directly in the output array and replaced the suffix array with a single running variable?

Approach 3: Optimized (Single Output Array)

Intuition

The prefix-suffix approach uses two extra arrays. But notice something: we don't actually need to store both arrays simultaneously. We can use the output array itself to store the prefix products first, and then do a single right-to-left pass where we multiply in the suffix products on the fly using a running variable.

This is a classic space optimization technique: replace an array with a single variable when you only need the running value.

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