Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 0762. Prime Number of Set Bits in Binary Representation

[LeetCode Link](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/)

Difficulty: Easy
Topics: Math, Bit Manipulation
Acceptance Rate: 73.2%

## Hints

### Hint 1

Think about what tools you have for counting bits in a number. Once you know the bit count, you just need a fast way to check a property of that count. What property? And how large can that count actually get given the constraints?

### Hint 2

Since numbers go up to 10^6 (roughly 2^20), the number of set bits is at most 20. You only need to determine which values from 0 to 20 are prime. Consider pre-computing or hardcoding that small set of primes so the check is O(1).

### Hint 3

Use a language built-in (like `bits.OnesCount` in Go) to count set bits efficiently, then check membership in the set {2, 3, 5, 7, 11, 13, 17, 19}. A bitmask where bit `i` is set when `i` is prime gives you a constant-time lookup: `(1 << bitCount) & mask != 0`.

## Approach

1. **Identify the prime set:** Since `right <= 10^6 < 2^20`, any number in the range has at most 20 set bits. The primes up to 20 are {2, 3, 5, 7, 11, 13, 17, 19}.

2. **Build a bitmask for fast lookup:** Encode those primes into a single integer where bit `p` is set for each prime `p`:
```
mask = (1<<2) | (1<<3) | (1<<5) | (1<<7) | (1<<11) | (1<<13) | (1<<17) | (1<<19)
```
This equals `665772`.

3. **Iterate and count:** For each number `i` in `[left, right]`:
- Count its set bits using `bits.OnesCount(uint(i))`.
- Check if `(mask >> bitCount) & 1 == 1`.
- If so, increment the result counter.

**Example walkthrough (left=6, right=10):**
- 6 = 110 → 2 bits → 2 is prime ✓
- 7 = 111 → 3 bits → 3 is prime ✓
- 8 = 1000 → 1 bit → 1 is not prime ✗
- 9 = 1001 → 2 bits → 2 is prime ✓
- 10 = 1010 → 2 bits → 2 is prime ✓
- Result: 4

## Complexity Analysis

Time Complexity: O(n) where n = right - left + 1. Each number requires O(1) for popcount and O(1) for the prime check.

Space Complexity: O(1). Only a fixed-size bitmask and a counter are used.

## Edge Cases

- **left == right:** Only one number to check. The loop still works correctly.
- **All numbers have prime bit counts:** Every number in the range qualifies (e.g., left=6, right=7).
- **No numbers have prime bit counts:** For example, powers of two greater than 2 have exactly 1 set bit, which is not prime.
- **Maximum range (right = 10^6):** The solution handles this efficiently since popcount and bitmask lookup are both O(1).
- **Numbers with 0 or 1 set bits:** 0 is not prime, 1 is not prime. These are correctly excluded by the bitmask.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
number: "0762"
frontend_id: "762"
title: "Prime Number of Set Bits in Binary Representation"
slug: "prime-number-of-set-bits-in-binary-representation"
difficulty: "Easy"
topics:
- "Math"
- "Bit Manipulation"
acceptance_rate: 7322.7
is_premium: false
created_at: "2026-02-21T03:06:58.567143+00:00"
fetched_at: "2026-02-21T03:06:58.567143+00:00"
link: "https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/"
date: "2026-02-21"
---

# 0762. Prime Number of Set Bits in Binary Representation

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 <= 106`
* `0 <= right - left <= 104`
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "math/bits"

// countPrimeSetBits counts numbers in [left, right] whose popcount is prime.
// Since right <= 10^6 < 2^20, popcount is at most 20. We encode primes up to
// 20 into a bitmask for O(1) prime checking per number.
func countPrimeSetBits(left int, right int) int {
// Bitmask with bit i set if i is prime, for i in 0..19
// Primes: 2, 3, 5, 7, 11, 13, 17, 19
const primeMask = 1<<2 | 1<<3 | 1<<5 | 1<<7 | 1<<11 | 1<<13 | 1<<17 | 1<<19

count := 0
for i := left; i <= right; i++ {
if primeMask>>(bits.OnesCount(uint(i)))&1 == 1 {
count++
}
}
return count
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import "testing"

func TestCountPrimeSetBits(t *testing.T) {
tests := []struct {
name string
left int
right int
expected int
}{
{
name: "example 1: 6 to 10",
left: 6,
right: 10,
expected: 4,
},
{
name: "example 2: 10 to 15",
left: 10,
right: 15,
expected: 5,
},
{
name: "edge case: single number with prime bit count",
left: 3,
right: 3,
expected: 1, // 3 = 11 -> 2 set bits, 2 is prime
},
{
name: "edge case: single number with non-prime bit count",
left: 1,
right: 1,
expected: 0, // 1 = 1 -> 1 set bit, 1 is not prime
},
{
name: "edge case: power of two (1 set bit, not prime)",
left: 8,
right: 8,
expected: 0, // 8 = 1000 -> 1 set bit
},
{
name: "edge case: left equals right at minimum",
left: 1,
right: 1,
expected: 0,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := countPrimeSetBits(tt.left, tt.right)
if result != tt.expected {
t.Errorf("countPrimeSetBits(%d, %d) = %d, want %d", tt.left, tt.right, result, tt.expected)
}
})
}
}