Find the repeating and missing number
Pattern: Cyclic sort
Values 1 to n with one repeated and one missing. Cyclic sort sends every value home. The one slot left holding the wrong value has the repeat in it, and its index gives the missing number.
The problem
You are given an array of n numbers whose values come from 1 to n. One value appears twice and one value is missing
completely. Return [repeating, missing]. For example, [3, 5, 4, 1, 1] gives [1, 2], and [1, 2, 3, 6, 7, 5, 7]
gives [7, 4].
This is takeuforward's problem. The function name findMissingRepeatingNumbers is this guide's own, and the function
may reorder the array. Use the link above for the exact statement.
Think first
- Where does value
vlive if everything is in order? - When you try to send a value home and its home already holds the same value, what have you found?
- After the swaps, how many slots can be wrong?
Hints
Brute force, and why it fails
Count every value from 1 to n with a nested loop: O(n²). A count array is O(n) time and O(n) memory. Sum and
sum-of-squares equations also give O(1) memory, with care to avoid overflow.
The key insight
Cyclic sort puts every value that has a free home into it. Only one home is never filled, the missing number's, and the leftover copy of the repeated value is the only thing that can end up there.
Dry run
nums = [3, 5, 4, 1, 1]. Value v belongs at index v - 1. At index 0 sits 3, whose home is index 2. Swap.
The plan
i = 0
while i < n:
v = nums[i]
if nums[v - 1] != v: swap nums[i], nums[v - 1]
else: i = i + 1
for j from 0 to n - 1:
if nums[j] != j + 1: return [nums[j], j + 1]
Complexity
- Time
O(n)- Space
O(1)
Each swap sends one value home for good, so at most n swaps and n moves of i.
Edge cases
n = 2:[2, 2]gives[2, 1], and[1, 1]gives[1, 2].- The repeat is the largest value or the missing one is 1: the same scan finds them.
- Large
n: no sums are computed, so nothing can overflow.
I've built my logic, show the code
Java
class Solution {
public int[] findMissingRepeatingNumbers(int[] nums) {
int n = nums.length;
int i = 0;
while (i < n) {
int v = nums[i];
if (nums[v - 1] != v) { // v belongs at index v - 1; send it there
nums[i] = nums[v - 1];
nums[v - 1] = v;
} else {
i++; // home already, or its home holds another copy
}
}
for (int j = 0; j < n; j++) {
if (nums[j] != j + 1) {
return new int[]{nums[j], j + 1}; // the extra copy sits in the missing number's slot
}
}
return new int[]{-1, -1}; // not reached for valid input
}
}C++
class Solution {
public:
vector<int> findMissingRepeatingNumbers(vector<int>& nums) {
int n = static_cast<int>(nums.size());
int i = 0;
while (i < n) {
int v = nums[i];
if (nums[v - 1] != v) { // v belongs at index v - 1; send it there
swap(nums[i], nums[v - 1]);
} else {
i++; // home already, or its home holds another copy
}
}
for (int j = 0; j < n; j++) {
if (nums[j] != j + 1) {
return {nums[j], j + 1}; // the extra copy sits in the missing number's slot
}
}
return {-1, -1}; // not reached for valid input
}
};Python
class Solution:
def findMissingRepeatingNumbers(self, nums: list[int]) -> list[int]:
n = len(nums)
i = 0
while i < n:
v = nums[i]
if nums[v - 1] != v: # v belongs at index v - 1; send it there
nums[i], nums[v - 1] = nums[v - 1], v
else:
i += 1 # home already, or its home holds another copy
for j in range(n):
if nums[j] != j + 1:
return [nums[j], j + 1] # the extra copy sits in the missing number's slot
return [-1, -1] # not reached for valid inputCommon mistakes
- Testing
i != nums[i] - 1instead ofnums[nums[i] - 1] != nums[i], which swaps two copies of the repeat forever. - Forgetting the shift: values 1 to
nlive at 0 ton - 1. - Returning the two numbers in the wrong order. This guide wants
[repeating, missing].