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.
Example 1:
Input: nums = [1,2,3,4] Output: [24,12,8,6]
Example 2:
Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0]
Constraints:
2 <= nums.length <= 105-30 <= nums[i] <= 30nums is guaranteed to fit in a 32-bit integer.
Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
Problem Overview: Given an integer array nums, build a new array where each position i contains the product of every element in the array except nums[i]. The constraint that makes this problem interesting: you cannot use division, and the solution must run in linear time.
The key observation is that the product for index i is simply the product of all elements to its left multiplied by the product of all elements to its right. Once you separate the array into prefix products and suffix products, the solution becomes straightforward.
Approach 1: Prefix and Suffix Product Arrays (Time: O(n), Space: O(n))
Create two auxiliary arrays: prefix and suffix. The prefix[i] value stores the product of all elements before index i, computed by iterating from left to right. The suffix[i] value stores the product of all elements after index i, computed by iterating from right to left. Once both arrays are built, the answer at position i becomes prefix[i] * suffix[i]. This approach is easy to reason about and clearly separates the logic for left and right products. Time complexity is O(n) because the array is scanned three times, and space complexity is O(n) for the extra arrays.
This technique frequently appears in problems involving cumulative values across an array or partial computations similar to prefix sum patterns, except multiplication replaces addition.
Approach 2: Single Array as Transformation (Time: O(n), Space: O(1) extra)
The optimal solution eliminates the explicit suffix array. First, iterate from left to right and store prefix products directly in the result array. At index i, store the product of all elements before it. Then traverse the array from right to left while maintaining a running variable that represents the suffix product. Multiply the current result value by this running suffix product, then update the suffix by multiplying it with nums[i]. Each element is processed twice, but no additional arrays are required.
The insight is that you do not need to store every suffix product—only the current cumulative value while scanning from the end. Time complexity remains O(n), while extra space drops to O(1) excluding the output array. This pattern—computing prefix values and updating them with a reverse pass—appears in many array transformation problems.
Recommended for interviews: The single-array transformation approach is what interviewers expect. The prefix/suffix array method shows you understand the decomposition of the problem, but the optimized version demonstrates awareness of space optimization and in-place computation patterns. Writing the prefix logic first and then layering the reverse suffix pass is usually the cleanest way to derive the optimal solution during an interview.
This approach involves calculating two additional arrays:
By multiplying the corresponding prefix and suffix product, we obtain the product of all other elements except the one at that given index.
The solution uses two auxiliary arrays for calculating prefixes and suffixes. The algorithm computes these in two separate for-loops and then calculates the result array in one final loop by multiplying appropriate elements of the prefix and suffix arrays.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) since it involves three passes through the array.
Space Complexity: O(n) due to the two additional arrays used.
This approach optimizes space usage by performing the transformations directly on the output array. It first fills the output array with the left product transformation, then updates it with right products.
With the first pass, it calculates the products of elements to the left, and on the second pass, it adjusts for the products of elements to the right by maintaining a right product multiplier.
This C solution reverses the calculation order to incorporate right-side product values while maintaining a single result array.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) due to linear pass transformations for left and right products.
Space Complexity: O(1) as no extra arrays are used, except for the output.
| Approach | Complexity |
|---|---|
| Prefix and Suffix Product Arrays | Time Complexity: O(n) since it involves three passes through the array. |
| Single Array as Transformation | Time Complexity: O(n) due to linear pass transformations for left and right products. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Prefix and Suffix Product Arrays | O(n) | O(n) | Best for understanding the core idea clearly or when extra memory is not a concern. |
| Single Array Transformation (Prefix + Running Suffix) | O(n) | O(1) extra | Preferred interview solution when you need optimal space while maintaining linear time. |
Product of Array Except Self - Leetcode 238 - Python • NeetCode • 747,485 views views
Watch 9 more video solutions →Practice Product of Array Except Self with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor