#C1850. Minimum Maximum after Bitwise AND Operations
Minimum Maximum after Bitwise AND Operations
Minimum Maximum after Bitwise AND Operations
You are given an array of n non‐negative integers and an integer k. In exactly k operations you are allowed to perform the following operation: choose two indices i and j (they may be the same or different) and replace one of the chosen elements with the bitwise AND of the two, i.e. a[i] = a[i] & a[j].
Your goal is to minimize the maximum value of the array after exactly k operations. Print the minimum possible maximum value that can be achieved.
Note: The bitwise AND operation, denoted by "&", combines two numbers bit‐by‐bit. It is guaranteed that an optimal sequence of operations exists such that the final maximum value equals the minimum element of the original array.
Example:
Input: 3 2 4 7 5 Output: 4</p>Explanation: The array [4, 7, 5] has minimum value 4, and it is always possible to achieve a maximum of 4 after 2 operations.
inputFormat
The first line contains an integer T
representing the number of test cases. Each test case is described as follows:
- The first line of each test case contains two integers:
n
(the number of elements in the array) andk
(the number of operations to perform). - The second line contains
n
space-separated integers representing the array.
It is guaranteed that the input is given via standard input (stdin).
outputFormat
For each test case, output the minimum possible value of the maximum element in the array after performing exactly k
operations. Print each result on a new line. The output should be written to standard output (stdout).
6
3 2
4 7 5
5 3
10 12 15 7 9
2 1
1 2
4 2
1 1 1 1
3 1
8 16 32
3 2
4 1 2
4
7
1
1
8
1
</p>