Sum of XOR of all sub-arrays of length K
Given an array of length n ( n > k), we have to find the sum of xor of all the elements of the sub-arrays which are of length k.
Examples:
Input : arr[]={1, 2, 3, 4}, k=2
Output :Sum= 11
Sum = 1^2 + 2^3 + 3^4 = 3 + 1 + 7 =11
Input :arr[]={1, 2, 3, 4}, k=3
Output :Sum= 5
Sum = 1^2^3 + 2^3^4 = 0 + 5 =5
Naive Solution: The idea is to traverse all the subarrays of length k and find the xor of all the elements of the subarray and sum them up to find the sum of XOR of all K length sub-array of an array.
Time Complexity : O(N2)
Efficient Solution: The efficient solution is to traverse the array and find all the subarray of length k, i.e. ( 0 to k-1), (1 to k), (2 to k+1), …., (n-k+1 to n).
We will find and store the xor of elements from 0 to i (in an array x[]) by forming a pre-xor array.
Now, xor of sub array from l to r is equal to x[l-1] ^ x[r] because x[r] will give the xor of all elements till r and x[l-1] will give the xor of all elements till l-1. When we will take xor of these two values the elements till 0 to l-1 will be repeated. As a^a = 0, the repeated values would contribute zero to the net value and we get the value of xor sub array from l to r.
Below is the implementation of the above approach:
C++
// C++ implementation of above approach #include <iostream> using namespace std; // Sum of XOR of all K length // sub-array of an array int FindXorSum(int arr[], int k, int n) { // If the length of the array is less than k if (n < k) return 0; // Array that will store xor values of // subarray from 1 to i int x[n] = { 0 }; int result = 0; // Traverse through the array for (int i = 0; i < n; i++) { // If i is greater than zero, store // xor of all the elements from 0 to i if (i > 0) x[i] = x[i - 1] ^ arr[i]; // If it is the first element else x[i] = arr[i]; // If i is greater than k if (i >= k - 1) { int sum = 0; // Xor of values from 0 to i sum = x[i]; // Now to find subarray of length k // that ends at i, xor sum with x[i-k] if (i - k > -1) sum ^= x[i - k]; // Add the xor of elements from i-k+1 to i result += sum; } } // Return the resultant sum; return result; } // Driver code int main() { int arr[] = { 1, 2, 3, 4 }; int n = 4, k = 2; cout << FindXorSum(arr, k, n) << endl; return 0; } |
Java
// Java implementation of the approach class GFG { // Sum of XOR of all K length // sub-array of an array static int FindXorSum(int arr[], int k, int n) { // If the length of the array is less than k if (n < k) return 0; // Array that will store xor values of // subarray from 1 to i int []x = new int[n]; int result = 0; // Traverse through the array for (int i = 0; i < n; i++) { // If i is greater than zero, store // xor of all the elements from 0 to i if (i > 0) x[i] = x[i - 1] ^ arr[i]; // If it is the first element else x[i] = arr[i]; // If i is greater than k if (i >= k - 1) { int sum = 0; // Xor of values from 0 to i sum = x[i]; // Now to find subarray of length k // that ends at i, xor sum with x[i-k] if (i - k > -1) sum ^= x[i - k]; // Add the xor of elements from i-k+1 to i result += sum; } } // Return the resultant sum; return result; } // Driver code public static void main(String[] args) { int arr[] = { 1, 2, 3, 4 }; int n = 4, k = 2; System.out.println(FindXorSum(arr, k, n)); } } // This code contributed by Rajput-Ji |
Python3
# Python implementation of above approach # Sum of XOR of all K length # sub-array of an array def FindXorSum(arr, k, n): # If the length of the array is less than k if (n < k): return 0; # Array that will store xor values of # subarray from 1 to i x = [0]*n; result = 0; # Traverse through the array for i in range(n): # If i is greater than zero, store # xor of all the elements from 0 to i if (i > 0): x[i] = x[i - 1] ^ arr[i]; # If it is the first element else: x[i] = arr[i]; # If i is greater than k if (i >= k - 1): sum = 0; # Xor of values from 0 to i sum = x[i]; # Now to find subarray of length k # that ends at i, xor sum with x[i-k] if (i - k > -1): sum ^= x[i - k]; # Add the xor of elements from i-k+1 to i result += sum; # Return the resultant sum; return result; # Driver code arr = [ 1, 2, 3, 4 ]; n = 4; k = 2; print(FindXorSum(arr, k, n)); # This code has been contributed by 29AjayKumar |
C#
// C# implementation of the above approach using System; class GFG { // Sum of XOR of all K length // sub-array of an array static int FindXorSum(int []arr, int k, int n) { // If the length of the array is less than k if (n < k) return 0; // Array that will store xor values of // subarray from 1 to i int []x = new int[n]; int result = 0; // Traverse through the array for (int i = 0; i < n; i++) { // If i is greater than zero, store // xor of all the elements from 0 to i if (i > 0) x[i] = x[i - 1] ^ arr[i]; // If it is the first element else x[i] = arr[i]; // If i is greater than k if (i >= k - 1) { int sum = 0; // Xor of values from 0 to i sum = x[i]; // Now to find subarray of length k // that ends at i, xor sum with x[i-k] if (i - k > -1) sum ^= x[i - k]; // Add the xor of elements from i-k+1 to i result += sum; } } // Return the resultant sum; return result; } // Driver code public static void Main() { int []arr = { 1, 2, 3, 4 }; int n = 4, k = 2; Console.WriteLine(FindXorSum(arr, k, n)); } } // This code is contributed by AnkitRai01 |
11
Time Complexity : O(N)
Recommended Posts:
- Maximum sum of non-overlapping subarrays of length atmost K
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Program to find Length of Bridge using Speed and Length of Train
- Length of the chord the circle if length of the another chord which is equally inclined through the diameter is given
- Sum of all Subarrays | Set 1
- Sum of XOR of all subarrays
- Number of subarrays having sum less than K
- Sum of bitwise OR of all subarrays
- Sum of bitwise AND of all subarrays
- Print all subarrays with 0 sum
- Count subarrays with same even and odd elements
- Generating subarrays using recursion
- Maximize the number of subarrays with XOR as zero
- Count subarrays with Prime sum
- Queries on XOR of XORs of all subarrays
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



