Given a set, find XOR of the XOR's of all subsets. - GeeksforGeeks
The question is to find XOR of the XOR's of all subsets. i.e if the set is {1,2,3}. All subsets are : [{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]. Find the XOR of each of the subset and then find the XOR of every subset result.
Read full article from Given a set, find XOR of the XOR's of all subsets. - GeeksforGeeks
The question is to find XOR of the XOR's of all subsets. i.e if the set is {1,2,3}. All subsets are : [{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]. Find the XOR of each of the subset and then find the XOR of every subset result.
This is a very simple question to solve if we get the first step (and only step) right. The solution is XOR is always 0 when n > 1 and Set[0] when n is 1.
int findXOR(int Set[], int n){ // XOR is 1 only when n is 1, else 0 if (n == 1) return Set[0]; else return 0;}
The logic goes simple. Let us consider n’th element, it can be included in all subsets of remaining (n-1) elements. The number of subsets for (n-1) elements is equal to 2(n-1) which is always even when n>1. Thus, in the XOR result, every element is included even number of times and XOR of even occurrences of any number is 0.
http://www.geeksforgeeks.org/xor-subarray-xors/
Given an array of integers, we need to get total XOR of all subarray XORs where subarray XOR can be obtained by XORing all elements of it.
A simple solution is to generate all subarrays and compute XOR of all of them.
int getTotalXorOfSubarrayXors(int arr[], int N){ // initialize result by 0 as (a xor 0 = a) int res = 0; // Consider all subarrays for (int i=0; i<N; i++) for (int j=i; j<N; j++) res = res ^ arr[j]; // return the result return res;}
An efficient solution is based on the idea to enumerate all subarrays, we can count frequency of each element occurred totally in all subarrays, if the frequency of an element is odd then it will be included in final result otherwise not.
As in above example, 3 occurred 5 times, 5 occurred 8 times, 2 occurred 9 times, 4 occurred 8 times, 6 occurred 5 times So our final result will be xor of all elements which occurred odd number of times i.e. 3^2^6 = 7 From above occurrence pattern we can observe that number at i-th index will have (i + 1) * (N - i) frequency.
So we can iterate over all elements once and calculate their frequencies and if it is odd then we can include that in our final result by XORing it with the result.
Total time complexity of solution will be O(N)
Total time complexity of solution will be O(N)
int getTotalXorOfSubarrayXors(int arr[], int N){ // initialize result by 0 as (a XOR 0 = a) int res = 0; // loop over all elements once for (int i = 0; i < N; i++) { // get the frequency of current element int freq = (i + 1) * (N - i); // Uncomment below line to print the // frequency of arr[i] // cout << arr[i] << " " << freq << endl; // if frequency is odd, then include it // in the result if (freq % 2 == 1) res = res ^ arr[i]; } // return the result return res;}