Product of Array Except Self
Medium · Arrays & hashing
Jump to codeFor each position, return the product of every array element except the one at that position. Use no division and aim for linear time. The array has at least two integers.
Examples
Example 1
nums = [1,2,3,4]
Output: [24,12,8,6]
Example 2
nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
Constraints
- 2 <= nums.length <= 1000
- -20 <= nums[i] <= 20
- Each output product fits a signed 32-bit integer.
Need a hint?
Separate the elements before an index from the elements after it.
Asked in real interviews
Candidates reported versions of Product of Array Except Self from these company interviews. Some company questions are locked and open with Premium or XP.
- Solve weighted pick and product except selfMeta
- Compute product excluding index without divisionAmazon
- Product of Array Except Self Without DivisionNewrelic
Solution approach
In a forward pass, store the product strictly before each index. In a backward pass, multiply that entry by the product strictly after the index. Update each running product only after using it. This naturally handles one or multiple zeros.
Complexity
O(n) time and O(1) extra space excluding the output array.
Python reference solution
def productExceptSelf(nums):
answer = [1] * len(nums)
prefix = 1
for i, value in enumerate(nums):
answer[i] = prefix
prefix *= value
suffix = 1
for i in range(len(nums) - 1, -1, -1):
answer[i] *= suffix
suffix *= nums[i]
return answer
JavaScript solution
function productExceptSelf(nums) {
const answer = Array(nums.length).fill(1);
let prefix = 1, suffix = 1;
for (let i = 0; i < nums.length; i++) {
answer[i] = prefix;
prefix *= nums[i];
}
for (let i = nums.length - 1; i >= 0; i--) {
answer[i] *= suffix;
suffix *= nums[i];
}
return answer;
}
Java solution
import java.util.*;
class Solution {
public int[] productExceptSelf(int[] nums) {
int[] answer = new int[nums.length];
int prefix = 1, suffix = 1;
for (int i = 0; i < nums.length; i++) {
answer[i] = prefix;
prefix *= nums[i];
}
for (int i = nums.length - 1; i >= 0; i--) {
answer[i] *= suffix;
suffix *= nums[i];
}
return answer;
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> productExceptSelf(vector<int> &nums) {
vector<int> answer(nums.size(), 1);
int prefix = 1, suffix = 1;
for (size_t i = 0; i < nums.size(); i++) {
answer[i] = prefix;
prefix *= nums[i];
}
for (int i = (int)nums.size() - 1; i >= 0; i--) {
answer[i] *= suffix;
suffix *= nums[i];
}
return answer;
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 10 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
1,
2,
3,
4
]
][ 24, 12, 8, 6 ]
[
[
-1,
1,
0,
-3,
3
]
][ 0, 0, 9, 0, 0 ]
[
[
2,
3,
4,
5
]
][ 60, 40, 30, 24 ]
[
[
1,
0
]
][ 0, 1 ]
[
[
0,
0,
2
]
][ 0, 0, 0 ]
Drafts are saved per language in this browser when storage is available.