Magicsheet logo

Maximum Number That Sum of the Prices Is Less Than or Equal to K

Medium
25%
Updated 8/1/2025

Maximum Number That Sum of the Prices Is Less Than or Equal to K

What is this problem about?

The Maximum Number That Sum of the Prices Is Less Than or Equal to K coding problem asks you to find the largest integer X such that the sum of "prices" of all integers from 1 to X (inclusive) is less than or equal to K. The "price" of an integer i is defined as the number of set bits (1s) in its binary representation.

Why is this asked in interviews?

Microsoft and Google use this problem to test a candidate's understanding of binary representations, bit manipulation, and binary search on the answer. It combines the need to efficiently calculate bit counts for ranges of numbers with the monotonic property that a larger X will result in a larger sum of prices.

Algorithmic pattern used

Binary Search on the Answer + Bit Counting Function: The problem asks for the maximum X satisfying a condition. The condition (sum of prices <= K) is monotonic: if sum_prices(X) <= K, then sum_prices(X-1) <= K. This allows us to binary search for X in a large range (e.g., [1, K] or a sufficiently large upper bound like 2*10^14 if K is price).

The core of the solution is an efficient count_set_bits_up_to_N(N) function, which calculates the sum of set bits for all integers from 1 to N. This function can be derived by observing patterns in binary numbers:

  • For a given bit position b, how many numbers from 1 to N have this bit set?
  • This count can be found by (N / (2^(b+1))) * (2^b) + max(0, (N % (2^(b+1))) - (2^b) + 1).
  • Sum this count for all bit positions b up to the most significant bit in N.

count_set_bits_up_to_N(N):

  1. Initialize count = 0.
  2. For each bit position b from 0 up to log2(N) (or 60 for long long in C++):
    • cycle_length = 1 << (b + 1) (e.g., for bit 0, cycle_length = 2; for bit 1, cycle_length = 4)
    • num_cycles = N // cycle_length
    • count += num_cycles * (cycle_length // 2) (number of times bit b is set in full cycles)
    • remainder = N % cycle_length
    • count += max(0, remainder - (cycle_length // 2) + 1) (number of times bit b is set in the partial cycle)
  3. Return count.

Binary Search Logic:

  • low = 1, high = 2 * 10^14 (a sufficiently large upper bound, as K can be large).
  • ans = 0.
  • While low <= high:
    • mid = low + (high - low) // 2.
    • If count_set_bits_up_to_N(mid) <= K:
      • ans = mid.
      • low = mid + 1 (try for a larger X).
    • Else:
      • high = mid - 1 (must use a smaller X).
  • Return ans.

Example explanation

K = 3. We want to find max X such that sum_prices(X) <= 3.

count_set_bits_up_to_N(N) function:

  • count_set_bits_up_to_N(1): 1 (binary 1 has 1 set bit).
  • count_set_bits_up_to_N(2): 1 (binary 1) + 1 (binary 10) = 2.
  • count_set_bits_up_to_N(3): 1 (01) + 1 (10) + 2 (11) = 4.

Binary Search: low = 1, high = (some large val, e.g., 3 for this K), ans = 0.

  1. mid = (1+3)//2 = 2. count_set_bits_up_to_N(2) = 2. 2 <= K (3). Yes. ans = 2, low = 3.

  2. mid = (3+3)//2 = 3. count_set_bits_up_to_N(3) = 4. 4 <= K (3). No. high = 2.

Loop terminates (low=3, high=2). ans = 2. This means the maximum number X for which the sum of prices is <= 3 is 2. (Prices: 1=1, 2=1. Sum = 2 <= 3).

Common mistakes candidates make

  • Inefficient bit counting: A naive loop for each number from 1 to N to count set bits will be O(N log N), too slow for large N. The count_set_bits_up_to_N function needs to be O(log N).
  • Off-by-one errors in bit counting logic: The +1 in max(0, remainder - (cycle_length // 2) + 1) is a common source of error.
  • Incorrect binary search range: Ensure low and high cover the full possible range of X.

Interview preparation tip

For the Math Binary Search Bit Manipulation Dynamic Programming (though here it's more direct math than DP for bit counting) interview pattern, this problem tests both efficient counting of set bits and the application of binary search to find the optimal X. Master the O(log N) bit counting function.

Similar Questions