Product array Puzzle
Pattern: Prefix sums and difference arrays
For each position, multiply the product of everything to its left by the product of everything to its right, without using division.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
You are given an array nums with at least 2 whole numbers. Return an array answer where answer[i] is the product of
every element except nums[i], without using division. For example, [1, 2, 3, 4] gives [24, 12, 8, 6]. Assume every
product of a prefix or suffix of the array fits in a 32-bit integer.
This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name
productExceptSelf and the limits are this guide's own; LeetCode has a very similar problem under that name.
Think first
- With division, you could take the product of everything and divide by
nums[i]. What goes wrong when there is a 0? - "Everything except
nums[i]" is "everything to the left" plus "everything to the right". How could you get both quickly for everyi? - Can you avoid a second extra array for the right-hand products?
Hints
Brute force, and why it fails
For each i, multiply all the other elements: O(n²). For 100,000 elements, 10¹⁰ multiplications. Using division is O(n)
but breaks on zeros and is not allowed here.
The key insight
"Everything except me" splits into a left part and a right part, and running products give both in two passes. Storing the left products in the answer array itself means the only extra memory is one variable.
Dry run
| answer | [1, ?, ?, ?] |
|---|
nums = [1, 2, 3, 4]. First pass from the left. answer[i] becomes the product of everything before i.
The plan
answer[0] = 1
for i from 1 to n - 1:
answer[i] = answer[i - 1] * nums[i - 1] # product of the left side
right = 1
for i from n - 1 down to 0:
answer[i] = answer[i] * right # times the product of the right side
right = right * nums[i]
return answer
Complexity
- Time
O(n)- Space
O(1)
Two passes. Apart from the answer array that is returned, only one variable is used.
Edge cases
- One zero: every position except the zero's gets 0, and the zero's position gets the product of the others.
- Two or more zeros: every answer is 0.
- Two elements: each answer is the other element.
I've built my logic, show the code
Java
class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] answer = new int[n];
answer[0] = 1;
for (int i = 1; i < n; i++) {
answer[i] = answer[i - 1] * nums[i - 1]; // product of everything to the left of i
}
int right = 1; // product of everything to the right of i
for (int i = n - 1; i >= 0; i--) {
answer[i] = answer[i] * right;
right = right * nums[i];
}
return answer;
}
}C++
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n = static_cast<int>(nums.size());
vector<int> answer(n, 1);
for (int i = 1; i < n; i++) {
answer[i] = answer[i - 1] * nums[i - 1]; // product of everything to the left of i
}
int right = 1; // product of everything to the right of i
for (int i = n - 1; i >= 0; i--) {
answer[i] = answer[i] * right;
right = right * nums[i];
}
return answer;
}
};Python
class Solution:
def productExceptSelf(self, nums):
n = len(nums)
answer = [1] * n
for i in range(1, n):
answer[i] = answer[i - 1] * nums[i - 1] # product of everything to the left of i
right = 1 # product of everything to the right of i
for i in range(n - 1, -1, -1):
answer[i] = answer[i] * right
right = right * nums[i]
return answerCommon mistakes
- Using division, which fails on zeros.
- Updating
rightbefore multiplying it intoanswer[i], which includesnums[i]itself. - Building a separate right-products array when one running variable is enough.