-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.java
61 lines (53 loc) · 1.26 KB
/
solution.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Scanner;
import java.util.Stack;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int Q = sc.nextInt();
Stack<Integer> A = new Stack<Integer>();
for (int i = 0; i < N; i++) {
A.push(sc.nextInt());
}
int prime = 2;
for (int i = 0; i < Q; i++) {
Stack<Integer> B = new Stack<Integer>();
Stack<Integer> nextA = new Stack<Integer>();
while (!A.empty()) {
int number = A.pop();
if (number % prime == 0) {
B.push(number); //if number is divisible by prime then add the number to B
} else {
nextA.push(number); //if not then add the number to nextA
}
}
printStack(B);
A = nextA;
prime = nextPrime(prime);
}
printStack(A);
sc.close();
}
static void printStack(Stack<Integer> s) {
while (!s.empty()) {
System.out.println(s.pop());
}
}
//function for checking next number is prime or not
static int nextPrime(int begin) {
for (int i = begin + 1;; i++) {
if (isPrime(i)) {
return i;
}
}
}
// function for checking number is prime or not
static boolean isPrime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}