Loading...
Loading...
You are given an integer array nums and two integers budget and m.
Consider an operation where you choose an index i and increase nums[i] by 1. Given that you can perform this operation at most budget times in total, return the maximum possible bitwise AND of m elements of nums. The m elements must be at distinct indices, and they are chosen after the operations.
Input: nums = [3,1,2], budget = 8, m = 2
Output: 6
Explanation: Raise nums[0] from 3 to 6 (3 operations) and nums[2] from 2 to 6 (4 operations).
That uses 7 operations, which is within the budget of 8.
The two chosen elements are 6 and 6, and 6 AND 6 = 6. No choice reaches a larger AND.
Input: nums = [1,2,8,4], budget = 7, m = 3
Output: 4
Explanation: Raise nums[0] from 1 to 4 (3 operations) and nums[1] from 2 to 4 (2 operations).
That uses 5 operations, which is within the budget of 7.
Choosing indices 0, 1, and 3 gives 4 AND 4 AND 4 = 4. No choice reaches a larger AND.
Input: nums = [1,1], budget = 3, m = 2
Output: 2
Explanation: Raise both elements from 1 to 2, using 2 operations.
Then 2 AND 2 = 2. Reaching 3 AND 3 would need 4 operations.
nums.lengthnums[i] ≤109budget ≤109m ≤n