Problem 1:
There are n+2 elements, which are in the range of 1 to n. All are unique, except two numbers. Find the two repeated numbers.
eg: {4, 2, 4, 5, 2, 3, 1}
output: 2, 4
Algorithm:
Given array: int[] ar
Find XOR of all elements in input array and all numbers from 1 to n:
XOR = ar[0] ^ ar[1] ^ ... ar[n+1] ^ ar[n+2] ^ 0 ^ 1 ... ^ n
Now Get the right most set bit in this XOR Result
right_most_set_bit = XOR & (~(XOR-1))
One of the missing number will be "XOR of all array elements and all numbers from 1 to n, which are having the righ_most_set_bit set in them"
Other missing number will be "XOR of all array elements and all numbers from 1 to n, which are not having the righ_most_set_bit set in them"
Program:
public class FindRepeatedNos {
static public void main(String[] args) {
int[] ar = {4, 2, 4, 5, 2, 3, 1};
int n = 5;
int x = 0, y = 0;
int xor = 0;
for(int i=0; i<ar.length; i++)
xor = xor ^ ar[i];
for(int i=1; i<=n; i++)
xor = xor ^ i;
//Get the right most digit
int right_most_bit = xor & (~(xor-1));
for(int i=0; i<ar.length; i++) {
if((ar[i] & right_most_bit) == 0)
x = x ^ ar[i];
else
y = y ^ ar[i];
}
for(int i=1; i<=n; i++) {
if((i & right_most_bit) == 0)
x = x ^ i;
else
y = y ^ i;
}
System.out.println(x);
System.out.println(y);
}
}
eg: {2, 4, 7, 9, 2, 4}
output: 7, 9
Algorithm:
It is similar to above algorithm, except that we dont have 1 to n numbers here. So only one for loop for all xor operations.
Program:
public class FindNonRepeatedNos {
static public void main(String[] args) {
int[] ar = {2, 4, 7, 9, 2, 4};
int x = 0, y = 0, xor = 0;
for(int i=0; i<ar.length; i++)
xor = xor ^ ar[i];
int right_most_set = xor & (~(xor-1));
for(int i=0; i<ar.length; i++) {
if((ar[i] & right_most_set) == 0)
x = x ^ ar[i];
else
y = y ^ ar[i];
}
System.out.println(x);
System.out.println(y);
}
}
There are n+2 elements, which are in the range of 1 to n. All are unique, except two numbers. Find the two repeated numbers.
eg: {4, 2, 4, 5, 2, 3, 1}
output: 2, 4
Algorithm:
Given array: int[] ar
Find XOR of all elements in input array and all numbers from 1 to n:
XOR = ar[0] ^ ar[1] ^ ... ar[n+1] ^ ar[n+2] ^ 0 ^ 1 ... ^ n
Now Get the right most set bit in this XOR Result
right_most_set_bit = XOR & (~(XOR-1))
One of the missing number will be "XOR of all array elements and all numbers from 1 to n, which are having the righ_most_set_bit set in them"
Other missing number will be "XOR of all array elements and all numbers from 1 to n, which are not having the righ_most_set_bit set in them"
Program:
public class FindRepeatedNos {
static public void main(String[] args) {
int[] ar = {4, 2, 4, 5, 2, 3, 1};
int n = 5;
int x = 0, y = 0;
int xor = 0;
for(int i=0; i<ar.length; i++)
xor = xor ^ ar[i];
for(int i=1; i<=n; i++)
xor = xor ^ i;
//Get the right most digit
int right_most_bit = xor & (~(xor-1));
for(int i=0; i<ar.length; i++) {
if((ar[i] & right_most_bit) == 0)
x = x ^ ar[i];
else
y = y ^ ar[i];
}
for(int i=1; i<=n; i++) {
if((i & right_most_bit) == 0)
x = x ^ i;
else
y = y ^ i;
}
System.out.println(x);
System.out.println(y);
}
}
Problem 2:
Given an array containing n elements. All the elements are repeated twice, except two numbers, which occurred only once. Find those two numbers. No range specified for input elements.eg: {2, 4, 7, 9, 2, 4}
output: 7, 9
Algorithm:
It is similar to above algorithm, except that we dont have 1 to n numbers here. So only one for loop for all xor operations.
Program:
public class FindNonRepeatedNos {
static public void main(String[] args) {
int[] ar = {2, 4, 7, 9, 2, 4};
int x = 0, y = 0, xor = 0;
for(int i=0; i<ar.length; i++)
xor = xor ^ ar[i];
int right_most_set = xor & (~(xor-1));
for(int i=0; i<ar.length; i++) {
if((ar[i] & right_most_set) == 0)
x = x ^ ar[i];
else
y = y ^ ar[i];
}
System.out.println(x);
System.out.println(y);
}
}