Sum of XOR of all sub-arrays of length K
Last Updated :
24 Feb, 2023
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)
Auxiliary Space: O(1)
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++
#include <iostream>
using namespace std;
int FindXorSum( int arr[], int k, int n)
{
if (n < k)
return 0;
int x[n] = { 0 };
int result = 0;
for ( int i = 0; i < n; i++) {
if (i > 0)
x[i] = x[i - 1] ^ arr[i];
else
x[i] = arr[i];
if (i >= k - 1) {
int sum = 0;
sum = x[i];
if (i - k > -1)
sum ^= x[i - k];
result += sum;
}
}
return result;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int n = 4, k = 2;
cout << FindXorSum(arr, k, n) << endl;
return 0;
}
|
Java
class GFG
{
static int FindXorSum( int arr[], int k, int n)
{
if (n < k)
return 0 ;
int []x = new int [n];
int result = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (i > 0 )
x[i] = x[i - 1 ] ^ arr[i];
else
x[i] = arr[i];
if (i >= k - 1 )
{
int sum = 0 ;
sum = x[i];
if (i - k > - 1 )
sum ^= x[i - k];
result += sum;
}
}
return result;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int n = 4 , k = 2 ;
System.out.println(FindXorSum(arr, k, n));
}
}
|
Python3
def FindXorSum(arr, k, n):
if (n < k):
return 0 ;
x = [ 0 ] * n;
result = 0 ;
for i in range (n):
if (i > 0 ):
x[i] = x[i - 1 ] ^ arr[i];
else :
x[i] = arr[i];
if (i > = k - 1 ):
sum = 0 ;
sum = x[i];
if (i - k > - 1 ):
sum ^ = x[i - k];
result + = sum ;
return result;
arr = [ 1 , 2 , 3 , 4 ];
n = 4 ; k = 2 ;
print (FindXorSum(arr, k, n));
|
C#
using System;
class GFG
{
static int FindXorSum( int []arr, int k, int n)
{
if (n < k)
return 0;
int []x = new int [n];
int result = 0;
for ( int i = 0; i < n; i++)
{
if (i > 0)
x[i] = x[i - 1] ^ arr[i];
else
x[i] = arr[i];
if (i >= k - 1)
{
int sum = 0;
sum = x[i];
if (i - k > -1)
sum ^= x[i - k];
result += sum;
}
}
return result;
}
public static void Main()
{
int []arr = { 1, 2, 3, 4 };
int n = 4, k = 2;
Console.WriteLine(FindXorSum(arr, k, n));
}
}
|
Javascript
<script>
function FindXorSum(arr, k, n)
{
if (n < k)
return 0;
let x = new Array(n).fill(0);
let result = 0;
for (let i = 0; i < n; i++) {
if (i > 0)
x[i] = x[i - 1] ^ arr[i];
else
x[i] = arr[i];
if (i >= k - 1) {
let sum = 0;
sum = x[i];
if (i - k > -1)
sum ^= x[i - k];
result += sum;
}
}
return result;
}
let arr = [ 1, 2, 3, 4 ];
let n = 4, k = 2;
document.write(FindXorSum(arr, k, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...