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.
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.
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:
b, how many numbers from 1 to N have this bit set?(N / (2^(b+1))) * (2^b) + max(0, (N % (2^(b+1))) - (2^b) + 1).b up to the most significant bit in N.count_set_bits_up_to_N(N):
count = 0.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_lengthcount += num_cycles * (cycle_length // 2) (number of times bit b is set in full cycles)remainder = N % cycle_lengthcount += max(0, remainder - (cycle_length // 2) + 1) (number of times bit b is set in the partial cycle)count.Binary Search Logic:
low = 1, high = 2 * 10^14 (a sufficiently large upper bound, as K can be large).ans = 0.low <= high:
mid = low + (high - low) // 2.count_set_bits_up_to_N(mid) <= K:
ans = mid.low = mid + 1 (try for a larger X).high = mid - 1 (must use a smaller X).ans.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.
mid = (1+3)//2 = 2.
count_set_bits_up_to_N(2) = 2.
2 <= K (3). Yes. ans = 2, low = 3.
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).
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).+1 in max(0, remainder - (cycle_length // 2) + 1) is a common source of error.low and high cover the full possible range of X.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.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Super Egg Drop | Hard | Solve | |
| 4 Keys Keyboard | Medium | Solve | |
| Egg Drop With 2 Eggs and N Floors | Medium | Solve | |
| 2 Keys Keyboard | Medium | Solve | |
| Count the Number of Square-Free Subsets | Medium | Solve |