Skip to content

Commit ff4c45e

Browse files
authored
Added task 762.
1 parent def52c3 commit ff4c45e

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0701_0800.s0762_prime_number_of_set_bits_in_binary_representation;
2+
3+
// #Easy #Math #Bit_Manipulation
4+
5+
public class Solution {
6+
public int countPrimeSetBits(int left, int right) {
7+
int count = 0;
8+
boolean[] notPrime = new boolean[33];
9+
notPrime[0] = true;
10+
notPrime[1] = true;
11+
sieve(notPrime);
12+
for (int i = left; i <= right; i++) {
13+
int num = i;
14+
int setBits = 0;
15+
while (num > 0) {
16+
num = num & (num - 1);
17+
setBits++;
18+
}
19+
if (!notPrime[setBits]) {
20+
count++;
21+
}
22+
}
23+
return count;
24+
}
25+
26+
private void sieve(boolean[] notPrime) {
27+
for (int i = 2; i <= 32; i++) {
28+
if (!notPrime[i]) {
29+
for (int j = 2 * i; j <= 32; j += i) {
30+
notPrime[j] = true;
31+
}
32+
}
33+
}
34+
}
35+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
762\. Prime Number of Set Bits in Binary Representation
2+
3+
Easy
4+
5+
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_.
6+
7+
Recall that the **number of set bits** an integer has is the number of `1`'s present when written in binary.
8+
9+
* For example, `21` written in binary is `10101`, which has `3` set bits.
10+
11+
**Example 1:**
12+
13+
**Input:** left = 6, right = 10
14+
15+
**Output:** 4
16+
17+
**Explanation:**
18+
19+
6 -> 110 (2 set bits, 2 is prime)
20+
7 -> 111 (3 set bits, 3 is prime)
21+
8 -> 1000 (1 set bit, 1 is not prime)
22+
9 -> 1001 (2 set bits, 2 is prime)
23+
10 -> 1010 (2 set bits, 2 is prime)
24+
4 numbers have a prime number of set bits.
25+
26+
**Example 2:**
27+
28+
**Input:** left = 10, right = 15
29+
30+
**Output:** 5
31+
32+
**Explanation:**
33+
34+
10 -> 1010 (2 set bits, 2 is prime)
35+
11 -> 1011 (3 set bits, 3 is prime)
36+
12 -> 1100 (2 set bits, 2 is prime)
37+
13 -> 1101 (3 set bits, 3 is prime)
38+
14 -> 1110 (3 set bits, 3 is prime)
39+
15 -> 1111 (4 set bits, 4 is not prime)
40+
5 numbers have a prime number of set bits.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= left <= right <= 10<sup>6</sup></code>
45+
* <code>0 <= right - left <= 10<sup>4</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0701_0800.s0762_prime_number_of_set_bits_in_binary_representation;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void countPrimeSetBits() {
11+
assertThat(new Solution().countPrimeSetBits(6, 10), equalTo(4));
12+
}
13+
14+
@Test
15+
void countPrimeSetBits2() {
16+
assertThat(new Solution().countPrimeSetBits(10, 15), equalTo(5));
17+
}
18+
}

0 commit comments

Comments
 (0)