Favourite Operation
Ninja has recently studied operators ‘AND’ and ‘XOR’. His teacher gave him an assignment related to the two operators but Ninja is taking a lot of time to compute it and he is afraid he would be unable to complete the assignment before the deadline. Ninja has asked you to help him complete the assignment before the deadline.
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.
Input Format :
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.
Constraints :
1 <= T <= 5
1 <= N <= 10^5
1 <= K <= N
1 <= A[i] <= 10^5
Time Limit : 1 sec
Be the first one to answer
Add answer anonymously...
Top Amazon Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Top HR questions asked in Amazon Software Developer Intern
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app