While giving 1hr Coding Round - 2 of an online assessment of XYZ company I came across this question which I could not solve to pass all the test cases.
If anyone can try hands on it and explain it in the comments, it would be of greater help.
Here goes the problem statement:-
Problem Statement: Alice and Bob are best friends once Alice shared the password of his account with Bob who wanted to play a game with Alice so he changed the password of Alice's account and challenged him to decode the password. You are given an array A of N integers and an integer K, the new passowrd is the bitwise XOR of bitwise AND of all the subarrays of A having a size of at most K.
Task: Determine the password of the account from the given instruction.
Input format:
N
A
K
where:
N - represents size of array A
A - represents the elements of array A
K - represents value of K
Input Constraints:
1<=T<=10
1<=N<=100000
1<=K<=N
1<=A[i]<=100000
Sample Input:
N=5
A=[5,7,8,2,6]
K=3
Sample Output:
9
Approach:
bitwise AND of subarrays having size less than 3 are as follows:
Note: 1 based indexing used here.
[5,5] = 6
[4,4] = 2, [4,5] = 2
[3,3] = 8, [3,4] = 0, [3,5] = 0
[2,2] = 7, [2,3] = 0, [2,4] = 0
[1,1] = 5, [1,2] = 5, [1,3] = 0
The bitwise XOR of calculated values will be the password:-
6^2^2^8^0^0^7^0^0^5^5^0 = 9
Hence the answer is 9.