Given a positive integer n, return the number of the integers in the range [0, n] whose binary representations do not contain consecutive ones.
Example 1:
Input: n = 5 Output: 5 Explanation: Here are the non-negative integers <= 5 with their corresponding binary representations: 0 : 0 1 : 1 2 : 10 3 : 11 4 : 100 5 : 101 Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
Example 2:
Input: n = 1 Output: 2
Example 3:
Input: n = 2 Output: 3
Constraints:
1 <= n <= 109When 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:
To find all numbers up to a given limit that do not have consecutive ones in their binary form, the brute force approach checks every single number starting from zero up to that limit. For each number, it inspects its binary representation to see if any two ones appear right next to each other.
Here's how the algorithm would work step-by-step:
def find_integers(upper_limit: int) -> int:
valid_number_count = 0
# Iterate through every integer from zero up to the upper limit to evaluate them individually
for current_number in range(upper_limit + 1):
binary_representation = bin(current_number)[2:]
has_consecutive_ones = False
# Scan adjacent character pairs to detect any forbidden consecutive ones in the binary string
for character_index in range(len(binary_representation) - 1):
if binary_representation[character_index] == '1' and binary_representation[character_index + 1] == '1':
has_consecutive_ones = True
break
# Exclude numbers with consecutive ones to ensure only strictly valid integers increase our tally
if not has_consecutive_ones:
valid_number_count += 1
return valid_number_count
Instead of checking every single number one by one, the optimal approach uses the patterns of binary numbers to count valid options efficiently by breaking the limit down piece by piece. It relies on the relationship between position lengths and known patterns of valid sequences to skip massive blocks of numbers all at once.
Here's how the algorithm would work step-by-step:
class Solution:
def find_integers(self, limit_number: int) -> int:
binary_representation = bin(limit_number)[2:]
length_of_binary = len(binary_representation)
fibonacci_sequence = [0] * (length_of_binary + 2)
fibonacci_sequence[0] = 1
fibonacci_sequence[1] = 2
for index in range(2, length_of_binary + 2):
fibonacci_sequence[index] = fibonacci_sequence[index - 1] + fibonacci_sequence[index - 2]
total_valid_count = 0
previous_bit_was_one = False
# Precompute valid counts for lengths to quickly sum valid combinations.
for index, character in enumerate(binary_representation):
power_of_two = length_of_binary - 1 - index
if character == '1':
total_valid_count += fibonacci_sequence[power_of_two]
if previous_bit_was_one:
return total_valid_count - fibonacci_sequence[power_of_two]
previous_bit_was_one = True
else:
previous_bit_was_one = False
# Add one more to include the upper limit number itself if it is valid.
return total_valid_count + 1
| Case | How to Handle |
|---|---|
| Input n equals zero | The algorithm should correctly return 1 because zero itself has no consecutive ones and is less than or equal to zero. |
| Input n equals one | The algorithm should return 2 since both zero and one are valid non-negative integers without consecutive ones. |
| Input n is a power of two minus one, such as 3 or 7, where the binary representation is all ones | The digit DP or bit manipulation approach will correctly terminate at the highest threshold without overcounting. |
| Maximum possible integer limits up to 10 to the 9th power or 32-bit integer limits | Using iterative digit DP or precomputed Fibonacci numbers prevents recursion depth limits and avoids integer overflow during intermediate Fibonacci sums. |
| Inputs with alternating binary bits like 1010 in binary which equals 10 | The state transition in the bit-counting logic will properly validate each bit position without prematurely stopping. |
| Inputs where n has pre-existing consecutive ones in its binary representation | The algorithm must correctly detect the violation, stop branching down invalid paths, and count all valid numbers strictly below that prefix. |