You are given an array βAβ having βNβ integers and an integer βKβ. You first calculate the bitwise βANDβ of all subarrays having size at most βKβ. Then you take the bitwise βXORβ of all these βANDβ results.
Your task is to output the integer you receive after performing the above operations.
Example :N = 3
K = 2
A = [ 1, 2, 3 ]
Explanation :
The bitwise βANDβ of all subarrays of size <= 2 are :
From index 1 :
Subarray of length 1 has βANDβ = 1.
Subarray of length 2 has βANDβ = 1 & 2 = 0.
From index 2 :
Subarray of length 1 has βANDβ = 2.
Subarray of length 2 has βANDβ = 2 & 3 = 2.
From index 3 :
Subarray of length 1 has βANDβ = 3.
βXORβ of all these βANDβ operations = 1 ^ 0 ^ 2 ^ 2 ^ 3 = 2.
So, final result = 2.
The first line contains an integer 'T' which denotes the number of test cases to be run. Then the test cases follow.
The first line of each test case contains two integers βNβ and βKβ.
The second line of each test case contains an array βAβ of size βNβ.
Output format :
For each test case, print one integer denoting the final result after performing the operations.
Print the output of each test case in a new line.
Note :
You donβt need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 5
1β<= N <= 10^5
1 <= K <= N
1 <= A[i] <= 10^5
Time Limit : 1 sec
2
4 2
1 2 3 4
3 3
4 8 2
6
14
For test case 1 we have,
The bitwise βANDβ of all subarrays of size <= 2 is : 1, 2, 3, 4, 0, 2, 0.
The bitwise βXORβ of all βANDβ operations is : 6
So, we output 6.
For test case 2 we have,
The bitwise βANDβ of all subarrays of size <= 3 is : 4, 8, 2, 0, 0, 0.
The bitwise βXORβ of all βANDβ operations is : 14
So, we output 14.
3
3 2
6 5 2
2 1
9 7
3 1
5 2 4
5
14
3
Simulate the Problem Statement.
Approach :
Algorithm :
O(N^2) , where βNβ is the size of array βAβ.
We are calculating βANDβ of all subarrays, so the overall time complexity is O( N^2 ).
O(1)
Constant extra space is required. Hence, the overall Space Complexity is O(1).