Problem#
Given two integers left
and right
, return _thecount of numbers in the
inclusive range _[left, right]
having aprime 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.
- For example,
21
written in binary is 10101
, which has 3
set bits.
Examples#
Example 1#
1
2
3
4
5
6
7
8
9
|
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#
1
2
3
4
5
6
7
8
9
10
|
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 <= 10^6
0 <= right - left <= 10^4
Solution#
Intuition#
For each number in the range [left, right], count the number of set bits (1s) in its binary representation. If this count is a prime number, increment the answer. Since the maximum number of set bits for numbers up to 10^6 is 20, we can precompute all primes up to 20.
Approach#
- Precompute the set of primes up to 20.
- For each number in [left, right], count its set bits (using built-in or bitwise method).
- If the count is in the set of primes, increment the answer.
- Return the answer.
Code#
C++#
1
2
3
4
5
6
7
8
9
10
11
12
|
#include <vector>
#include <unordered_set>
using namespace std;
int countPrimeSetBits(int left, int right) {
unordered_set<int> primes = {2,3,5,7,11,13,17,19};
int ans = 0;
for (int x = left; x <= right; ++x) {
int cnt = __builtin_popcount(x);
if (primes.count(cnt)) ++ans;
}
return ans;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
func countPrimeSetBits(left int, right int) int {
primes := map[int]bool{2:true,3:true,5:true,7:true,11:true,13:true,17:true,19:true}
ans := 0
for x := left; x <= right; x++ {
cnt := 0
y := x
for y > 0 {
cnt += y & 1
y >>= 1
}
if primes[cnt] {
ans++
}
}
return ans
}
|
Java#
1
2
3
4
5
6
7
8
9
10
11
12
|
import java.util.*;
class Solution {
public int countPrimeSetBits(int left, int right) {
Set<Integer> primes = Set.of(2,3,5,7,11,13,17,19);
int ans = 0;
for (int x = left; x <= right; ++x) {
int cnt = Integer.bitCount(x);
if (primes.contains(cnt)) ++ans;
}
return ans;
}
}
|
Kotlin#
1
2
3
4
5
6
7
8
9
|
fun countPrimeSetBits(left: Int, right: Int): Int {
val primes = setOf(2,3,5,7,11,13,17,19)
var ans = 0
for (x in left..right) {
val cnt = Integer.bitCount(x)
if (cnt in primes) ans++
}
return ans
}
|
Python#
1
2
3
4
5
6
7
8
|
def countPrimeSetBits(left: int, right: int) -> int:
primes = {2,3,5,7,11,13,17,19}
ans = 0
for x in range(left, right+1):
cnt = bin(x).count('1')
if cnt in primes:
ans += 1
return ans
|
Rust#
1
2
3
4
5
6
7
8
9
10
11
|
fn count_prime_set_bits(left: i32, right: i32) -> i32 {
let primes = [2,3,5,7,11,13,17,19];
let mut ans = 0;
for x in left..=right {
let cnt = x.count_ones();
if primes.contains(&(cnt as i32)) {
ans += 1;
}
}
ans
}
|
TypeScript#
1
2
3
4
5
6
7
8
9
10
11
12
13
|
export function countPrimeSetBits(left: number, right: number): number {
const primes = new Set([2,3,5,7,11,13,17,19]);
let ans = 0;
for (let x = left; x <= right; ++x) {
let cnt = 0, y = x;
while (y > 0) {
cnt += y & 1;
y >>= 1;
}
if (primes.has(cnt)) ans++;
}
return ans;
}
|
Complexity#
- ⏰ Time complexity:
O(N log M)
, where N = right - left + 1, M is the maximum value (up to 10^6), log M for counting set bits.
- 🧺 Space complexity:
O(1)
(ignoring output and the set of small primes).