https://www.hackerrank.com/challenges/waiter
https://www.hackerrank.com/rest/contests/master/challenges/waiter/hackers/acetseng/download_solution
https://www.hackerrank.com/rest/contests/master/challenges/waiter/hackers/mprashant/download_solution
You are a waiter at a party. There are stacked plates. Each plate has a number written on it. You start picking up the plates from the top one by one and check whether the number written on the plate is divisible by (a prime). The starting value of is . If the number is divisible, you stack that plate separately with other divisible plates. If not, you stack that plate separately with the other plates that are not divisible by .
In the next iteration, the value of changes to the next prime number after . You check the plates from the pile that were not divisible by in the last iteration. You repeat this process a number of times. By doing this process times, you end up getting different piles of plates. The plates that are not divisible by the prime (which is our last iteration), from the last pile of plates. Say you have (it is clear that is either or ) different piles of plates. Starting from the first pile, print the number written on the plate while removing plates from a pile in the same order as described above. Do this process for all the piles. Print one value in a single line.
http://www.martinkysel.com/hackerrank-waiter-solution/
First of all, generate primes using Sieve, or copy-paste a list of pre-generated primes. After each iteration the order of the remaining plates is reversed (you put the last on top and once finished you start from the top). That means that the solution has to have a stack, not a queue.
generated_primes = [...]def solveWaiter(N, Q, numbers): stack = [] for value in numbers: if value % generated_primes[0] != 0: stack.append(value) else: print value for prime_idx in xrange(1, Q): leftover = [] while stack: value = stack.pop() if value % generated_primes[prime_idx] != 0: leftover.append(value) else: print value stack = leftover for value in stack: print valuedef main(): N, Q = map(int, raw_input().split()) numbers = map(int, raw_input().split()) solveWaiter(N, Q, numbers)https://www.hackerrank.com/rest/contests/master/challenges/waiter/hackers/acetseng/download_solution
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int N=sc.nextInt(), Q=sc.nextInt();
Stack<Integer> pile=new Stack<Integer>();
for(int i=0; i<N; i++)
pile.push(sc.nextInt());
List<Stack<Integer>> piles=new ArrayList<Stack<Integer>>();
piles.add(pile);
process(piles, Q);
output(piles);
}
public static void process(List<Stack<Integer>> piles, int Q) {
List<Integer> primes=getQPrimes( Q );
for(int p: primes){
Stack<Integer> stack=piles.remove(piles.size()-1);
Stack<Integer> divisable=new Stack<Integer>();
Stack<Integer> notdivzab=new Stack<Integer>();
while(!stack.empty()) {
int t=stack.pop();
if(t%p==0)
divisable.push(t);
else
notdivzab.push(t);
}
piles.add(divisable);
piles.add(notdivzab);
}
}
public static void output(List<Stack<Integer>> piles) {
StringBuilder sb=new StringBuilder();
for(Stack<Integer> stack: piles) {
while(!stack.empty())
sb.append(stack.pop()+"\n");
}
System.out.println(sb);
}
public static List<Integer> getQPrimes(int q) {
List<Integer> res=new ArrayList<Integer>();
BigInteger bi = BigInteger.valueOf(1);
for(int i=0; i<q; i++){
bi=bi.nextProbablePrime();//\\
res.add(bi.intValue());
}
return res;
}
https://www.hackerrank.com/rest/contests/master/challenges/waiter/hackers/mprashant/download_solution
static int nextPrimeNumber(int num,int idx,int[] primes){
if(num == 2) return 3;
while(true){
num = num + 2;
boolean isPrime = true;
for(int i = 0; i <= idx; ++i){
if(num%primes[i] == 0){
isPrime = false;
break;
}
}
if(isPrime) break;
}
return num;
}