# Waiter

### Problem Statement :

```You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the  prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in  from top to bottom in answers. In the next iteration, do the same with the values in stack . Once the required number of iterations is complete, store the remaining values in Ai  in answers , again from top to bottom. Return the answers  array.

Function Description

Complete the waiter function in the editor below.

waiter has the following parameters:

int number[n]: the numbers on the plates
int q: the number of iterations
Returns

int[n]: the numbers on the plates after processing
Input Format

The first line contains two space separated integers, n and q.
The next line contains n space separated integers representing the initial pile of plates, i.e., A.```

### Solution :

```                            ```Solution in C :

In C ++ :

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <set>
#include <map>
using namespace std;

int m, n;

const int mycount = 10000;
vector<int> prime_results;
vector<int> sieve(int n)
{
set<int> primes;
vector<int> vec;

primes.insert(2);

for(int i=3; i<=n ; i+=2)
{
primes.insert(i);
}

int p=*primes.begin();
vec.push_back(p);
primes.erase(p);

int maxRoot = sqrt(*(primes.rbegin()));

while(primes.size() > 0)
{
if(p > maxRoot)
{
while(primes.size() > 0)
{
p=*primes.begin();
vec.push_back(p);
primes.erase(p);
}
break;
}

int i = p*p;
int temp = (*(primes.rbegin()));

while(i<=temp)
{
primes.erase(i);
i += p;
i += p;
}

p=*primes.begin();
vec.push_back(p);
primes.erase(p);
}

return vec;
}

void prepare() {
prime_results = sieve(mycount);
}

int a;
vector<int> thearray[mycount];
void process() {
int i, j, k, q, l;

prepare();
scanf("%d %d", &n, &q);

for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

for (i = 0; i < n; i++) {
for (j = 0; j < q; j++) {
k = prime_results[j];
if (a[i] % k == 0) {
thearray[j].push_back(a[i]);
break;
}
}

if (j == q) {
thearray[j].push_back(a[i]);
}
}

for (i = 0; i < q; i++) {
if ((i & 1) == 0) {
for (j = 0; j < thearray[i].size(); j++) {
printf("%d\n", thearray[i][j]);
}
} else {
for (j = thearray[i].size() - 1; j >= 0;j--) {
printf("%d\n", thearray[i][j]);
}
}
}

if (q & 1) {
for (j = 0; j < thearray[i].size(); j++) {
printf("%d\n", thearray[i][j]);
}
} else {
for (j = thearray[i].size() - 1; j >= 0;j--) {
printf("%d\n", thearray[i][j]);
}
}

}

int main() {
process();

return 0;
}

In Java  :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String nq[] = sc.nextLine().split(" ");
int n = Integer.parseInt(nq);
int q = Integer.parseInt(nq);
String nums[] = sc.nextLine().split(" ");
int pnum = 2;
int primes[] = new int[q];
MyStack first = new MyStack();
MyStack rest = new MyStack();
MyStack dpst = new MyStack();
for(int i = 0; i < nums.length; ++i){
first.push(Integer.parseInt(nums[i]));
}
for(int i = 0; i < q; ++i){
primes[i] = pnum;
int number = first.pop();
if(number%pnum == 0)
dpst.push(number);
else
rest.push(number);
}
System.out.println(dpst.pop());
}
MyStack temp = rest;
rest = first;
first = temp;
}
System.out.println(first.pop());
}
}
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;
}
}

class MyStack{
MyStack(){
}
void push(int data){
Node newNode = new Node(data);
}

int pop(){
return data;
}
}
class Node{
int data;
Node next;
Node(int d){
data = d;
next = null;
}
}

In C :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX 50005
int top=-1;
int pri[MAX],q,b[MAX],stack[MAX];
void push(int sym){
stack[++top]=sym;
}
int pop(){
return stack[top--];
}
int isempty(){
if(top==-1)
return 1;
return 0;
}
void prime(){

pri=2;
int count,c,i=3;
for ( count = 1 ; count < 1250 ;  )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
pri[count]=i;
count++;
}
i++;
}
}
int main() {

prime();
int n,i,a[MAX];
scanf("%d %d",&n,&q);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int k=0;
for(i=0;i<n;i++)
{
if(a[i]%pri[k]==0)
printf("%d\n",a[i]);
else
push(a[i]);
}

k++;
int s;

while(k<q)
{
int z=0;
while(!isempty())
{
b[z++]=pop();
}
for(i=0;i<z;i++)
{
if(b[i]%pri[k]==0)
printf("%d\n",b[i]);
else
push(b[i]);
}
k++;
}
int x=0,c[MAX];
if(k==q)
{
while(!isempty())
{
c[x++]=pop();
}
for(i=x-1;i>=0;i--)
printf("%d\n",c[i]);
}

return 0;
}

In Python3 :

a,b = map(int, input().split(" "))
nums = list(map(int, input().split(" ")))
def prime(x):
x = x + 1
primes = []
for a in range(1, 10000):
for b in range(2, a):
if a % b == 0: break
else:
primes.append(a)
if len(primes) == x:
return primes[1:]

primes = prime(b)
L = []
for i in primes:
temp_nums = []
L2 = []
for j in range(len(nums)):
num = nums[j]
if num % i == 0:
L2 += [num]
else:
temp_nums += [num]
L += L2
nums = list(reversed(temp_nums))

for i in L:
print(i)
for i in list(reversed(nums)):
print(i)```
```

## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your \$inOrder* func

## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.