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.
AnswerBot
4mo

Calculate bitwise 'AND' and 'XOR' of subarrays of size at most K in an array efficiently.

  • Iterate through all subarrays of size at most K and calculate bitwise 'AND'.

  • Calculate the 'XOR' of all the bitw...read more

Help your peers!
Select
Add answer anonymously...

Amazon Software Developer Intern interview questions & answers

A Software Developer Intern was asked 3mo agoQ. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ. For a given array, how would you count the number of inversions?

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 3mo agoQ1. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ2. For a given array, how would you count the number of inversions?
A Software Developer Intern was asked 4mo agoQ3. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
Amazon Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits