Product of Array Except Self — Problem & Solution
Product of Array Except Self solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem.
Product of Array Except Self is a medium coding problem on arrays and prefix sum, asked in Freshworks interviews.
- Difficulty: Medium
- Topics: Arrays, Prefix Sum
- Asked at: Freshworks
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an integer array nums, return an array answer where answer[i] is the product of every element of nums except nums[i].
You must solve it without using division and in O(n) time.
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 <= 10-4 <= nums[i] <= 4
Follow-up: can you do it in O(1) extra space (the output array does not count)?
Hints for Product of Array Except Self
Hint 1
answer[i] equals (product of everything to the left of i) × (product of everything to the right).
Hint 2
Two sweeps: one accumulating prefix products, one accumulating suffix products into the same output.
How to solve Product of Array Except Self
The product excluding index i is the product of everything to its left times everything to its right. Compute those two running products in two sweeps, and division is never needed — which matters because a single zero would make division impossible.
Approach
- Sweep left to right, writing into
answer[i]the product of all elements strictly beforei. - Start that running product at
1, so the first entry is1. - Sweep right to left with a second running product of everything strictly after
i. - Multiply it into
answer[i]as you go. - The output array itself carries the left products, so no third array is needed.
Why it works
Splitting at index i partitions the other elements into exactly two groups, so their products multiply to give the answer — no element is counted twice or missed. Doing it as prefix and suffix sweeps means each product is built incrementally rather than recomputed, giving linear time. It also handles zeros for free: a zero simply propagates into whichever side contains it, with no special-casing of one-zero or two-zero inputs.
Complexity
- Time —
O(n) - Space —
O(1) extra, not counting the output
Pitfalls
- Division is forbidden, and would need awkward special cases for one zero and for two or more zeros.
- Both running products start at
1, the multiplicative identity. - The products must exclude
iitself — multiply before folding the current element into the running product. - Reuse the output array for the left pass to keep the extra space constant.
Reference solution
Python
from typing import List
def productExceptSelf(nums: List[int]) -> List[int]:
n = len(nums)
out = [1] * n
acc = 1
for i in range(n):
out[i] = acc
acc *= nums[i]
acc = 1
for i in range(n - 1, -1, -1):
out[i] *= acc
acc *= nums[i]
return outJava
public static int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] answer = new int[n];
int running = 1;
for (int i = 0; i < n; i++) {
answer[i] = running;
running *= nums[i];
}
running = 1;
for (int i = n - 1; i >= 0; i--) {
answer[i] *= running;
running *= nums[i];
}
return answer;
}C++
vector<int> productExceptSelf(vector<int>& nums) {
int n = (int) nums.size();
vector<int> answer(n, 1);
int running = 1;
for (int i = 0; i < n; i++) {
answer[i] = running;
running *= nums[i];
}
running = 1;
for (int i = n - 1; i >= 0; i--) {
answer[i] *= running;
running *= nums[i];
}
return answer;
}JavaScript
var productExceptSelf = function(nums) {
const n = nums.length;
const out = new Array(n).fill(1);
let acc = 1;
for (let i = 0; i < n; i++) {
out[i] = acc;
acc *= nums[i];
}
acc = 1;
for (let i = n - 1; i >= 0; i--) {
out[i] *= acc;
acc *= nums[i];
}
return out;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Prefix Sum