Find Two Missing Numbers | Set 2 (XOR based solution)
Last Updated :
03 May, 2021
Given an array of n unique integers where each element in the array is in range [1, n]. The array has all distinct elements and size of an array is (n-2). Hence Two numbers from the range are missing from this array. Find the two missing numbers.
Examples:
Input : arr[] = {1, 3, 5, 6}, n = 6
Output : 2 4
Input : arr[] = {1, 2, 4}, n = 5
Output : 3 5
Input : arr[] = {1, 2}, n = 4
Output : 3 4
Find Two Missing Numbers | Set 1 (An Interesting Linear Time Solution)
We have discussed two methods to solve this problem in above article. The method 1 requires O(n) extra space and method 2 can causes overflow. In this post, a new solution is discussed. The solution discussed here is O(n) time, O(1) extra space and causes no overflow.
Below are steps.
- Find XOR of all array elements and natural numbers from 1 to n. Let the array be arr[] = {1, 3, 5, 6}
XOR = (1 ^ 3 ^ 5 ^ 6) ^ (1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6)
- As per the property of XOR, same elements will cancel out and we will be left with 2 XOR 4 = 6 (110). But we don’t know the exact numbers,let them be X and Y.
- A bit is set in xor only if corresponding bits in X and Y are different. This is the crucial step to understand.
- We take a set bit in XOR. Let us consider the rightmost set bit in XOR, set_bit_no = 010
- Now again if we XOR all the elements of arr[] and 1 to n that have rightmost bit set we will get one of the repeating numbers, say x.
Ex: Elements in arr[] with bit set: {3, 6}
Elements from 1 to n with bit set {2, 3, 6}
Result of XOR'ing all these is x = 2.
- Similarly, if we XOR all the elements of arr[] and 1 to n that have rightmost bit not set, we will get the other element, say y.
Ex: Elements in arr[] with bit not set: {1, 5}
Elements from 1 to n with bit not set {1, 4, 5}
Result of XOR'ing all these is y = 4
Below is the implementation of above steps.
C++
#include<bits/stdc++.h>
void findTwoMissingNumbers( int arr[], int n)
{
int XOR = arr[0];
for ( int i = 1; i < n-2; i++)
XOR ^= arr[i];
for ( int i = 1; i <= n; i++)
XOR ^= i;
int set_bit_no = XOR & ~(XOR-1);
int x = 0, y = 0;
for ( int i = 0; i < n-2; i++)
{
if (arr[i] & set_bit_no)
x = x ^ arr[i];
else
y = y ^ arr[i];
}
for ( int i = 1; i <= n; i++)
{
if (i & set_bit_no)
x = x ^ i;
else
y = y ^ i;
}
printf ( "Two Missing Numbers are\n %d %d" , x, y);
}
int main()
{
int arr[] = {1, 3, 5, 6};
int n = 2 + sizeof (arr)/ sizeof (arr[0]);
findTwoMissingNumbers(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void findTwoMissingNumbers( int arr[], int n)
{
int XOR = arr[ 0 ];
for ( int i = 1 ; i < n- 2 ; i++)
XOR ^= arr[i];
for ( int i = 1 ; i <= n; i++)
XOR ^= i;
int set_bit_no = XOR & ~(XOR- 1 );
int x = 0 , y = 0 ;
for ( int i = 0 ; i < n- 2 ; i++)
{
if ((arr[i] & set_bit_no) > 0 )
x = x ^ arr[i];
else
y = y ^ arr[i];
}
for ( int i = 1 ; i <= n; i++)
{
if ((i & set_bit_no)> 0 )
x = x ^ i;
else
y = y ^ i;
}
System.out.println( "Two Missing Numbers are " );
System.out.println( x + " " + y);
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 5 , 6 };
int n = 2 +arr.length;
findTwoMissingNumbers(arr, n);
}
}
|
Python3
def findTwoMissingNumbers(arr, n):
XOR = arr[ 0 ]
for i in range ( 1 ,n - 2 ):
XOR ^ = arr[i]
for i in range ( 1 ,n + 1 ):
XOR ^ = i
set_bit_no = XOR & ~(XOR - 1 )
x = 0
y = 0
for i in range ( 0 ,n - 2 ):
if arr[i] & set_bit_no:
x = x ^ arr[i]
else :
y = y ^ arr[i]
for i in range ( 1 ,n + 1 ):
if i & set_bit_no:
x = x ^ i
else :
y = y ^ i
print ( "Two Missing Numbers are\n%d %d" % (x,y))
arr = [ 1 , 3 , 5 , 6 ]
n = 2 + len (arr)
findTwoMissingNumbers(arr, n)
|
C#
using System;
class GFG {
static void findTwoMissingNumbers( int [] arr, int n)
{
int XOR = arr[0];
for ( int i = 1; i < n - 2; i++)
XOR ^= arr[i];
for ( int i = 1; i <= n; i++)
XOR ^= i;
int set_bit_no = XOR & ~(XOR - 1);
int x = 0, y = 0;
for ( int i = 0; i < n - 2; i++) {
if ((arr[i] & set_bit_no) > 0)
x = x ^ arr[i];
else
y = y ^ arr[i];
}
for ( int i = 1; i <= n; i++) {
if ((i & set_bit_no) > 0)
x = x ^ i;
else
y = y ^ i;
}
Console.WriteLine( "Two Missing Numbers are " );
Console.WriteLine(x + " " + y);
}
public static void Main()
{
int [] arr = { 1, 3, 5, 6 };
int n = 2 + arr.Length;
findTwoMissingNumbers(arr, n);
}
}
|
PHP
<?php
function findTwoMissingNumbers( $arr , $n )
{
$XOR = $arr [0];
for ( $i = 1; $i < $n - 2; $i ++)
$XOR ^= $arr [ $i ];
for ( $i = 1; $i <= $n ; $i ++)
$XOR ^= $i ;
$set_bit_no = $XOR & ~( $XOR - 1);
$x = 0;
$y = 0;
for ( $i = 0; $i < $n - 2; $i ++)
{
if ( $arr [ $i ] & $set_bit_no )
$x = $x ^ $arr [ $i ];
else
$y = $y ^ $arr [ $i ];
}
for ( $i = 1; $i <= $n ; $i ++)
{
if ( $i & $set_bit_no )
$x = $x ^ $i ;
else
$y = $y ^ $i ;
}
echo "Two Missing Numbers are\n" , $x ;
echo "\n" , $y ;
}
$arr = array (1, 3, 5, 6);
$n = 2 + count ( $arr );
findTwoMissingNumbers( $arr , $n );
?>
|
Javascript
<script>
function findTwoMissingNumbers(arr, n)
{
let XOR = arr[0];
for (let i = 1; i < n-2; i++)
XOR ^= arr[i];
for (let i = 1; i <= n; i++)
XOR ^= i;
let set_bit_no = XOR & ~(XOR-1);
let x = 0, y = 0;
for (let i = 0; i < n-2; i++)
{
if (arr[i] & set_bit_no)
x = x ^ arr[i];
else
y = y ^ arr[i];
}
for (let i = 1; i <= n; i++)
{
if (i & set_bit_no)
x = x ^ i;
else
y = y ^ i;
}
document.write(`Two Missing Numbers are<br> ${x} ${y}`);
}
let arr = [1, 3, 5, 6];
n = 2 + arr.length;
findTwoMissingNumbers(arr, n);
</script>
|
Output:
Two Missing Numbers are
2 4
Time Complexity : O(n)
Auxiliary Space : O(1)
No integer overflow
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...