Given two integers left
and right
, return the count of numbers in the inclusive range [left, right]
having a prime number of set bits in their binary representation.
Recall that the number of set bits an integer has is the number of 1
's present when written in binary.
21
written in binary is 10101
, which has 3
set bits.Example 1:
Input: left = 6, right = 10 Output: 4 Explanation: 6 -> 110 (2 set bits, 2 is prime) 7 -> 111 (3 set bits, 3 is prime) 8 -> 1000 (1 set bit, 1 is not prime) 9 -> 1001 (2 set bits, 2 is prime) 10 -> 1010 (2 set bits, 2 is prime) 4 numbers have a prime number of set bits.
Example 2:
Input: left = 10, right = 15 Output: 5 Explanation: 10 -> 1010 (2 set bits, 2 is prime) 11 -> 1011 (3 set bits, 3 is prime) 12 -> 1100 (2 set bits, 2 is prime) 13 -> 1101 (3 set bits, 3 is prime) 14 -> 1110 (3 set bits, 3 is prime) 15 -> 1111 (4 set bits, 4 is not prime) 5 numbers have a prime number of set bits.
Constraints:
1 <= left <= right <= 106
0 <= right - left <= 104
When 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 approach to this problem involves examining every number within a given range. For each number, we determine if the count of '1' bits in its binary form is a prime number, and then count how many times this happens.
Here's how the algorithm would work step-by-step:
def prime_number_of_set_bits(left, right):
number_of_primes = 0
for number in range(left, right + 1):
binary_representation = bin(number)[2:]
set_bits_count = binary_representation.count('1')
# Check if the number of set bits is prime
if set_bits_count > 1:
is_prime = True
# Optimization: only need to check up to the square root
for i in range(2, int(set_bits_count**0.5) + 1):
if set_bits_count % i == 0:
is_prime = False
break
# Only increment if number of set bits is indeed prime
if is_prime:
number_of_primes += 1
return number_of_primes
The problem requires us to count numbers within a range where the number of '1's in their binary form is a prime number. The most efficient approach involves directly counting the '1's in the binary representation of each number and checking if that count is a prime number using a precomputed list.
Here's how the algorithm would work step-by-step:
def prime_number_of_set_bits_in_binary_representation(left, right):
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}
count = 0
for number in range(left, right + 1):
# Count the number of set bits
set_bits_count = bin(number).count('1')
# Check if the count of set bits is a prime number
if set_bits_count in prime_numbers:
# Increment the total count
count += 1
return count
Case | How to Handle |
---|---|
Low bound is 0 | The problem should explicitly state whether zero should be included in the range or not; assume it's inclusive and count the bits of zero appropriately. |
High bound is a very large number (close to integer limit) | Check for potential integer overflow issues when counting set bits for large numbers, especially if using bitwise operations without considering the sign bit. |
Low bound is equal to the high bound | If low equals high, check if the number of set bits in its binary representation is prime, and return a list containing 1 if it is, otherwise 0. |
Low bound is negative | Clarify with the interviewer about handling negative numbers since the number of set bits in a negative number's two's complement representation can be different. |
Large interval between low and high | Consider optimizing the bit counting function for speed, as the function might be called many times; precompute primes within a certain range. |
Interval contains only even numbers | Test how the prime set bit count behaves when many numbers have even parity (might affect overall performance). |
Interval contains only powers of 2 | Powers of two have exactly one set bit, so count the number of powers of two in the interval and check if 1 is prime (it isn't). |
Integer overflow in calculating number of set bits | Ensure intermediate results when calculating set bits (especially using bitwise operators and shifts on potentially large values) do not lead to integer overflow, using appropriate data types. |