You are given a positive number n.
Return the smallest number x greater than or equal to n, such that the binary representation of x contains only set bits
Example 1:
Input: n = 5
Output: 7
Explanation:
The binary representation of 7 is "111".
Example 2:
Input: n = 10
Output: 15
Explanation:
The binary representation of 15 is "1111".
Example 3:
Input: n = 3
Output: 3
Explanation:
The binary representation of 3 is "11".
Constraints:
1 <= n <= 1000When 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 method for this problem involves systematically checking each number, starting from one, until we find the one that satisfies our condition. We're essentially going through all the numbers one by one, testing each to see if its binary representation has all bits set up to a certain point.
Here's how the algorithm would work step-by-step:
def find_smallest_number_with_all_set_bits():
number = 1
while True:
binary_representation = bin(number)[2:]
# Determine the highest bit that is set
highest_set_bit_index = len(binary_representation) - 1
all_bits_set = True
# Check if all bits are set up to the highest set bit.
for bit_index in range(highest_set_bit_index + 1):
if binary_representation[highest_set_bit_index - bit_index] == '0':
all_bits_set = False
break
if all_bits_set:
# Found a number with all set bits
return number
number += 1The goal is to find the smallest whole number that contains all the '1' bits from a given number. The trick is to realize we're really just looking for the next power of two that's larger than the input number. This lets us directly calculate the answer without trying a bunch of different numbers.
Here's how the algorithm would work step-by-step:
def smallest_number_with_all_set_bits(given_number):
next_power_of_two = 1
# Find the next power of 2 greater than the given number
while next_power_of_two <= given_number:
next_power_of_two <<= 1
# We must subtract one to set all bits less significant than the MSB
return next_power_of_two| Case | How to Handle |
|---|---|
| Input is zero | Return 0 because zero has no set bits. |
| Input is negative | Return 1 as it is smallest number with all set bits using two's complement representation depending on the specification. |
| Input is very large integer | May need to return Long.MAX_VALUE or a sentinel value signifying an error depending on the specification. |
| Integer Overflow | Ensure the return type is large enough to represent the result, potentially using long or BigInteger in languages like Java and Python. |
| Minimum Integer Value (Integer.MIN_VALUE) | Handle the minimum integer value appropriately based on two's complement representation by setting all bits to 1 using appropriate mask. |
| Input is a power of 2 | Return the next higher power of 2 minus 1. |
| The input has all bits set | Return the input as it is already the smallest number with all bits set. |
| No valid solution exists (e.g. if a specified maximum bit length is too short) | Return -1 or throw an exception indicating that no valid solution can be found. |