


Consider an array of size six. The elements of the array are { 6, 4, 3, 5, 5, 1 }.
The array should contain elements from one to six. Here, 2 is not present and 5 is occurring twice. Thus, 2 is the missing number (M) and 5 is the repeating number (R).
Can you do this in linear time and constant additional space?
The first line contains an integer ‘N’ denoting the size of the array.
The second line contains ‘N’ space-separated integers.
Print the missing number (M) and the repeating number (R) separated by a single space.
You don’t have to print anything, it has already been taken care of. Just implement the function.
You have to return a pair whose first element is the missing number ‘M’ and the second element is the repeating number ‘R’.
2 <= N <= 5 * 10^4
1 <= data <= N
Where ‘N’ is the size of the array and ‘data’ denotes the value of the elements of the array.
For finding the repeating number,
For finding the missing number,
Missing number ‘M’ = N*(N+1)/2 - ( sum(givenArray) - repeating number ).
Where N * (N + 1) / 2 will give the sum of the numbers in the range [1, N].
For Example: Consider an array { 1, 2, 5, 2, 3 }.
At index 0:
We will check whether 1 (arr[0]) is present in the remaining array [2, 5, 2, 3] or not. Since 1 is present in the remaining array, we will move to the next number.
At index 1:
We will check whether 2 (arr[1]) is present in the remaining array [5, 2, 3] or not.
Since 2 is present in the remaining array, it is our repeating number.
Thus, 2 is our repeating element ‘R’.
Now, for finding the missing element, we will use the below formula.
M = N*(N+1)/2 + R - sum(givenArray)
M = 15 + 2 - 13
M = 4.
Thus, our missing number is 4 and the repeating number is 2.
Missing number ‘M’ = N*(N+1)/2 - ( sum(givenArray) - repeating number ).
Where N * (N + 1) / 2 will give the sum of the numbers in the range [1, N].
For Example: Consider an array { 1, 5, 2, 2, 3 }.
After sorting the array, the array will become { 1, 2, 2, 3, 5 }.
When we traverse this array, we will see that the element at index one and index two is 2. Thus, 2 is our repeating element ‘R’.
Now, for finding the missing element, we will use the below formula.
M = N*(N+1)/2 + R - sum(givenArray)
M = 15 + 2 - 13
M = 4.
Thus, our missing number is 4 and the repeating number is 2.
The idea is to traverse the array and mark the visited elements.
While traversing the array, we will use the absolute value of every element as an index and make the value at this index as negative to mark it visited. For example, for element 3, we will make the value at index 2 as negative ( since the array is 0-indexed ). For any element in the array, if the element at the index {element - 1} is already marked negative, then this is the repeating element.
To find the missing number, we will traverse the array again and look for a positive value. The index at which we find the positive value is our missing number because that index is not present in the array as an element.
For Example: Consider the array Arr = { 1, 5, 2, 2, 3 }.
Now we will traverse the array and mark the visited numbers as follows:
Current array Arr: {-1, 5, 2, 2, 3}.
Current array Arr: {-1, 5, 2, 2, -3}.
Current array Arr: {-1, -5, 2, 2, -3}.
Here, the element at index 1 (2 - 1), is already negative. It means we have already visited it. Thus, we have found our repeating number ‘R’ which is 2.
Current array Arr: {-1, -5, 2, 2, -3}.
Current array Arr: {-1, -5, -2, 2, -3}.
So, our missing number ‘M’ is 4 and the repeating number ‘R’ is 2.
We know that XOR of a number with itself is 0.
Now, let's start with the algorithm.
Now, XOR the result with all numbers from 1 to n.
findXOR = (findXOR)^1^2^…..^n.
For example:
If the given array is [1, 3, 5, 3, 4]
Then, findXOR = [1 ^ 2 ^ 3 ^ 4 ^ 5] ^ [ 1 ^ 3 ^ 5 ^ 3 ^ 4] = 1
Notice that, 2 is our missing number and 3 is our repeating number in the given array and 2^3 is 1.
Now, the first bit is set in 1.
We will traverse the array and find all the elements whose this bit is set and take their XOR and store it in A. Also, we will take the XOR of all the elements in the array and from 1 to n whose this bit is not set and store it in B.
A = [ 1 ^ 3 ^ 3 ^ 5 ] ^ [ 1 ^ 3 ^ 5] = 3
B = [ 4 ] ^ [ 2 ^ 4] = 2
As 3 is present in the array, this cant be our missing number.
Thus, 2 is our missing number and 3 is our repeating number.