Repeated and missing number in an array using xor - c

How to find the repeated number and missing number as well using xor?
For eg: actual = [1,2,3] input_received = [3,2,3]. Here the missing number is 1 and the repeated number is 3. I found a quite interesting solution while surfing,
int missing_and_repeating(int a[], int n, int size){
int xor =0;
int i;
int x =0 , y =0;
for(i=0; i<size; i++)
xor = xor^a[i];
for(i=1; i<=n; i++)
xor = xor^i;
// Get the rightmost bit which is set
int set_bit_no = xor & ~(xor -1);
// XOR numbers in two buckets
for(i=0; i<size; i++){
if(a[i]& set_bit_no){
x = x^a[i];
}
else
y = y^ a[i];
}
for(i=1; i<=n; i++){
if(i & set_bit_no)
x = x^i;
else
y = y^i;
}
printf("\n %d %d ", x,y );
}
'actual' array is XORed and 'input_received' array is XORed
set_bit_no is assigned and both the arrays are split into two halves according to set_bit_no.
So again go back to our array and numbers from 1 to N-1 and 0 to size and XOR numbers in two buckets, one buckets contains XOR result of XORing all numbers with given bit set and other bucket contains XOR result of XORing all numbers with given bit reset.
I could not understand what set_bit_no is and why they are taking it, and how the array is split according to it. Someone please help me with a short example.

I assume that pre-condition is that exactly one array element is missing and instead replaced by a duplicate of another element. So, actual = {a1, a2, ..., an} and input_received = {a2, a2,..., an} (we can always rearrange elements so that a1 is missing and a2 is repeated).
So when you xor all elements from both arrays you get xored = a1 ^ a2.
Now we have to decompose that number to know a1 and a2. You take one of the non-zero bits (doesn't mater which one, the easiest way is to take the least significant like it's done in the code, and you always have one if a1 != a2). This bit is set in only one of numbers a1 or a2. So you xor all numbers which have this bit, and they'll annihilate each other, leaving only a1 or a2, and you xor all other numbers, so the result will be another number - a2 or a1 respectively.
It's important to note that this algorithm doesn't tell which of these numbers is missing and which is repeated. This is demonstrated by the following code:
int main() {
int arr1[] = {1, 1};
missing_and_repeating(arr1, 2, 2);
int arr2[] = {2, 2};
missing_and_repeating(arr2, 2, 2);
}
Output:
1 2
1 2

Related

Find 2 repeating elements in given array

Given an array with n+2 elements, all elements in the array are in the range 1 to n and all elements occur only once except two elements which occur twice.
Find those 2 repeating numbers. For example, if the array is [4, 2, 4, 5, 2, 3, 1], then n is 5, there are n+2 = 7 elements with all elements occurring only once except 2 and 4.
So my question is how to solve the above problem using XOR operation. I have seen the solution on other websites but I'm not able to understand it. Please consider the following example:
arr[] = {2, 4, 7, 9, 2, 4}
XOR every element. xor = 2^4^7^9^2^4 = 14 (1110)
Get a number which has only one set bit of the xor. Since we can easily get the rightmost set bit, let us use it.
set_bit_no = xor & ~(xor-1) = (1110) & ~(1101) = 0010. Now set_bit_no will have only set as rightmost set bit of xor.
Now divide the elements in two sets and do xor of elements in each set, and we get the non-repeating elements 7 and 9.
Yes, you can solve it with XORs. This answer expands on Paulo Almeida's great comment.
The algorithm works as follows:
Since we know that the array contains every element in the range [1 .. n], we start by XORing every element in the array together and then XOR the result with every element in the range [1 .. n]. Because of the XOR properties, the unique elements cancel out and the result is the XOR of the duplicated elements (because the duplicate elements have been XORed 3 times in total, whereas all the others were XORed twice and canceled out). This is stored in xor_dups.
Next, find a bit in xor_dups that is a 1. Again, due to XOR's properties, a bit set to 1 in xor_dups means that that bit is different in the binary representation of the duplicate numbers. Any bit that is a 1 can be picked for the next step, my implementation chooses the least significant. This is stored in diff_bit.
Now, split the array elements into two groups: one group contains the numbers that have a 0 bit on the position of the 1-bit that we picked from xor_dups. The other group contains the numbers that have a 1-bit instead. Since this bit is different in the numbers we're looking for, they can't both be in the same group. Furthermore, both occurrences of each number go to the same group.
So now we're almost done. Consider the group for the elements with the 0-bit. XOR them all together, then XOR the result with all the elements in the range [1..n] that have a 0-bit on that position, and the result is the duplicate number of that group (because there's only one number repeated inside each group, all the non-repeated numbers canceled out because each one was XORed twice except for the repeated number which was XORed three times).
Rinse, repeat: for the group with the 1-bit, XOR them all together, then XOR the result with all the elements in the range [1..n] that have a 1-bit on that position, and the result is the other duplicate number.
Here's an implementation in C:
#include <assert.h>
void find_two_repeating(int arr[], size_t arr_len, int *a, int *b) {
assert(arr_len > 3);
size_t n = arr_len-2;
int i;
int xor_dups = 0;
for (i = 0; i < arr_len; i++)
xor_dups ^= arr[i];
for (i = 1; i <= n; i++)
xor_dups ^= i;
int diff_bit = xor_dups & -xor_dups;
*a = 0;
*b = 0;
for (i = 0; i < arr_len; i++)
if (arr[i] & diff_bit)
*a ^= arr[i];
else
*b ^= arr[i];
for (i = 1; i <= n; i++)
if (i & diff_bit)
*a ^= i;
else
*b ^= i;
}
arr_len is the total length of the array arr (the value of n+2), and the repeated entries are stored in *a and *b (these are so-called output parameters).

FFT reordering phase

I've got some code for you guys.
This is the first step of the splitting algorithm for the Fast Fourier Transform.
What the algorithm should do is reorder the array such that every element on the input will be displaced in the "binary mirrored" position of the output.
For example, the element X[4] will be in position X[1], since the mirrored representation of 100 is 001.
Till here, everything is clear. However, the algorithm that does such reordering is not. Atleast I have a hard time understanding.
What does the second, inner loop do?
// N is the length of the array
// p is the number of bits needed to represent the index
for(int n=0; n<N; n++) {
int j=0;
int m=n;
for(int i=0; i<p; i++) {
j = 2∗j + m%2; m = m/2;
}
if ( j>n) {
complex<double> h;
h = X[j];
X[j] = X[n];
X[n] = h;
}
}
Think of an integer as being a sequence of bits.
j = 2j this pops the bit on the left and pushes zero into the right
m % 2 this obtains the right bit
m = m / 2 pops the bit on the right and pushes a copy of the leftmost bit on the left
j + x sets the rightmost bit of j to x, assuming that bit is currently zero and that x is 0 or 1
so all this is doing is popping bits off of the right of m and pushing them onto the right of j.
Each iteration, we multiply j by 2 (which is the same as shifting left by 1), then add the parity of m. Then we do integer division of m by two (which is the same as shifting right by 1). m starts with the value of n. So we are essentially reversing the bits in n, and storing it in j.

Radix-sort algorithm on the binary representation in C

Whats the most efficient way of implementing a Radix-sorting algorithm in C, that sorts by the binary representation (on an array of integeres)?
I would like to be able to give a number n representing the number of elements to sort, and also a value b telling how many binary digits you want to be interpreted as one digit in the sorting algorithm, so that you can vary the n and b depending on how big your numbers are.
Any suggestions how to implement this?
Any suggestions how to implement this?
#include <string.h>
#include <limits.h>
void radixsort(unsigned int array[], int n, int b)
{
unsigned int place[n], *a0 = array, *a1 = place, *a;
int values = 1<<b, start[values+1], bmask = values-1, shift;
for (shift = 0; shift < sizeof (int) * CHAR_BIT; shift += b)
{
int i;
memset(start, 0, sizeof start);
for (i = 0; i < n; ++i) ++start[(a0[i]>>shift&bmask)+1];
for (i = 2; i < values; ++i) start[i] += start[i-1];
for (i = 0; i < n; ++i) a1[start[a0[i]>>shift&bmask]++] = a0[i];
a = a0, a0 = a1, a1 = a;
}
if (a0 != array) memcpy(array, a0, sizeof place);
}
It turns out that rcgldr's description of the algorithm fits this implementation, except that the starting offsets are computed not until they're needed.
The approach I use is to make a single pass over the data to create a set (matrix) of histograms of the number of occurrences of a bit pattern in each of the bit fields used to sort by. This is the "counting" step of a radix sort. Then the histograms are converted into starting index offsets by starting with zero and accumulating a sum of the counts for each entry in a histogram set. Then the radix sort is performed for each bit field, lowest to highest order, using the starting indexes associated with that bit field and incrementing each index as it's used. Wiki article:
http://en.wikipedia.org/wiki/Counting_sort

How can I maximize XOR sum of subset of given array of integer? [duplicate]

I have to find maximum value of exclusive xor among the elements of subsets of an array. I have to check every subset of the array and the subset which will yield maximum xor will be the answer.
For exapmle- let F(S) denote the fuction which takes xor over all elements of subset S of array P={1,2,3,4}
F({1,2}) = 3
F({1,3}) = 2
F({1,2,3}) = 0
F({1,4}) = 5
F({2,3}) = 1
F({2,4}) = 6
F({3,4}) = 7
F({2,3,4}) = 5
F({1,2,3,4}) = 4`
Maximum of them is 7. Hence the answer is 7.(There are other subsets but they are not worth considering). If you are about to tell me about Gaussian Elimination method, I've read that somewhere on MSE but it was not at all clear to me.
If gauss elimination is the only answer than please elaborate that to me or is there some method/algorithm I don't know of?
Gaussian Elimination is what you need.
For example : 3 numbers {9, 8, 5}
First sort them in decreasing order and convert them into binary :
9 : 1001
8 : 1000
5 : 0101
Observe the 1st number. Highest bit is 4.
Now check 4th bit of the 1st number (9). As it is 1, xor the number with the rest of the numbers where 4th bit is 1.
9 : 1001
1 : 0001 > changed
5 : 0101
Now check 3rd bit of 2nd number (1). As it is 0, check rest of the below numbers where 3rd bit is 1.
Number 5 has 1 in 3rd bit. Swap them :
9 : 1001
5 : 0101 > swapped
1 : 0001 >
Now xor 5 with the rest of the numbers where 3rd bit is 1. Here none exists. So there will be no change.
Now check 2nd bit of 3rd number (1). As it is 0 and there is no other number below where 2nd bit is 1, so there will be no change.
Now check 1st bit of 3rd number (1). As it is 1, change the rest of the numbers where 1st bit is 1.
8 : 1000 > changed
4 : 0100 > changed
1 : 0001
No more bit left to consider :)
Now xor the whole remaining array {8 ^ 4 ^ 1} = 13
So 13 is the solution :)
That's pretty much how you solve the problem using Gaussian Elimination :)
Here is my C++ implementation :
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
ull check_bit(ull N,int POS){return (N & (1ULL<<POS));}
vector<ull>v;
ull gaussian_elimination()
{
int n=v.size();
int ind=0; // Array index
for(int bit=log2(v[0]);bit>=0;bit--)
{
int x=ind;
while(x<n&&check_bit(v[x],bit)==0)
x++;
if(x==n)
continue; // skip if there is no number below ind where current bit is 1
swap(v[ind],v[x]);
for(int j=0;j<n;j++)
{
if(j!=ind&&check_bit(v[j],bit))
v[j]^=v[ind];
}
ind++;
}
ull ans=v[0];
for(int i=1;i<n;i++)
ans=max(ans,ans^v[i]);
return ans;
}
int main()
{
int i,j,k,l,m,n,t,kase=1;
scanf("%d",&n);
ull x;
for(i=0;i<n;i++)
{
cin>>x;
v.push_back(x);
}
sort(v.rbegin(),v.rend());
cout<<gaussian_elimination()<<"\n";
return 0;
}
I guess that you're referring to this question.
Gaussian Elimination is the algorithm description that I would expect from the math site. This is what the algorithm looks like in Python.
def max_xor(iterable):
array = list(iterable) # make it a list so that we can iterate it twice
if not array: # special case the empty array to avoid an empty max
return 0
x = 0
while True:
y = max(array)
if y == 0:
return x
# y has the leading 1 in the array
x = max(x, x ^ y)
# eliminate
array = [min(z, z ^ y) for z in array]

How to find a 2 unpaired elements in array?

You have an array with n=2k+2 elements where 2 elements haven't pair. Example for 8 elemets array: 1 2 3 47 3 1 2 0. "47" and "0" haven't pair in array. If I have array where only 1 element has't pair, I solve this problem with XOR. But I have 2 unpair elements! What can I do? Solution could be for a O(n) time performance and for O(1) additional memory.
Some hints...
It will take 2 passes. First, go through the list and XOR all elements together. See what you get. Proceed from there.
Edit: The key observation about the result of the first pass should be that it shows you the set of bits in which the 2 unpaired elements differ.
Use INT_MAX/8 bytes of memory. Walk the array. XOR the bit corresponding to each value with 1. If there are 0 or 2 instances the bit will end up 0. If there is only one instance, it will be set. O(1) mem, O(N) time.
Scan the Array and put each number and count in hash.
Rescan and find out the items with count=1.
This is O(n).
You can try this.It will take O(n) time
int xor = arr[0];
int set_bit_no;
int i;
int x = 0; //First unpair number
int y = 0; //second unpair number
for (i = 1; i < n; i++)
xor ^= arr[i];
set_bit_no = xor & ~(xor-1);//Get the rightmost set bit in set_bit_no
for (i = 0; i < n; i++)
{
if (arr[i] & set_bit_no) {
//XOR of first set
x = x ^ arr[i];
}
else
{
//XOR of second set
y = y ^ arr[i];
}
}
Explanation...
arr[] = {2, 4, 7, 9, 2, 4}
1) Get the XOR of all the elements.
xor = 2^4^7^9^2^4 = 14 (1110)
2) Get a number which has only one set bit of the xor.
Since we can easily get the rightmost set bit, let us use it.
set_bit_no = xor & ~(xor-1) = (1110) & ~(1101) = 0010
Now set_bit_no will have only set as rightmost set bit of xor.
3) Now divide the elements in two sets and do xor of
elements in each set, and we get the non-repeating
elements 7 and 9.

Resources