FastPrepMaximize K-Element AND with an Increment Budget
Problems/Maximize K-Element AND with an Increment Budget

Maximize K-Element AND with an Increment Budget

Amazon logoAmazon● HardFULLTIMENEW GRADOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report specifies choosing exactly k values, distributing at most a fixed number of integer increments, and maximizing their bitwise AND. The practice contract makes explicit that increments may be assigned to any array positions and that only the maximum value is returned.

Special thanks: A big thank-you to the friend who shared this question with us. Wishing them all the best with everything ahead!

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) → long

Examples

Example 1

nums = [5,4,1,7,2]k = 3budget = 3return = 6

Increase 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 = 2

Without increments, choose 2 and 3; their bitwise AND is 2.

Example 3

nums = [0,0]k = 2budget = 2return = 1

Increase both values to 1. Their bitwise AND is 1.

Constraints

  • 1 <= nums.length <= 100000
  • 1 <= k <= nums.length
  • 0 <= nums[i] <= 1000000000
  • 0 <= budget <= 1000000000
  • The result is returned as a signed 64-bit integer.

Source note: Author-written prompt excerpt retained for source fidelity.

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
class Solution {
    public long maximizeAnd(int[] nums, int k, int budget) {
        // Write your solution here.
    }
}
Checking account…