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;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Waiter
You are viewing a single comment's thread. Return to all comments →