Favourite Operation - Problem Statement
Ninja has studied the bitwise operations ‘AND’ and ‘XOR’. He received an assignment that involves these operations and needs help to complete it efficiently before the deadline.
Given an array A
containing N
integers and an integer K
, you have to:
- Calculate the bitwise ‘AND’ for all subarrays of size at most
K
. - Calculate the bitwise ‘XOR’ of all these ‘AND’ results.
Your task is to output the resulting integer after performing the above operations.
Input:
The first line contains an integer 'T', the number of test cases.
For each test case:
- The first line contains two integers 'N' and 'K'.
- The second line contains 'N' space-separated integers representing the array ‘A’.
Output:
For each test case, output one integer resulting from the operations.
Example:
Input:
N = 3
K = 2
A = [1, 2, 3]
Output:
2
Explanation:
The bitwise ‘AND’ of all subarrays of size <= 2 are:
- From index 1: AND of subarray [1] = 1; AND of subarray [1, 2] = 0.
- From index 2: AND of subarray [2] = 2; AND of subarray [2, 3] = 2.
- From index 3: AND of subarray [3] = 3.
The XOR of these AND results is: 1 ^ 0 ^ 2 ^ 2 ^ 3 = 2.
Constraints:
- 1 <= T <= 5
- 1 <= N <= 105
- 1 <= K <= N
- 1 <= A[i] <= 105
- Time Limit: 1 sec
Note:
Just implement the function as directed and the printing will be handled elsewhere.
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