SuYo
BAN USERI am going to try explain this more clearly.
First how xor works: if you have two numbers say 10 (1010) and 8 (1000), xor operation ( done using operator ^ ) will return the bits which are set in one number but not in the other.
10 -- 1010
08 -- 1000
---------------
^ -- 0010
---------------
Here the third bit is set(from right) in 10 but not in 8;
Now the problem states that there are 2n + 2 numbers such that n numbers are repeated.
eg. for n = 3: { 2, 3, 2, 4, 5, 4, 5, 6 }
there are two of ( 2, 4 & 5 ) but only one of ( 3 & 6 )
Lets take the first set of number that repeats..
when we xor a number with itself the result is zero.
2 ^ 2 = 0 (in binary: 10 ^ 10 )
4 ^ 4 = 0
5 ^ 5 = 0
Now we have actually got rid of the numbers that repeats twice.
so when we xor all the elements of the array : a[i] ^ a[i+1] ^ ... a[n], we would have performed, for the above example ( 2 ^ 3 ^ 2 ^ 4 ^ 5 ^ 4 ^ 5 ^ 6 )
Since the repeating elements are nullified :) we are left with ( 3 ^ 6 ), the xor of the two elements we need to find. If the elements are equal, the 2n+2 constraint is not valid and hence these two numbers would have also been nullified.
Now all we have to do is extract the two numbers (x, y) from ( 3 ^ 6 )
3 ^ 6 is
011
110
-----
101
-----
what we can infer from the above is that the last bit is set in one number and not in the other. ( and so does the first bit)
set_bit_no = xor & ~(xor-1);
This line in the above example by Rajat Rastogi can be replaced with
int set_bit_no = 1;
while ( ! ( xor & set_bit_no ) )
set_bit_no <<=1;
what it does is right shift, a bit at a time and check whether it is set in the result of ( 3 ^ 6 )
Why do we need this?
Because, when doing a xor, a bit is set only when it is set in one and not in the other. So we are actually isolating a trait of one number so that we can extract it from the result.
Now is the tricky part.
for(i = 0; i < n; i++)
{
if(arr[i] & set_bit_no)
x = x ^ arr[i]; /*XOR of first set */
else
y = y ^ arr[i]; /*XOR of second set*/
}
Our example array: { 2, 3, 2, 4, 5, 4, 5, 6 }
x & y is initially zero;
Again we apply the same principle, but this time we check whether the rightmost bit in set in one each element of the array..if its set, it will be removed when a pair of the same item is encountered and thus we will be able to extract x;
Then we also xor the elems which does not have the right most bit set. So, we will be extracting all the pairs with this bit not set and y ( the pairs cancel each other and y remains )
I am going to try explain this more clearly.
First how xor works: if you have two numbers say 10 (1010) and 8 (1000), xor operation ( done using operator ^ ) will return the bits which are set in one number but not in the other.
10 -- 1010
08 -- 1000
---------------
^ -- 0010
---------------
Here the third bit is set(from right) in 10 but not in 8;
Now the problem states that there are 2n + 2 numbers such that n numbers are repeated.
eg. for n = 3: { 2, 3, 2, 4, 5, 4, 5, 6 }
there are two of ( 2, 4 & 5 ) but only one of ( 3 & 6 )
Lets take the first set of number that repeats..
when we xor a number with itself the result is zero.
2 ^ 2 = 0 (in binary: 10 ^ 10 )
4 ^ 4 = 0
5 ^ 5 = 0
Now we have actually got rid of the numbers that repeats twice.
so when we xor all the elements of the array : a[i] ^ a[i+1] ^ ... a[n], we would have performed, for the above example ( 2 ^ 3 ^ 2 ^ 4 ^ 5 ^ 4 ^ 5 ^ 6 )
Since the repeating elements are nullified :) we are left with ( 3 ^ 6 ), the xor of the two elements we need to find. If the elements are equal, the 2n+2 constraint is not valid and hence these two numbers would have also been nullified.
Now all we have to do is extract the two numbers (x, y) from ( 3 ^ 6 )
3 ^ 6 is
011
110
-----
101
-----
what we can infer from the above is that the last bit is set in one number and not in the other. ( and so does the first bit)
set_bit_no = xor & ~(xor-1);
This line in the above example by Rajat Rastogi can be replaced with
int set_bit_no = 1;
while ( ! ( xor & set_bit_no ) )
set_bit_no <<=1;
what it does is right shift, a bit at a time and check whether it is set in the result of ( 3 ^ 6 )
Why do we need this?
Because, when doing a xor, a bit is set only when it is set in one and not in the other. So we are actually isolating a trait of one number so that we can extract it from the result.
Now is the tricky part.
for(i = 0; i < n; i++)
{
if(arr[i] & set_bit_no)
x = x ^ arr[i]; /*XOR of first set */
else
y = y ^ arr[i]; /*XOR of second set*/
}
Our example array: { 2, 3, 2, 4, 5, 4, 5, 6 }
x & y is initially zero;
Again we apply the same principle, but this time we check whether the rightmost bit in set in one each element of the array..if its set, it will be removed when a pair of the same item is encountered and thus we will be able to extract x;
Then we also xor the elems which does not have the right most bit set. So, we will be extracting all the pairs with this bit not set and y ( the pairs cancel each other and y remains )
I am going to try explain this more clearly.
First how xor works: if you have two numbers say 10 (1010) and 8 (1000), xor operation ( done using operator ^ ) will return the bits which are set in one number but not in the other.
10 -- 1010
08 -- 1000
---------------
^ -- 0010
---------------
Here the third bit is set(from right) in 10 but not in 8;
Now the problem states that there are 2n + 2 numbers such that n numbers are repeated.
eg. for n = 3: { 2, 3, 2, 4, 5, 4, 5, 6 }
there are two of ( 2, 4 & 5 ) but only one of ( 3 & 6 )
Lets take the first set of number that repeats..
when we xor a number with itself the result is zero.
2 ^ 2 = 0 (in binary: 10 ^ 10 )
4 ^ 4 = 0
5 ^ 5 = 0
Now we have actually got rid of the numbers that repeats twice.
so when we xor all the elements of the array : a[i] ^ a[i+1] ^ ... a[n], we would have performed, for the above example ( 2 ^ 3 ^ 2 ^ 4 ^ 5 ^ 4 ^ 5 ^ 6 )
Since the repeating elements are nullified :) we are left with ( 3 ^ 6 ), the xor of the two elements we need to find. If the elements are equal, the 2n+2 constraint is not valid and hence these two numbers would have also been nullified.
Now all we have to do is extract the two numbers (x, y) from ( 3 ^ 6 )
3 ^ 6 is
011
110
-----
101
-----
what we can infer from the above is that the last bit is set in one number and not in the other. ( and so does the first bit)
set_bit_no = xor & ~(xor-1);
This line in the above example by Rajat Rastogi can be replaced with
int set_bit_no = 1;
while ( ! ( xor & set_bit_no ) )
set_bit_no <<=1;
what it does is right shift, a bit at a time and check whether it is set in the result of ( 3 ^ 6 )
Why do we need this?
Because, when doing a xor, a bit is set only when it is set in one and not in the other. So we are actually isolating a trait of one number so that we can extract it from the result.
Now is the tricky part.
for(i = 0; i < n; i++)
{
if(arr[i] & set_bit_no)
x = x ^ arr[i]; /*XOR of first set */
else
y = y ^ arr[i]; /*XOR of second set*/
}
Our example array: { 2, 3, 2, 4, 5, 4, 5, 6 }
x & y is initially zero;
Again we apply the same principle, but this time we check whether the rightmost bit in set in one each element of the array..if its set, it will be removed when a pair of the same item is encountered and thus we will be able to extract x;
Then we also xor the elems which does not have the right most bit set. So, we will be extracting all the pairs with this bit not set and y ( the pairs cancel each other and y remains )
I am going to try explain this more clearly.
First how xor works: if you have two numbers say 10 (1010) and 8 (1000), xor operation ( done using operator ^ ) will return the bits which are set in one number but not in the other.
10 -- 1010
08 -- 1000
---------------
^ -- 0010
---------------
Here the third bit is set(from right) in 10 but not in 8;
Now the problem states that there are 2n + 2 numbers such that n numbers are repeated.
eg. for n = 3: { 2, 3, 2, 4, 5, 4, 5, 6 }
there are two of ( 2, 4 & 5 ) but only one of ( 3 & 6 )
Lets take the first set of number that repeats..
when we xor a number with itself the result is zero.
2 ^ 2 = 0 (in binary: 10 ^ 10 )
4 ^ 4 = 0
5 ^ 5 = 0
Now we have actually got rid of the numbers that repeats twice.
so when we xor all the elements of the array : a[i] ^ a[i+1] ^ ... a[n], we would have performed, for the above example ( 2 ^ 3 ^ 2 ^ 4 ^ 5 ^ 4 ^ 5 ^ 6 )
Since the repeating elements are nullified :) we are left with ( 3 ^ 6 ), the xor of the two elements we need to find. If the elements are equal, the 2n+2 constraint is not valid and hence these two numbers would have also been nullified.
Now all we have to do is extract the two numbers (x, y) from ( 3 ^ 6 )
3 ^ 6 is
011
110
-----
101
-----
what we can infer from the above is that the last bit is set in one number and not in the other. ( and so does the first bit)
set_bit_no = xor & ~(xor-1);
This line in the above example by Rajat Rastogi can be replaced with
int set_bit_no = 1;
while ( ! ( xor & set_bit_no ) )
set_bit_no <<=1;
what it does is right shift, a bit at a time and check whether it is set in the result of ( 3 ^ 6 )
Why do we need this?
Because, when doing a xor, a bit is set only when it is set in one and not in the other. So we are actually isolating a trait of one number so that we can extract it from the result.
Now is the tricky part.
for(i = 0; i < n; i++)
{
if(arr[i] & set_bit_no)
x = x ^ arr[i]; /*XOR of first set */
else
y = y ^ arr[i]; /*XOR of second set*/
}
Our example array: { 2, 3, 2, 4, 5, 4, 5, 6 }
x & y is initially zero;
Again we apply the same principle, but this time we check whether the rightmost bit in set in one each element of the array..if its set, it will be removed when a pair of the same item is encountered and thus we will be able to extract x;
Then we also xor the elems which does not have the right most bit set. So, we will be extracting all the pairs with this bit not set and y ( the pairs cancel each other and y remains )
The cost of spawning a thread is much cheaper than creating a separate process.
Create a process when you want a part of your system to run independently, such as something that provides a service. You can still communicate with IPC ( inter process communication ). But if you want to speed up your program and sharing some data, address space..use threads.
I am going to try explain this more clearly.
First how xor works: if you have two numbers say 10 (1010) and 8 (1000), xor operation ( done using operator ^ ) will return the bits which are set in one number but not in the other.
10 -- 1010
08 -- 1000
---------------
^ -- 0010
---------------
Here the third bit is set(from right) in 10 but not in 8;
Now the problem states that there are 2n + 2 numbers such that n numbers are repeated.
eg. for n = 3: { 2, 3, 2, 4, 5, 4, 5, 6 }
there are two of ( 2, 4 & 5 ) but only one of ( 3 & 6 )
Lets take the first set of number that repeats..
when we xor a number with itself the result is zero.
2 ^ 2 = 0 (in binary: 10 ^ 10 )
4 ^ 4 = 0
5 ^ 5 = 0
Now we have actually got rid of the numbers that repeats twice.
so when we xor all the elements of the array : a[i] ^ a[i+1] ^ ... a[n], we would have performed, for the above example ( 2 ^ 3 ^ 2 ^ 4 ^ 5 ^ 4 ^ 5 ^ 6 )
Since the repeating elements are nullified :) we are left with ( 3 ^ 6 ), the xor of the two elements we need to find. If the elements are equal, the 2n+2 constraint is not valid and hence these two numbers would have also been nullified.
Now all we have to do is extract the two numbers (x, y) from ( 3 ^ 6 )
3 ^ 6 is
011
110
-----
101
-----
what we can infer from the above is that the last bit is set in one number and not in the other. ( and so does the first bit)
set_bit_no = xor & ~(xor-1);
This line in the above example by Rajat Rastogi can be replaced with
int set_bit_no = 1;
while ( ! ( xor & set_bit_no ) )
set_bit_no <<=1;
what it does is right shift, a bit at a time and check whether it is set in the result of ( 3 ^ 6 )
Why do we need this?
Because, when doing a xor, a bit is set only when it is set in one and not in the other. So we are actually isolating a trait of one number so that we can extract it from the result.
Now is the tricky part.
}
- SuYo February 13, 2012Our example array: { 2, 3, 2, 4, 5, 4, 5, 6 }
x & y is initially zero;
Again we apply the same principle, but this time we check whether the rightmost bit in set in one each element of the array..if its set, it will be removed when a pair of the same item is encountered and thus we will be able to extract x;
Then we also xor the elems which does not have the right most bit set. So, we will be extracting all the pairs with this bit not set and y ( the pairs cancel each other and y remains )