Java Array Program · A36
Product of Array Except Self in Java
The product of everything except arr[i] is the product of everything to its left times the product of everything to its right. Two passes compute both.
Problem Statement
Given an array of integers, return an array where position i holds the product of every element except arr[i].
Do not use division, which fails when the array contains zeros. Assume the products fit in an int.
Input/Output Examples
[2, 3, 4, 5]Output[60, 40, 30, 24]Explanation: for example 3 × 4 × 5 = 60.[3, 0, 4]Output[0, 12, 0]Explanation: only the zero's position gets a non-zero product.[0, 5, 0]Output[0, 0, 0]Explanation: every product includes at least one zero.Visual Explanation
| index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| value | 2 | 3 | 4 | 5 |
| left product | 1 | 2 | 6 | 24 |
| right product | 60 | 20 | 5 | 1 |
| result | 60 | 40 | 30 | 24 |
[2, 3, 4, 5]: the left product at each index excludes the element itself, and so does the right product. Their product is the answer (purple).The first pass writes the left products into the result array. The second pass walks backwards with a single running right product and multiplies it in, so no second array is needed.
Approaches
The prefix-suffix method is linear without division. The brute force multiplies all other elements for every position.
Approach 1: Prefix and Suffix Products (Recommended)
Idea: store the left products in the result, then multiply in the right products with one running variable.
- Left to right: set
result[i] = left, then multiplyleftbyarr[i]. - Right to left: multiply
result[i]byright, then multiplyrightbyarr[i]. - Return
result.
Time: O(n) · Space: O(1). Not counting the output array.
Approach 2: Multiply All Others
Idea: for each index, multiply every other element.
- For each
i, start withp = 1. - Multiply in every
arr[j]withj ≠ i. - Store
p.
Time: O(n²) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Prefix and Suffix Products (Recommended)
Zeros need no special handling, because no division is involved.
import java.util.Arrays;
public class ProductExceptPrefix {
// result[i] = (product of everything left of i) x (product of everything right of i)
static int[] productExceptSelf(int[] arr) {
int n = arr.length;
int[] result = new int[n];
int left = 1;
for (int i = 0; i < n; i++) {
result[i] = left; // product of arr[0..i-1]
left *= arr[i];
}
int right = 1;
for (int i = n - 1; i >= 0; i--) {
result[i] *= right; // times product of arr[i+1..n-1]
right *= arr[i];
}
return result;
}
public static void main(String[] args) {
int[][] inputs = {{2, 3, 4, 5}, {3, 0, 4}, {0, 5, 0}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + Arrays.toString(productExceptSelf(arr)));
}
}
}
[2, 3, 4, 5] -> [60, 40, 30, 24] [3, 0, 4] -> [0, 12, 0] [0, 5, 0] -> [0, 0, 0]
Time complexity: O(n) · Space complexity: O(1).
Approach 2: Multiply All Others
Simple and correct; used here to check the linear version.
import java.util.Arrays;
public class ProductExceptBrute {
// Multiplies all other elements for every position: O(n^2), no division
static int[] productExceptSelf(int[] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
int p = 1;
for (int j = 0; j < arr.length; j++) {
if (j != i) {
p *= arr[j];
}
}
result[i] = p;
}
return result;
}
public static void main(String[] args) {
int[][] inputs = {{2, 3, 4, 5}, {3, 0, 4}, {0, 5, 0}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + Arrays.toString(productExceptSelf(arr)));
}
}
}
[2, 3, 4, 5] -> [60, 40, 30, 24] [3, 0, 4] -> [0, 12, 0] [0, 5, 0] -> [0, 0, 0]
Time complexity: O(n²) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Prefix and Suffix Products | O(n) | O(1) |
| Multiply All Others | O(n²) | O(1) |
Here n is the number of elements. The main approach makes two passes and uses only the output array; the brute force multiplies n - 1 values for each position.
Interview Tips and Recommended Approach
- Explain why division is avoided. One zero makes "total / arr[i]" undefined at that position.
- Reuse the output array. Storing the left products there gives
O(1)extra space. - Watch for overflow. Use
longif the products can be large. - Recognise the pattern. Prefix and suffix passes solve many "everything except i" questions.
Recommended Approach: Prefix and Suffix Products
Why: it runs in linear time without division, handles zeros naturally and uses no memory beyond the output.