Maximize K-Element AND with an Increment Budget
Problem statement
You are given an array nums, an integer k, and a nonnegative increment budget budget. You may increase array elements by nonnegative integer amounts whose total is at most budget.
After applying the increments, choose exactly k distinct array positions. Return the maximum possible bitwise AND of the chosen values.
You do not need to return the chosen positions or the increment allocation.
Function
maximizeAnd(nums: int[], k: int, budget: int) → longExamples
Example 1
nums = [5,4,1,7,2]k = 3budget = 3return = 6Increase 4 to 6 and 5 to 6. Choosing 6, 6, 7 gives 6 & 6 & 7 = 6.
Example 2
nums = [1,2,3]k = 2budget = 0return = 2Without increments, choose 2 and 3; their bitwise AND is 2.
Example 3
nums = [0,0]k = 2budget = 2return = 1Increase both values to 1. Their bitwise AND is 1.
Constraints
1 <= nums.length <= 1000001 <= k <= nums.length0 <= nums[i] <= 10000000000 <= budget <= 1000000000- The result is returned as a signed 64-bit integer.
Source note: Author-written prompt excerpt retained for source fidelity.