767. Prime Number of Set Bits in Binary Representation
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to count the numbers in a given inclusive range [left, right] that have a prime number of set bits in their binary representation. A "set bit" is a 1 in the binary form of a number.
Solution
Prime Number of Set Bits in Binary Representation in Python
class Solution:
def countPrimeSetBits(self, left: int, right: int) -> int:
# Based on the constraint right <= 10^6, the max number of set bits is low (around 20).
# Primes up to 20 are 2, 3, 5, 7, 11, 13, 17, 19.
# A set provides O(1) average time complexity for lookups.
primes = {2, 3, 5, 7, 11, 13, 17, 19}
ans = 0
# Iterate through each number in the inclusive range [left, right].
for i in range(left, right + 1):
# Count set bits. In Python, bin(i) returns a string like '0b101',
# so we count the occurrences of '1'.
# For Python 3.10+, i.bit_count() is a more efficient alternative.
set_bits_count = bin(i).count('1')
# Check if the count of set bits is in our set of primes.
if set_bits_count in primes:
ans += 1
return ans
# A more concise way using a generator expression:
# return sum(1 for i in range(left, right + 1) if bin(i).count('1') in primes)
Prime Number of Set Bits in Binary Representation in Java
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
class Solution {
public int countPrimeSetBits(int left, int right) {
// Based on the constraint right <= 10^6, the maximum number of bits is around 20 (2^20 > 10^6).
// The primes up to 20 are 2, 3, 5, 7, 11, 13, 17, 19.
// We use a HashSet for O(1) average time complexity for lookups.
Set<Integer> primes = new HashSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19));
int count = 0;
// Iterate through each number in the given range.
for (int i = left; i <= right; i++) {
// Count the number of set bits (1s) in the binary representation of i.
// Integer.bitCount() is a built-in, efficient method for this.
int setBits = Integer.bitCount(i);
// Check if the number of set bits is a prime number.
if (primes.contains(setBits)) {
count++;
}
}
return count;
}
}
Prime Number of Set Bits in Binary Representation in C++
class Solution {
public:
int countPrimeSetBits(int left, int right) {
// Based on the constraint right <= 10^6, the maximum number of bits is around 20 (2^20 > 10^6).
// The primes up to 20 are 2, 3, 5, 7, 11, 13, 17, 19.
// We can use a hash set for O(1) lookup.
std::unordered_set<int> primes = {2, 3, 5, 7, 11, 13, 17, 19};
int count = 0;
// Iterate through each number in the given range.
for (int i = left; i <= right; ++i) {
// Count the number of set bits (1s) in the binary representation of i.
// __builtin_popcount is a GCC/Clang built-in function that is highly efficient.
int setBits = __builtin_popcount(i);
// Check if the number of set bits is a prime number.
if (primes.count(setBits)) {
count++;
}
}
return count;
}
};
Prime Number of Set Bits in Binary Representation in TypeScript
function countPrimeSetBits(left: number, right: number): number {
// Based on the constraint right <= 10^6, the max number of set bits is low (around 20).
// Primes up to 20 are 2, 3, 5, 7, 11, 13, 17, 19.
// A Set provides O(1) average time complexity for lookups.
const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]);
let count = 0;
// Helper function to count set bits in a number's binary representation.
// TypeScript/JavaScript do not have a standard built-in bitCount function.
const countSetBits = (n: number): number => {
let bitCount = 0;
while (n > 0) {
// The expression n & (n - 1) clears the least significant set bit.
n &= (n - 1);
bitCount++;
}
return bitCount;
};
// Iterate through each number in the inclusive range [left, right].
for (let i = left; i <= right; i++) {
const setBits = countSetBits(i);
// Check if the count of set bits is a prime number.
if (primes.has(setBits)) {
count++;
}
}
return count;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.