GeeksforGeeks Easy

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.

Constraints
  • 2 <= arr.size() <= 10⁶
  • 1 <= arr[i] <= arr.size()
  • Exactly one value in 1..n is missing and exactly one appears twice
arraymathbit-manipulation
Open on GeeksforGeeks ↗
02

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 gives R + M.

Two simple equations in two unknowns: adding them gives 2R, and M follows. Only two running totals are stored.

How to spot this pattern

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.

03

Approach

Try it first

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?

1

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.

2

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.

3

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.

4

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.

5

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def findTwoElement(self, arr):
▶3 n = len(arr)
▶4 total = squares = 0
▶5 for x in arr:
▶6 total += x
▶7 squares += x * x
▶8 r_minus_m = total - n * (n + 1) // 2
▶9 sq_diff = squares - n * (n + 1) * (2 * n + 1) // 6
▶10 r_plus_m = sq_diff // r_minus_m
▶11 repeating = (r_minus_m + r_plus_m) // 2
▶12 missing = repeating - r_minus_m
▶13 return [repeating, missing]
arr3011225334sum3array−1 to n=R − Msquares9array−1 to n=R² − M²R + MR² − M²÷R − M=R + MR(R − M+R + M)÷=repeatingMR−R − M=missingadd 3 to the sum, 9 to the squares
x3
total3
squares9
Add 3 to the running sum and 9 to the running sum of squares. Two totals are kept because there are two unknowns, the repeating number R and the missing number M, and each total will give one equation.
arr3011225334sum4array−1 to n=R − Msquares10array−1 to n=R² − M²R + MR² − M²÷R − M=R + MR(R − M+R + M)÷=repeatingMR−R − M=missingadd 1 to the sum, 1 to the squares
x1
total4
squares10
Add 1 to the running sum and 1 to the running sum of squares. Only these two numbers are stored, never a count per value.
arr3011225334sum6array−1 to n=R − Msquares14array−1 to n=R² − M²R + MR² − M²÷R − M=R + MR(R − M+R + M)÷=repeatingMR−R − M=missingadd 2 to the sum, 4 to the squares
x2
total6
squares14
Add 2 to the running sum and 4 to the running sum of squares. Only these two numbers are stored, never a count per value.
arr3011225334sum11array−1 to n=R − Msquares39array−1 to n=R² − M²R + MR² − M²÷R − M=R + MR(R − M+R + M)÷=repeatingMR−R − M=missingadd 5 to the sum, 25 to the squares
x5
total11
squares39
Add 5 to the running sum and 25 to the running sum of squares. Only these two numbers are stored, never a count per value.
arr3011225334sum14array−1 to n=R − Msquares48array−1 to n=R² − M²R + MR² − M²÷R − M=R + MR(R − M+R + M)÷=repeatingMR−R − M=missingadd 3 to the sum, 9 to the squares
x3
total14
squares48
Add 3 to the running sum and 9 to the running sum of squares. That was the last element; the totals are complete.
arr3011225334sum14array−151 to n=−1R − Msquares48array−1 to n=R² − M²R + MR² − M²÷R − M=R + MR(R − M+R + M)÷=repeatingMR−R − M=missingsum gap is −1
expected15n(n+1)/2
r_minus_m−1
If the array held 1 to 5 exactly once, its sum would be 5 × 6 / 2 = 15. The real array counts R twice and M not at all, so the gap is R − M = −1. It is negative because the repeating number is smaller than the missing one.
arr3011225334sum14array−151 to n=−1R − Msquares48array−551 to n=−7R² − M²R + MR² − M²÷R − M=R + MR(R − M+R + M)÷=repeatingMR−R − M=missingsquares gap is −7
expected55n(n+1)(2n+1)/6
sq_diff−7
The squares of 1 to 5 add up to 5 × 6 × 11 / 6 = 55. By the same counting, the gap is R² − M² = −7. On its own this does not pin down R and M, but it factors as (R − M)(R + M).
arr3011225334sum14array−151 to n=−1R − Msquares48array−551 to n=−7R² − M²R + M−7R² − M²÷−1R − M=7R + MR(R − M+R + M)÷=repeatingMR−R − M=missingdivide the gaps → repeating + missing is 7
r_plus_m7
Since R² − M² = (R − M)(R + M) and R − M is already known, dividing gives R + M = 7. R − M is never 0, because the repeating and missing numbers are different, so the division is safe and exact.
arr3011225334sum14array−151 to n=−1R − Msquares48array−551 to n=−7R² − M²R + M−7R² − M²÷−1R − M=7R + MR(−1R − M+7R + M)÷2=3repeatingMR−R − M=missingadd and halve → repeating is 3
repeating3
Adding R − M = −1 and R + M = 7 cancels M and leaves 2R = 6, so R = 3.
arr3011225334sum14array−151 to n=−1R − Msquares48array−551 to n=−7R² − M²R + M−7R² − M²÷−1R − M=7R + MR(−1R − M+7R + M)÷2=3repeatingM3R−−1R − M=4missingsubtract the sum gap → missing is 4
missing4
Then M = R − (R − M) = 3 − (−1) = 4.
arr3011225334sum14array−151 to n=−1R − Msquares48array−551 to n=−7R² − M²R + M−7R² − M²÷−1R − M=7R + MR(−1R − M+7R + M)÷2=3repeatingM3R−−1R − M=4missingrepeating 3, missing 4
result[3, 4]
Check against the array: 3 appears twice (red) and 4 does not appear at all. The answer lists the repeating number first: [3, 4].
05

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.

▶1class Solution:
▶2 def findTwoElement(self, arr):
▶3 n = len(arr)
▶4 both = 0
▶5 for i in range(n):
▶6 both ^= arr[i] ^ (i + 1)
▶7 bit = both & -both
▶8 first = second = 0
▶9 for i in range(n):
▶10 for v in (arr[i], i + 1):
▶11 if v & bit:
▶12 first ^= v
▶13 else:
▶14 second ^= v
▶15 if first in arr:
▶16 return [first, second]
▶17 return [second, first]
06

Common pitfalls

Summing squares in a 32-bit int

✗ Wrong
int squares = 0;
for (int x : arr) squares += x * x;
✓ Right
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

✗ Wrong
return [missing, repeating]
✓ Right
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.

07

Edge cases

arr = [1, 1], the repeating number is smaller than the missing one

The 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.

08

Complexity

Time
O(n)
Space
O(1)
One pass builds the two totals; everything after it is a few arithmetic operations. The largest value held is about 3.3 × 10¹⁷, which fits in a 64-bit integer.
09

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.