Product of All Elements Except Self
Solve this Problemnums, return an array result where result[i] is the product of every element in nums except nums[i] — without using division, and ideally in O(n) time.
Multiplying everything except one index, one index at a time, repeats almost the same work over and over. The prefix productPrefix ProductThe running product of every element from the start of the array up to (but not including) a given index — the multiplicative sibling of a prefix sum. trick reuses that work: sweep once left-to-right building the product of everything to the left of each index, then once right-to-left building the product of everything to the right, multiplying the two halves together as you go.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
2 ≤ nums.length ≤ 10⁵ - ◆
-30 ≤ nums[i] ≤ 30 - ◆
The product of any prefix or suffix fits in a 32-bit integer - ◆
Division is not allowed as a solving technique
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public int[] productExceptSelf(int[] nums) { |
| 3 | int n = nums.length; |
| 4 | int[] result = new int[n]; |
| 5 | result[0] = 1; |
| 6 | for (int i = 1; i < n; i++) { |
| 7 | result[i] = result[i - 1] * nums[i - 1]; |
| 8 | } |
| 9 | int suffix = 1; |
| 10 | for (int i = n - 1; i >= 0; i--) { |
| 11 | result[i] *= suffix; |
| 12 | suffix *= nums[i]; |
| 13 | } |
| 14 | return result; |
| 15 | } |
| 16 | } |
| 17 |
1result[0] = 1 — there's nothing to the left of index 0, so its prefix product is empty (1).
Approach & Solutions
Brute Force
BruteFor every index i, loop over the whole array again and multiply together every element except nums[i]. Correct, but it repeats almost the same multiplication work for every single index instead of reusing anything from the index before it.
O(n²)O(n)1class Solution {
2 public int[] productExceptSelf(int[] nums) {
3 int[] result = new int[nums.length];
4 for (int i = 0; i < nums.length; i++) {
5 int product = 1;
6 for (int j = 0; j < nums.length; j++) {
7 if (j != i) product *= nums[j];
8 }
9 result[i] = product;
10 }
11 return result;
12 }
13}Optimal — Prefix × Suffix Product
OptimalThis is the same prefix-sum idea, but with multiplication and swept from both directions. First pass, left to right: fill result[i] with the product of everything strictly to the left of i (result[0] = 1, since nothing is to its left). Second pass, right to left: multiply each result[i] by a running suffix product of everything strictly to the right of i, updating that running product as you go. Left-of-i times right-of-i covers every element except i itself — no division needed.
O(n)O(1)1class Solution {
2 public int[] productExceptSelf(int[] nums) {
3 int n = nums.length;
4 int[] result = new int[n];
5 result[0] = 1;
6 for (int i = 1; i < n; i++) {
7 result[i] = result[i - 1] * nums[i - 1];
8 }
9 int suffix = 1;
10 for (int i = n - 1; i >= 0; i--) {
11 result[i] *= suffix;
12 suffix *= nums[i];
13 }
14 return result;
15 }
16}