Skip to content

Commit 876fab0

Browse files
add 3233
1 parent 9b62138 commit 876fab0

File tree

3 files changed

+107
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+107
-0
lines changed

Diff for: paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3233 | [Find the Count of Numbers Which Are Not Special](https://leetcode.com/problems/find-the-count-of-numbers-which-are-not-special/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3233.java) | | Medium | Math
34
| 3232 | [Find if Digit Game Can Be Won](https://leetcode.com/problems/find-if-digit-game-can-be-won/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3232.java) | | Easy |
45
| 3226 | [Number of Bit Changes to Make Two Integers Equal](https://leetcode.com/problems/number-of-bit-changes-to-make-two-integers-equal/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3226.java) | | Easy |
56
| 3224 | [Minimum Array Changes to Make Differences Equal](https://leetcode.com/problems/minimum-array-changes-to-make-differences-equal/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3224.java) | | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.Arrays;
4+
5+
public class _3233 {
6+
public static class Solution1 {
7+
/**
8+
* credit: https://leetcode.com/problems/find-the-count-of-numbers-which-are-not-special/solutions/5546339/sieve-of-eratosthenes/
9+
* In order for a number to be special, it must be a square of a prime number;
10+
* so we use sieve algorithm to find all prime numbers up to Math.sqrt(r);
11+
* since Math.sqrt() method rounds down, we'll use Math.sqrt(r) + 1 as the bound for isPrime array
12+
* and check if the number is within the range of [l, r] later.
13+
*/
14+
public int nonSpecialCount(int l, int r) {
15+
int limit = (int) Math.sqrt(r);
16+
boolean[] isPrime = new boolean[limit + 1];
17+
Arrays.fill(isPrime, true);
18+
isPrime[0] = false;
19+
isPrime[1] = false;
20+
for (int i = 2; i * i < isPrime.length; i++) {
21+
if (isPrime[i]) {
22+
//below for loop is key to construct isPrime[] array:
23+
//we start j from i * i, as long as j is within boundary, we increase j by i each time
24+
//i.e. if i = 2, j starts from 4, then 6, 8, 10, 12
25+
//if i = 3, j starts from 9, then 12, 15, 18, 21
26+
for (int j = i * i; j < isPrime.length; j += i) {
27+
isPrime[j] = false;
28+
}
29+
}
30+
}
31+
32+
//now count special numbers
33+
int special = 0;
34+
for (int i = Math.max(2, (int) Math.sqrt(l)); i < isPrime.length; i++) {
35+
if (isPrime[i]) {
36+
int square = i * i;
37+
if (square <= r && square >= l) {
38+
special++;
39+
}
40+
}
41+
}
42+
//total number of numbers in this range
43+
int totalCount = r - l + 1;
44+
//minus the special ones
45+
return totalCount - special;
46+
}
47+
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3233;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3233Test {
10+
private static _3233.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3233.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.nonSpecialCount(5, 7));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(11, solution1.nonSpecialCount(4, 16));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(14, solution1.nonSpecialCount(1, 16));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(2, solution1.nonSpecialCount(1, 2));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(3, solution1.nonSpecialCount(1, 3));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(3, solution1.nonSpecialCount(1, 4));
45+
}
46+
47+
@Test
48+
public void test7() {
49+
assertEquals(77, solution1.nonSpecialCount(1, 81));
50+
}
51+
52+
@Test
53+
public void test8() {
54+
assertEquals(433, solution1.nonSpecialCount(1, 441));
55+
}
56+
57+
}

0 commit comments

Comments
 (0)