Skip to content

Commit 58e4476

Browse files
committed
Interview Question :PrimeSum
Signed-off-by: jatin107 <jatingoyal100@gmail.com>
1 parent 771b153 commit 58e4476

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package codingInterview.Games;
2+
3+
/*package whatever //do not write package name here */
4+
5+
import java.util.Scanner;
6+
7+
//Problem Statement link https://practice.geeksforgeeks.org/problems/finger-game/0
8+
//Solution
9+
class CountTheFinger {
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
13+
int t = sc.nextInt();
14+
while (t-- > 0) {
15+
int v = sc.nextInt();
16+
//System.out.println(v);
17+
switch (v % 8) {
18+
case 1:
19+
System.out.println("1");
20+
break;
21+
case 2:
22+
case 0:
23+
System.out.println("2");
24+
break;
25+
case 3:
26+
case 7:
27+
System.out.println("3");
28+
break;
29+
case 4:
30+
case 6:
31+
System.out.println("4");
32+
break;
33+
case 5:
34+
System.out.println("5");
35+
break;
36+
37+
}
38+
}
39+
//Space O(1) Time O(1)
40+
sc.close();
41+
}
42+
43+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package codingInterview.bitManupulation;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
public class PrimeSum {
7+
//link : https://www.interviewbit.com/problems/prime-sum/
8+
static private ArrayList<Integer> primesum(int n) {
9+
boolean isPrime[] = new boolean[n + 1];
10+
Arrays.fill(isPrime, Boolean.TRUE);
11+
ArrayList<Integer> numL = new ArrayList<>();
12+
isPrime[0] = false;
13+
isPrime[1] = false;
14+
15+
for (int i = 2; i * i <= n; i++) {
16+
if (isPrime[i]) {
17+
// Update all multiples of p
18+
for (int j = i * 2; j <= n; j += i)
19+
isPrime[j] = false;
20+
}
21+
}
22+
23+
for (int i = 2; i <= n; i++) {
24+
if (isPrime[i] && isPrime[n - i]) {
25+
numL.add(i);
26+
numL.add(n - i);
27+
return numL;
28+
}
29+
}
30+
// System.out.println(numL);
31+
return numL;
32+
//space O(n) time O(n)
33+
34+
}
35+
36+
public static void main(String[] args) {
37+
System.out.println(primesum(1048574));
38+
System.out.println(primesum(378));
39+
//System.out.println(primeSum(4));
40+
}
41+
42+
}
43+
44+

0 commit comments

Comments
 (0)