Find a number X such that XOR of given Array after adding X to each element is 0
Given an array arr[] of odd length N containing positive integers. The task is to find a positive integer X such that, adding X to all the elements of arr[] and then taking XOR of all the elements gives 0. Return -1 if no such X exists.
Examples:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Input: arr[] = {2, 4, 5}
Output: 1
Explanation: Following are the operations performed in arr[] to get the desired result.
Adding 1 to each element in arr[] updates arr[] to arr[] = {3, 5, 6}
Now XOR of all the elements in arr[] is 3^5^6 = 0.
Therefore, 1 is the required answer.Input: arr[] = {4, 5, 13}
Output: -1
Explanation: No such x exists for fulfilling the desired conditions.
Approach: XOR of Odd number of 1’s = 1, while even number of 1’s = 0. This idea can be used to solve the given problem. Follow the steps mentioned below:
- Initialize variable X = 0.
- The binary representations of the array elements will be used for traversal of the elements and determining X.
- Start traversing from the 0th bit to 63rd bit.
- If at any bit position total number of set bits (1’s) of the array elements are odd, add that power of 2 with X.
- If after completion of iteration there is odd number of 1 at any bit position then no such X exists. Otherwise, print X as answer.
See the illustrations below:
Illustration:
Case-1 (X possible): Take arr[] = { 2, 4, 5}
5th 4th 3rd 2nd 1st 0th arr[0] 0 0 0 0 1 0 arr[1] 0 0 0 1 0 0 arr[2] 0 0 0 1 0 1 X 0 0 0 0 0 0
- Initially, X = 0 0 0 0 0 0, at 0th position set(1) bits are odd, so in order to make the set bits even, flip the bits at 0th position. So, for flipping the bits just add (0 0 0 0 0 1) to all the elements of arr[] and in X.
- Now, the table will look like the following:
5th 4th 3rd 2nd 1st 0th arr[0] 0 0 0 0 1 1 arr[1] 0 0 0 1 0 1 arr[2] 0 0 0 1 1 0 XOR 0 0 0 0 0 0 X 0 0 0 0 0 1
- Now, the XOR of (arr[0]+x) ^ ( arr[1]+x) ^ arr[2]+x) = 0, result will be 0. So, print res = X.
Take the following when no possible X
Case-2: Take example: arr[] = { 4, 5, 13 }
5th 4th 3rd 2nd 1st 0th arr[0] 0 0 0 1 0 0 arr[1] 0 0 0 1 0 1 arr[2] 0 0 1 1 0 1 XOR 0 0 1 1 0 0 X 0 0 0 0 0 0 XOR = Arr[0] ^ Arr[1] ^ Arr[2] = 1 1 0 0, Here there are odd number of 1‘s at 2nd and 3rd bits.
- So, add 2pow(2nd) to all elements of arr and in X, then again will take the XOR, after this the elements become:
5th 4th 3rd 2nd 1st 0th arr[0] 0 0 1 0 0 0 arr[1] 0 0 1 0 0 1 arr[2] 0 1 0 0 0 1 XOR 0 1 0 0 0 0 X 0 0 0 1 0 0 If this keeps on going the left most 1 in XOR keeps on moving left.
Below is the implementation of the above approach:
C++
// C++ program for above approach#include <bits/stdc++.h>using namespace std; // Function to find required resultlong long solve(vector<long long>& a, int n){ long long res = 0, j = 0, one = 1; // For 64 Bit while (j < 64) { // j is traversing each bit long long Xor = 0; long long powerOf2 = one << j; for (auto x : a) Xor ^= x; if (j == 63 && (Xor & powerOf2)) return -1; if (Xor & powerOf2) { res += powerOf2; for (int i = 0; i < n; i++) a[i] += powerOf2; } j++; } return res;} // Driver Codeint main(){ // Size of arr[] int N = 3; vector<long long> arr = { 2, 4, 5 }; cout << solve(arr, N) << '\n'; return 0;} |
1
Time Complexity: O(N*logN)
Auxiliary Space: O(1)


