Skip to content

Commit 580f9b8

Browse files
add 762
1 parent ff19040 commit 580f9b8

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Your ideas/fixes/algorithms are more than welcome!
2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
2525
|763|[Partition Labels](https://leetcode.com/problems/partition-labels/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_763.java) | O(n) | O(1) | |Medium|
26+
|762|[Prime Number of Set Bits in Binary Representation](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_762.java) | O(n) | O(1) | |Easy|
2627
|760|[Find Anagram Mappings](https://leetcode.com/problems/find-anagram-mappings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_760.java) | O(n^2) | O(1) | |Easy|
2728
|755|[Reach a Number](https://leetcode.com/problems/reach-a-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_755.java) | O(n) | O(1) | |Medium| Math
2829
|750|[Number Of Corner Rectangles](https://leetcode.com/problems/number-of-corner-rectangles/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_750.java) | O((m*n)^2) | O(1) | |Medium|
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 762. Prime Number of Set Bits in Binary Representation
5+
*
6+
* Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation.
7+
* (Recall that the number of set bits an integer has is the number of 1s present when written in binary.
8+
* For example, 21 written in binary is 10101 which has 3 set bits. Also, 1 is not a prime.)
9+
10+
Example 1:
11+
12+
Input: L = 6, R = 10
13+
Output: 4
14+
15+
Explanation:
16+
6 -> 110 (2 set bits, 2 is prime)
17+
7 -> 111 (3 set bits, 3 is prime)
18+
9 -> 1001 (2 set bits , 2 is prime)
19+
10->1010 (2 set bits , 2 is prime)
20+
21+
Example 2:
22+
23+
Input: L = 10, R = 15
24+
Output: 5
25+
26+
Explanation:
27+
10 -> 1010 (2 set bits, 2 is prime)
28+
11 -> 1011 (3 set bits, 3 is prime)
29+
12 -> 1100 (2 set bits, 2 is prime)
30+
13 -> 1101 (3 set bits, 3 is prime)
31+
14 -> 1110 (3 set bits, 3 is prime)
32+
15 -> 1111 (4 set bits, 4 is not prime)
33+
34+
Note:
35+
36+
L, R will be integers L <= R in the range [1, 10^6].
37+
R - L will be at most 10000.
38+
*/
39+
40+
public class _762 {
41+
public static class Solution1 {
42+
public int countPrimeSetBits(int L, int R) {
43+
int count = 0;
44+
for (int i = L; i <= R; i++) {
45+
if (hasPrimeNumberSetBits(i)) {
46+
count++;
47+
}
48+
}
49+
return count;
50+
}
51+
52+
private boolean hasPrimeNumberSetBits(int num) {
53+
int k = getSetBits(num);
54+
if (k <= 1) {
55+
return false;
56+
}
57+
for (int i = 2; i * i <= k; i++) {
58+
if (k % i == 0) {
59+
return false;
60+
}
61+
}
62+
return true;
63+
}
64+
65+
private int getSetBits(int n) {
66+
int bits = 0;
67+
while (n != 0) {
68+
bits++;
69+
n &= (n - 1);
70+
}
71+
return bits;
72+
}
73+
}
74+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._762;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _762Test {
10+
private static _762.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _762.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.countPrimeSetBits(6, 10));
20+
}
21+
}

0 commit comments

Comments
 (0)