We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Stacks
  4. Waiter

Waiter

Problem
Submissions
Leaderboard
Discussions
Editorial

You are a waiter at a party. There is a pile of numbered plates. Create an empty array. At each iteration, , remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible by the prime number. If it is, stack it in pile . Otherwise, stack it in stack . Store the values in from top to bottom in . 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 in , again from top to bottom. Return the array.

Example


An abbreviated list of primes is . Stack the plates in reverse order.


Begin iterations. On the first iteration, check if items are divisible by .

Move elements to .

On the second iteration, test if elements are divisible by .

Move elmements to .

And on the third iteration, test if elements are divisible by .

Move elmements to .

All iterations are complete, so move the remaining elements in , from top to bottom, to .

. Return this list.

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, and .
The next line contains space separated integers representing the initial pile of plates, i.e., .

Constraints



Sample Input 0

5 1
3 4 7 6 5

Sample Output 0

4
6
3
7
5

Explanation 0

Initially:

= [3, 4, 7, 6, 5]<-TOP

After 1 iteration (divide by 2, the 1st prime number):

= [5, 7, 3]<-TOP

= [6, 4]<-TOP

Move elements to .

All iterations are complete, so move elements to .

.

Sample Input 1

5 2
3 3 4 4 9

Sample Output 1

4
4
9
3
3

Explanation 1

Initially:

= [3, 3, 4, 4, 9]<-TOP

After iteration (divide by 2):

= [9, 3, 3]<-TOP

= [4, 4]<-TOP

Move to .

After iteration (divide by 3):

= []<- TOP

= [3, 3, 9]<-TOP

Move elements to .

There are no values remaining in .

Author

dcod5

Difficulty

Medium

Max Score

75

Submitted By

33746

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website