-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeSum.java
More file actions
44 lines (35 loc) · 1.13 KB
/
Copy pathPrimeSum.java
File metadata and controls
44 lines (35 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package codingInterview.bitManupulation;
import java.util.ArrayList;
import java.util.Arrays;
public class PrimeSum {
//link : https://www.interviewbit.com/problems/prime-sum/
static private ArrayList<Integer> primesum(int n) {
boolean isPrime[] = new boolean[n + 1];
Arrays.fill(isPrime, Boolean.TRUE);
ArrayList<Integer> numL = new ArrayList<>();
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
// Update all multiples of p
for (int j = i * 2; j <= n; j += i)
isPrime[j] = false;
}
}
for (int i = 2; i <= n; i++) {
if (isPrime[i] && isPrime[n - i]) {
numL.add(i);
numL.add(n - i);
return numL;
}
}
// System.out.println(numL);
return numL;
//space O(n) time O(n)
}
public static void main(String[] args) {
System.out.println(primesum(1048574));
System.out.println(primesum(378));
//System.out.println(primeSum(4));
}
}