You are viewing a single comment's thread. Return to all comments →
public static List waiter(List number, int q) {
Stack<Integer> numberStack = new Stack<>(); for(int no : number) { numberStack.push(no); } List<Integer> primeNos = getPrimes(number); List<Integer> answers = new ArrayList<>(); int curr = 0; while(q != 0) { Stack<Integer> a = new Stack<>(); Stack<Integer> b = new Stack<>(); while(!numberStack.isEmpty()) { int top = numberStack.pop(); if(top % primeNos.get(curr) == 0) { b.push(top); } else { a.push(top); } } curr++; numberStack = a; while(!b.isEmpty()) { answers.add(b.pop()); } q--; if(q == 0) { while(!a.isEmpty()) { answers.add(a.pop()); } } } return answers; } public static List<Integer> getPrimes(List<Integer> number){ List<Integer> primeNos = new ArrayList<>(); int no = 2; while(primeNos.size() != number.size()) { boolean flag = false; for(int i=2; i<=Math.sqrt(no); i++) { if(no%i == 0) { flag = !flag; break; } } if(!flag) { primeNos.add(no); } no++; } return primeNos; }
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 →
JAVA Solution. Easy to understand. All test cases passed.
public static List waiter(List number, int q) {