Skip to content

Product of Array Except Self

Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation.

InputOutput
[1,2,3,4][24,12,8,6]
[-1,1,0,-3,3][0,-9,0,9,0]
  • 2 <= nums.length <= 10^5
  • -30 <= nums[i] <= 30
  • The product of any prefix or suffix fits in a 32-bit integer
ApproachTimeSpaceBest When
Prefix/Suffix ArraysO(n)O(n)Clarity matters, extra memory allowed
Two-Pass (O(1) space)O(n)O(1)*Follow-up requirement, memory-constrained

*The output array is not counted as extra space per the problem convention.

★ Recommended
⏱ Time O(n) Two passes 💾 Space O(n) Two extra arrays

Build a prefix array where prefix[i] is the product of all elements to the left of index i, and a suffix array where suffix[i] is the product of all elements to the right of index i. The answer at each index is simply prefix[i] * suffix[i].

Input: [1, 2, 3, 4]

iprefix[i]suffix[i]output[i]
012424
111212
2248
3616

Animated walkthrough

Building prefix and suffix arrays

Use Next or Autoplay to watch the prefix array fill left→right, then the suffix array fill right→left, before combining them.

Step 1 of 4
Waiting to begin...
Array state
Memory / lookup state

function productExceptSelf(nums):
n = len(nums)
prefix = [1] * n
suffix = [1] * n
for i in range(1, n):
prefix[i] = prefix[i-1] * nums[i-1]
for i in range(n-2, -1, -1):
suffix[i] = suffix[i+1] * nums[i+1]
return [prefix[i] * suffix[i] for i in range(n)]
product_except_self_prefix_suffix.py
from typing import List
def product_except_self(nums: List[int]) -> List[int]:
n = len(nums)
prefix = [1] * n
suffix = [1] * n
for i in range(1, n):
prefix[i] = prefix[i - 1] * nums[i - 1]
for i in range(n - 2, -1, -1):
suffix[i] = suffix[i + 1] * nums[i + 1]
return [prefix[i] * suffix[i] for i in range(n)]
💾 Space-Optimal
⏱ Time O(n) 💾 Space O(1) Output array only

Reuse the output array itself instead of allocating separate prefix and suffix arrays. The first pass (left→right) fills each slot with the running prefix product. The second pass (right→left) multiplies each slot by a running suffix product variable.

Input: [1, 2, 3, 4]

After pass 1 (left prefix into output): output = [1, 1, 2, 6]

Pass 2 (right→left, running suffix starts at 1):

ioutput[i] beforesuffixoutput[i] aftersuffix update
3616×1 = 61×4 = 4
2242×4 = 84×3 = 12
11121×12 = 1212×2 = 24
01241×24 = 24

Result: [24, 12, 8, 6]

Animated walkthrough

Two-pass in-place product

Pass 1 fills the output with left-prefix products. Pass 2 multiplies in right-suffix products using a single variable.

Step 1 of 3
Waiting to begin...
Array state
Memory / lookup state

function productExceptSelfOptimal(nums):
n = len(nums)
output = [1] * n
prefix = 1
for i in range(n):
output[i] = prefix
prefix *= nums[i]
suffix = 1
for i in range(n-1, -1, -1):
output[i] *= suffix
suffix *= nums[i]
return output
product_except_self_two_pass.py
from typing import List
def product_except_self(nums: List[int]) -> List[int]:
n = len(nums)
output = [1] * n
prefix = 1
for i in range(n):
output[i] = prefix
prefix *= nums[i]
suffix = 1
for i in range(n - 1, -1, -1):
output[i] *= suffix
suffix *= nums[i]
return output
✓ Simple

A third approach demonstrating an alternative technique for solving this problem. This implementation optimizes either time or space complexity compared to the previous approaches.

⏱ Time O(n) Optimized complexity 💾 Space O(1) Efficient space usage
function approach3():
// Implementation approach goes here
product_of_array_except_self_space_optimized.py
"""
Solution for Product of Array Except Self
"""
def solve():
"""Implementation for approach 3 of Product of Array Except Self"""
pass
if __name__ == "__main__":
print("Solution ready")