Java Array Program · A16
Find the Missing Number in an Array in Java
The numbers 1 to n add up to n(n + 1) / 2. Subtract the actual sum of the array, and what is left is the missing number.
Problem Statement
An array contains n - 1 distinct numbers from the range 1 to n, in any order, with exactly one number missing. Return the missing number.
The array has length n - 1, so n is one more than its length.
Input/Output Examples
[3, 7, 1, 2, 8, 4, 5]Output6Explanation: n = 8; the numbers 1 to 8 sum to 36, and the array sums to 30.[2]Output1Explanation: n = 2; 1 + 2 = 3, and 3 - 2 = 1.[1]Output2Explanation: n = 2; 3 - 1 = 2.Visual Explanation
- Expected1..8368 × 9 / 2
- Actualarray sum303+7+1+2+8+4+5
- Difference36 − 306
[3, 7, 1, 2, 8, 4, 5]: the full range 1 to 8 should add up to 36, but the array adds up to 30, so 6 is missing.Because exactly one number is missing, the difference between the expected and the actual sum can only be that number. Using a long keeps the expected sum from overflowing for large n.
Approaches
Both approaches use constant extra memory. One uses arithmetic; the other uses XOR, which cannot overflow.
Approach 1: Sum Formula (Recommended)
Idea: compute n(n + 1) / 2 as a long and subtract the sum of the array.
- Set
n = arr.length + 1. - Compute the expected sum as a
long. - Subtract the actual sum and return the difference.
Time: O(n) · Space: O(1).
Approach 2: XOR Trick
Idea: XOR all numbers from 1 to n and all array elements; every present number cancels itself out.
- XOR the numbers 1 to
nintox. - XOR every array element into
x. - Return
x, which is the missing number.
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: Sum Formula (Recommended)
Easy to explain. The long matters: for n around 100,000 the sum already exceeds the int range.
import java.util.Arrays;
public class MissingSum {
// The numbers 1..n add up to n(n+1)/2; the missing one is the difference
static int missing(int[] arr) {
long n = arr.length + 1;
long expected = n * (n + 1) / 2; // long avoids overflow for large n
long actual = 0;
for (int v : arr) {
actual += v;
}
return (int) (expected - actual);
}
public static void main(String[] args) {
int[][] inputs = {{3, 7, 1, 2, 8, 4, 5}, {2}, {1}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + missing(arr));
}
}
}
[3, 7, 1, 2, 8, 4, 5] -> 6 [2] -> 1 [1] -> 2
Time complexity: O(n) · Space complexity: O(1).
Approach 2: XOR Trick
a ^ a = 0 and a ^ 0 = a, so pairs cancel and only the missing value remains. No overflow is possible.
import java.util.Arrays;
public class MissingXor {
// x ^ x = 0, so XOR-ing 1..n with every element leaves only the missing number
static int missing(int[] arr) {
int n = arr.length + 1;
int x = 0;
for (int i = 1; i <= n; i++) {
x ^= i;
}
for (int v : arr) {
x ^= v;
}
return x;
}
public static void main(String[] args) {
int[][] inputs = {{3, 7, 1, 2, 8, 4, 5}, {2}, {1}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + missing(arr));
}
}
}
[3, 7, 1, 2, 8, 4, 5] -> 6 [2] -> 1 [1] -> 2
Time complexity: O(n) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Sum Formula | O(n) | O(1) |
| XOR Trick | O(n) | O(1) |
Here n is the size of the range. Both approaches read the array once and keep one or two numbers.
Interview Tips and Recommended Approach
- Watch for overflow.
n * (n + 1) / 2inintoverflows long before memory runs out. - Explain XOR cancellation; it is a common follow-up question.
- Check the assumptions. These methods need exactly one missing number and no duplicates.
- Know the related problems: two missing numbers, or the first missing positive in an arbitrary array.
Recommended Approach: Sum Formula
Why: it is a one-line idea with constant memory, easy to prove correct, and the long removes the overflow risk.