Product of array except self without division. Show brute force, division and prefix/suffix products.
Problem
Return out[i] = product of every element except nums[i]. [1, 2, 3, 4] → [24, 12, 8, 6]. Constraint: O(n), and usually "without using division".
Pattern
Prefix and suffix accumulation. Whenever an answer for position i depends on "everything to the left" and "everything to the right", precompute prefix and suffix aggregates.
Approach 1: Nested loops (feasible, O(n²))
class ProductBrute {
static int[] compute(int[] nums) {
int[] out = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
int p = 1;
for (int j = 0; j < nums.length; j++) if (j != i) p *= nums[j];
out[i] = p;
}
return out;
}
}Approach 2: Total product divided by each element (O(n), but fragile)
Breaks on zeros unless handled carefully (one zero: only that position is non-zero; two zeros: everything is zero), and interviews usually forbid division.
class ProductDivision {
static int[] compute(int[] nums) {
int zeros = 0, productNonZero = 1;
for (int x : nums) { if (x == 0) zeros++; else productNonZero *= x; }
int[] out = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
if (zeros > 1) out[i] = 0;
else if (zeros == 1) out[i] = nums[i] == 0 ? productNonZero : 0;
else out[i] = productNonZero / nums[i];
}
return out;
}
}Approach 3: Prefix and suffix arrays (O(n) time, O(n) extra space)
class ProductPrefixSuffix {
static int[] compute(int[] nums) {
int n = nums.length;
int[] left = new int[n], right = new int[n], out = new int[n];
left[0] = 1;
for (int i = 1; i < n; i++) left[i] = left[i - 1] * nums[i - 1];
right[n - 1] = 1;
for (int i = n - 2; i >= 0; i--) right[i] = right[i + 1] * nums[i + 1];
for (int i = 0; i < n; i++) out[i] = left[i] * right[i];
return out;
}
}Approach 4: Prefix in the output array, suffix in one variable (O(n), O(1) extra) — optimal
import java.util.Arrays;
class ProductOptimal {
static int[] compute(int[] nums) {
int n = nums.length;
int[] out = new int[n];
out[0] = 1;
for (int i = 1; i < n; i++) out[i] = out[i - 1] * nums[i - 1]; // left products
int suffix = 1;
for (int i = n - 1; i >= 0; i--) {
out[i] *= suffix; // multiply in right products
suffix *= nums[i];
}
return out;
}
}
class ProductDemo {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4}, z = {-1, 1, 0, -3, 3};
System.out.println(Arrays.toString(ProductBrute.compute(a)) + " " + Arrays.toString(ProductDivision.compute(a)));
System.out.println(Arrays.toString(ProductPrefixSuffix.compute(z)) + " " + Arrays.toString(ProductOptimal.compute(z)));
// [24, 12, 8, 6] [24, 12, 8, 6] / [0, 0, 9, 0, 0] [0, 0, 9, 0, 0]
}
}Feasible → optimal
O(n²) brute force → O(n) with division (fragile) → O(n) prefix/suffix arrays → O(n) with O(1) extra space by reusing the output array.
Follow-up questions interviewers ask
Try answering each one before you open it.
Related (same pattern): range sum queries on a static array.
Precompute prefix[i + 1] = prefix[i] + a[i]; then sum(l..r) = prefix[r + 1] - prefix[l] in O(1). The 2D version (prefix[r][c] with inclusion–exclusion) answers rectangle sums in O(1). For arrays that change, use a Fenwick tree or segment tree (O(log n) update and query).
Related (same pattern): trapping rain water.
Water above i is min(maxLeft[i], maxRight[i]) - h[i]: the same prefix/suffix idea with max instead of product, then optimised to two pointers with O(1) space (see the Two Pointers category).