You are given an integer k and an integer x. The price of a number num is calculated by the count of set bits at positions x, 2x, 3x, etc., in its binary representation, starting from the least significant bit. The following table contains examples of how price is calculated.
| x | num | Binary Representation | Price |
|---|---|---|---|
| 1 | 13 | 000001101 | 3 |
| 2 | 13 | 000001101 | 1 |
| 2 | 233 | 011101001 | 3 |
| 3 | 13 | 000001101 | 1 |
| 3 | 362 | 101101010 | 2 |
The accumulated price of num is the total price of numbers from 1 to num. num is considered cheap if its accumulated price is less than or equal to k.
Return the greatest cheap number.
Example 1:
Input: k = 9, x = 1
Output: 6
Explanation:
As shown in the table below, 6 is the greatest cheap number.
| x | num | Binary Representation | Price | Accumulated Price |
|---|---|---|---|---|
| 1 | 1 | 001 | 1 | 1 |
| 1 | 2 | 010 | 1 | 2 |
| 1 | 3 | 011 | 2 | 4 |
| 1 | 4 | 100 | 1 | 5 |
| 1 | 5 | 101 | 2 | 7 |
| 1 | 6 | 110 | 2 | 9 |
| 1 | 7 | 111 | 3 | 12 |
Example 2:
Input: k = 7, x = 2
Output: 9
Explanation:
As shown in the table below, 9 is the greatest cheap number.
| x | num | Binary Representation | Price | Accumulated Price |
|---|---|---|---|---|
| 2 | 1 | 0001 | 0 | 0 |
| 2 | 2 | 0010 | 1 | 1 |
| 2 | 3 | 0011 | 1 | 2 |
| 2 | 4 | 0100 | 0 | 2 |
| 2 | 5 | 0101 | 0 | 2 |
| 2 | 6 | 0110 | 1 | 3 |
| 2 | 7 | 0111 | 1 | 4 |
| 2 | 8 | 1000 | 1 | 5 |
| 2 | 9 | 1001 | 1 | 6 |
| 2 | 10 | 1010 | 2 | 8 |
Constraints:
1 <= k <= 10151 <= x <= 8When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy is to try every possible number one by one, from the largest possible number down to 1. For each number, we calculate the sum of prices. If the sum of the prices is less than or equal to K, we have found our answer.
Here's how the algorithm would work step-by-step:
def find_maximum_number(k_value):
maximum_possible_number = k_value
# Iterate down from the maximum possible number
for current_number in range(maximum_possible_number, 0, -1):
price_sum = calculate_price_sum(current_number)
# Check if the price sum is within the limit
if price_sum <= k_value:
# This number works so return it immediately
return current_number
# If no number is found, return 0.
return 0
def calculate_price_sum(number):
price = 0
string_number = str(number)
for digit_char in string_number:
digit = int(digit_char)
price += digit
return priceThe most efficient approach involves using binary search to quickly find the largest possible number. Instead of checking every number one by one, this method smartly narrows down the range of possibilities, leading to a faster solution.
Here's how the algorithm would work step-by-step:
def maximum_number(maximum_possible_number, maximum_sum_allowed):
low_number = 0
high_number = maximum_possible_number
while low_number <= high_number:
middle_number = (low_number + high_number) // 2
price_of_middle_number = calculate_price(middle_number)
# Check if middle number's price is within the allowed sum
if price_of_middle_number <= maximum_sum_allowed:
low_number = middle_number + 1
# If price exceeds the limit, reduce the search range
else:
high_number = middle_number - 1
return high_number
def calculate_price(number):
# This function calculates price (e.g., count set bits)
binary_representation = bin(number)[2:]
price = binary_representation.count('1')
return price| Case | How to Handle |
|---|---|
| k = 0 | Return 0 since a number with a digit sum of 0 is the only possible solution. |
| k is a very large number (e.g., close to the maximum integer value) | The solution must handle potential integer overflow issues when constructing the large number; using strings is more robust. |
| k is a small positive number (e.g., 1, 2, 3) | The solution should correctly return the largest single-digit number less than or equal to k. |
| k is greater than the maximum possible sum of digits for a number with the maximum number of digits allowed (e.g., all 9s) | The solution should return a number comprised of nines until cost approaches k, and handle final value appropriately. |
| Resulting number has a very large number of digits exceeding integer representation limits | Represent the resulting number as a string to avoid integer overflow problems. |
| k is negative | Return an appropriate error message or potentially 0 as no positive number can have a negative digit sum. |
| Leading zeros are not allowed in the final number | Ensure that the resulting number does not contain leading zeros unless the number is 0 itself. |
| Memory limits when k is extremely large | Iteratively build the result string to minimize memory usage rather than allocating a large array. |