You are viewing a single comment's thread. Return to all comments →
public class Solution {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int Q = sc.nextInt(); int[] primes = generate_prime_array(Q); Stack<Integer> stt = new Stack<>(); ArrayList<Integer> st = new ArrayList<>(); for(int i = 0; i < N; i++){ int x = sc.nextInt(); if(x % 2 == 0) st.add(x); else stt.push(x); } for(int j = 1; j < Q; j++){ ArrayList<Integer> ax = new ArrayList<>(); while(!stt.isEmpty()) ax.add(stt.pop()); for(int i = 0; i < ax.size(); i++){ if(ax.get(i) % primes[j] == 0) st.add(ax.get(i)); else stt.push(ax.get(i)); } } for(int c : st) System.out.println(c+ " "); ArrayList<Integer> sb = new ArrayList<>(); while(!stt.isEmpty()) sb.add(0,stt.pop()); for(int c : sb) System.out.println(c+ " "); } private static int[] generate_prime_array(int Q){ int[] result = new int[Q]; ArrayList<Integer> a = new ArrayList<>(); int status = 1; int num = 3; for ( int i = 2; i <=Q;){ for ( int j = 2 ; j <= Math.sqrt(num) ; j++ ){ if ( num%j == 0 ){status = 0;break;} } if (status != 0 ){ a.add(num); i++; } status = 1; num++; } a.add(0,2); for(int i = 0; i < Q; i++) result[i] = a.get(i); return result;
} }
Seems like cookies are disabled on this browser, please enable them to open this website
Waiter
You are viewing a single comment's thread. Return to all comments →
public class Solution {
} }