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.
publicstaticList<Integer>waiter(List<Integer>number,intq){// Write your code hereList<Integer>primeNumbers=getPrimeNumbers(maxValueStack(number));List<Integer>result=newArrayList<>();Stack<Integer>stackB=newStack<>();Stack<Integer>tempA=newStack<>();for(inti=0;i<q;i++){while(!number.isEmpty()){inttop=number.remove(number.size()-1);if(top%primeNumbers.get(i)==0){stackB.add(top);}else{tempA.add(top);}}while(!stackB.isEmpty()){result.add(stackB.pop());}while(!tempA.isEmpty()){number.add(0,tempA.pop());}}while(!number.isEmpty()){result.add(number.remove(number.size()-1));}returnresult;}publicstaticintmaxValueStack(List<Integer>stack){intmax=-1;Stack<Integer>tempStack=newStack<>();while(!stack.isEmpty()){inttop=stack.remove(stack.size()-1);if(top>max){max=top;}tempStack.add(top);}while(!tempStack.isEmpty()){stack.add(tempStack.pop());}returnmax;}publicstaticList<Integer>getPrimeNumbers(intmax){List<Integer>result=newArrayList<>();result.add(2);for(inti=3;i<=max;i=i+2){result.add(i);}intlast=3;intindex=2;while(last<result.get(result.size()-1)){for(inti=index;i<result.size();i++){if(result.get(i)%last==0){result.remove(i);i--;}}last=result.get(index);index++;}returnresult;}
As written, this problem requires the Sieve of Eratosthenes. In a real interview, I believe the list of primes would be provided as an argument... Implementing the sieve off top is trivia BS.
defget_primes(n):primes=[2]slicing=slice(1,None)num=3whilelen(primes)<n:is_prime=Trueforprimeinprimes[slicing]:ifprime*prime>num:breakifnum%prime==0:is_prime=Falsebreakifis_prime:primes.append(num)num+=2returnprimesdefwaiter(number,q):# get 'q' number of primesprimes=get_primes(q)# initializationanswers=[]nums=number# q iterationsforiinrange(q):stack_a,stack_b=[],[]prime=primes[i]fornuminreversed(nums):ifnum%prime==0:stack_b.append(num)else:stack_a.append(num)answers.extend(reversed(stack_b))nums=stack_aanswers.extend(reversed(nums))returnanswers
So I think the hardest part was trying to find the prime numbers, I eventually found an algorithm called "The Sieve of Eratosthenes" and it worked, but would time out in some cases. Then realized we only need to determine prime numbers up to the maximum value of the numbers. So if the array of prime numbers is smaller than K, well everything just goes into Stack A always, for anything past the prime array.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
As written, this problem requires the Sieve of Eratosthenes. In a real interview, I believe the list of primes would be provided as an argument... Implementing the sieve off top is trivia BS.
C#
So I think the hardest part was trying to find the prime numbers, I eventually found an algorithm called "The Sieve of Eratosthenes" and it worked, but would time out in some cases. Then realized we only need to determine prime numbers up to the maximum value of the numbers. So if the array of prime numbers is smaller than K, well everything just goes into Stack A always, for anything past the prime array.