Find Missing and Repeating Number
Find Missing and Repeating Number is a GFG problem (Easy). An unsorted array arr of size n should hold every number from 1 to n once, but one number appears twice and another is missing. Return [repeating, missing], with the repeating number first.
With n up to 10⁶ the solution must be O(n), and the usual follow-up asks for O(1) extra space, which rules out a count per value.
- 2 <= arr.size() <= 10⁶
- 1 <= arr[i] <= arr.size()
- Exactly one value in 1..n is missing and exactly one appears twice
Intuition
Call the repeating number R and the missing number M. Compared with the list 1 to n, the array has one extra R and one M too few. So any total taken over the array differs from the same total over 1 to n by R's share minus M's share.
- Plain sum: the gap is
R − M. - Sum of squares: the gap is
R² − M², which factors as(R − M)(R + M). Dividing by the first gap givesR + M.
Two simple equations in two unknowns: adding them gives 2R, and M follows. Only two running totals are stored.
This problem is one case of a wider trick. When a few values are unknown and the rest of the data is fully predictable, compare totals: each different total (sum, sum of squares, XOR) gives one equation. Missing Number uses one total for one unknown; this problem needs two.
Approach
Before reading on: for arr = [3, 1, 2, 5, 3], compute the array's sum and the sum of 1 to 5. Which pairs (R, M) fit that gap? What second total would tell those pairs apart?
Two ways to solve it
Two totals give R − M and R + M; add and halve.
- One pass: two running totals.
- Short: six lines of arithmetic.
- Watch out: needs 64-bit totals in C++ and Java.
The usual interview answer.
XOR of the array and 1 to n leaves R XOR M; one set bit splits everything into two groups that isolate R and M.
- No large numbers: nothing can overflow.
- Extra pass: a final scan tells which of the two is the repeat.
Pick it when overflow is a worry.
Both are O(n) time and O(1) space. The sums are shorter to write and explain, so the steps, code and live demo follow them, and the XOR code comes after the demo.
Total the array and its squares
Walk the array once, adding each x to total and x * x to squares. There are two unknowns, so two different totals are needed; one sum alone fits many pairs.
Compare the sum with 1 to n
The numbers 1 to n add up to n(n + 1) / 2. Subtract it from total to get R − M: R was counted one time too many and M one time too few, and every other number cancels.
Compare the squares with 1 to n
The squares of 1 to n add up to n(n + 1)(2n + 1) / 6. Subtracting it from squares leaves R² − M², for the same reason as the plain sum.
Divide to get R + M
Since R² − M² = (R − M)(R + M), divide the squares gap by the sum gap. The division is exact, and R − M is never 0 because the repeating and missing numbers differ.
Solve the two equations
Adding R − M and R + M gives 2R, so R = ((R − M) + (R + M)) / 2, and then M = R − (R − M). Both results are whole numbers, because their sum 2R is even.
Code in Python, C++ and Java
XOR and one split bit
XOR every element and every number from 1 to n together. Each number that appears once in both cancels, R appears three times in total and M once, so what remains is R ^ M. That value is not 0, so it has a set bit, and R and M differ at that bit. Splitting all the numbers by that bit and XOR-ing each group leaves R in one group and M in the other. One more scan of the array shows which of the two actually occurs, and that one is the repeat.
Common pitfalls
Summing squares in a 32-bit int
int squares = 0; for (int x : arr) squares += x * x;
long long squares = 0; for (int x : arr) squares += 1LL * x * x;
The squares of 1 to n pass 2³¹ once n is about 1,900, and at n = 10⁶ they reach about 3.3 × 10¹⁷. The int total wraps and the answer is garbage. Cast before multiplying, and keep n 64-bit too, since n(n + 1)(2n + 1) is about 2 × 10¹⁸ before the division by 6. Python integers never overflow.
Returning the pair in the wrong order
return [missing, repeating]
return [repeating, missing]
For arr = [2, 2] the judge expects [2, 1]: 2 repeats and 1 is missing. Swapping them prints [1, 2], which is marked wrong even though both numbers were found.
Edge cases
arr = [1, 1], the repeating number is smaller than the missing oneThe sum gap is 2 − 3, so R − M = −1; the squares gap is 2 − 5, so R² − M² = −3. Then R + M = 3, R = 1 and M = 2. A negative gap needs no special handling; the signs cancel in the division.
Complexity
Find Missing and Repeating Number FAQ
Is a frequency array or hash set acceptable?
It is correct and O(n) time: count each value, then the value with count 2 repeats and the one with count 0 is missing. It costs O(n) extra space, which the follow-up rules out. A middle option marks values in place by negating arr[x - 1]; a slot that is already negative reveals the repeat, and the one slot left positive reveals the missing number. That uses O(1) extra space but changes the input.
Is this the same as Set Mismatch?
Yes. Set Mismatch (LeetCode 645) asks for the duplicate and the missing number of 1 to n in the same order, [repeating, missing], so this code solves it after renaming the method. Find Missing and Repeated Values (LeetCode 2965) puts the numbers 1 to n² in a grid; flatten the grid first.