First sort the array then replace every odd indexed element ‘ARR[i]’ with ‘ARR[i]’ ⊕ ‘X’
Where 1 <= ‘i’ <= ‘N’ and ⊕ is bitwise xor.
Input: ‘N’ = 4 ‘K’ = 1 ‘X’ = 2 ‘ARR’ = [2, 3, 1 , 4]
Output: 1
The execution of the operation is as follows :
[2, 3, 1, 4] =>Sort => [1, 2, 3, 4] => [1⊕2, 2, 3⊕2, 4] => [3, 2, 1, 4]
The first line contains ‘T’, denoting the number of test cases.
The First line of each test case contains three integers ‘N’, ‘K’, ‘X’.
The Second line contains ‘N’ numbers representing elements of the ‘ARR’.
Return Minimum and Maximum in order of the array ‘ARR’ after performing ‘K’ operations.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 10^5
1 <= K <= 10^4
0 <= X, ARR[i] <= 1000
Time Limit: 1 sec
For ‘K’ operations for each operation we first sort ‘ARR’ then we perform the second part of the operation of XORing all the odd indexed elements with ‘X’.
After the above step, we again sort ‘ARR’ and return the first and last element of the ‘ARR’.
In the problem, if we observe the constraints of ‘X’ and ‘ARR’ the value can be utmost 1000. So after the XOR operation, the max value can be 1023. As for 9 bits number max value after XOR we can achieve is 1023.
So now we know that every element can not exceed 1023 after the operation.
We will maintain the count of numbers that are in the array. For a number ‘Y’ which is in the array 1 or more than 1 time denoted by ‘cnt[Y]’.
We will replace a few of these numbers ‘Y’ with ‘Y’⊕‘X’ and the remaining stay the same.
Consider the number ‘Y’ if there are ‘Z’ numbers smaller than ‘Y’ in the array then the number of ‘Y’ changes to ‘Y’⊕‘X’ can be found out by the ‘Z’.
If ‘Z’ is even then ceil(cnt[y]/2) will change to ‘Y’⊕‘X’ and remain stays same.
Else floor(cnt[y]/2) will change to ‘Y’⊕‘X’ and remain stays same.
We will iterate through all ‘K’ operations with maintaining the frequency of elements in ‘cnt’ array and create another array ‘currCnt’ in each operation and store the count of numbers after the execution of the operation. After each operation, we will replace ‘cnt’ with ‘currCnt’.