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[100005];
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[0]);
int q = Integer.parseInt(nq[1]);
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){
if(first.head == null) break;
primes[i] = pnum;
while(first.head != null){
int number = first.pop();
if(number%pnum == 0)
dpst.push(number);
else
rest.push(number);
}
while(dpst.head != null){
System.out.println(dpst.pop());
}
pnum = nextPrimeNumber(pnum, i, primes);
MyStack temp = rest;
rest = first;
first = temp;
}
while(first.head != null){
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{
Node head;
MyStack(){
head = null;
}
void push(int data){
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}
int pop(){
int data = head.data;
head = head.next;
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[0]=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)
View More Similar Problems
Print in Reverse
Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing
View Solution →Reverse a linked list
Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio
View Solution →Compare two linked lists
You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis
View Solution →Merge two sorted linked lists
This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C
View Solution →Get Node Value
This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t
View Solution →Delete duplicate-value nodes from a sorted linked list
This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -
View Solution →