Sliding Window (Help)

Problem Description.

Given an array of N integers you find XOR every possible k continous elements in the array.

Input:
5 3
1 2 3 4 5

Output:
0 5 2

Can anyone please help me how could i use sliding window concept here or is there any other method to solve this problem…??

Please write the problem statement properly.

I think the input is
5 3
1 2 3 4 5

Anyways.
Find the xor for First K elements suppose it’s X
After that loop for other elements at each step ( Suppose the step is i now) remove the contribution of the previous element by doing xor with current xor ( that is X ^ (A[i-1] )
And Add the contribution of the last element of the current window by doing xor with A[i+k-1] ^ X
Print X every time.

its quite easy problem…

int ans = 0;
for(int i = 0; i < k; i++)
 ans = (ans ^ a[i]);
cout << ans << ' ';
int pt = 0; 
for(int j = k; j < n; j++)
{
 ans = (ans ^ a[pt]);
 ans = (ans ^ a[j]);
 cout << ans << ' ';
 pt++;
}
1 Like

@rgkbitw So we can used XOR operation same as addition operation…??