Prime Number of Set Bits in Binary Representation
LeetCode 767 | Difficulty: Easyβ
EasyProblem Descriptionβ
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.
- For example, `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 <= 10^6`
- `0 <= right - left <= 10^4`
Topics: Math, Bit Manipulation
Approachβ
Bit Manipulationβ
Operate directly on binary representations. Key operations: AND (&), OR (|), XOR (^), NOT (~), shifts (<<, >>). XOR is especially useful: a ^ a = 0, a ^ 0 = a.
Finding unique elements, power of 2 checks, subset generation, toggling flags.
Mathematicalβ
Look for mathematical patterns or formulas. Consider: modular arithmetic, GCD/LCM, prime factorization, combinatorics, or geometric properties.
Problems with clear mathematical structure, counting, number properties.
Solutionsβ
Solution 1: C# (Best: 82 ms)β
| Metric | Value |
|---|---|
| Runtime | 82 ms |
| Memory | 25.8 MB |
| Date | 2022-01-19 |
public class Solution {
public int CountPrimeSetBits(int left, int right) {
List<int> primes = new List<int>() {2, 3, 5, 7, 11, 13, 17, 19};
int count = 0;
for (int i = left; i <= right; i++)
{
int bits = CountSetBits(i);
if(primes.Contains(bits)) count++;
}
return count;
}
private int CountSetBits(int n)
{
int count = 0;
while(n > 0)
{
if((n&1) == 1) count++;
n = n >> 1;
}
return count;
}
}
π 1 more C# submission(s)
Submission (2022-01-19) β 97 ms, 25.5 MBβ
public class Solution {
public int CountPrimeSetBits(int left, int right) {
bool[] primes = new bool[33];
primes[0] = false; primes[1] = false;
primes[2] = true; primes[3] = true;
for (int i = 4; i <= 32; i++)
{
primes[i] = true;
}
for (int i = 2; i*i <= 32; i++)
{
if(primes[i])
{
for (int j = i*2; j <= 32; j=j+i)
{
primes[j] = false;
}
}
}
int count = 0;
for (int i = left; i <= right; i++)
{
int bits = CountSetBits(i);
if(primes[bits]) count++;
}
return count;
}
private int CountSetBits(int n)
{
int count = 0;
while(n > 0)
{
if((n&1) == 1) count++;
n = n >> 1;
}
return count;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Bit Manipulation | $O(n) or O(1)$ | $O(1)$ |
Interview Tipsβ
- Start by clarifying edge cases: empty input, single element, all duplicates.
- LeetCode provides 1 hint(s) for this problem β try solving without them first.
π‘ Hints
Hint 1: Write a helper function to count the number of set bits in a number, then check whether the number of set bits is 2, 3, 5, 7, 11, 13, 17 or 19.