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.
Problem description is a bit of a hustle to understand and worded unnecessarily complicated.
A simple solution with Modern C++ and horrendous code otherwise.
vector<int>primes{2,3,5,7,11,13};boolisPrime(intn){for(intp:primes){if(p>n/2){returntrue;}if(n%p==0){returnfalse;}}returntrue;}intgetNthPrime(intn){if(n<=primes.size()){returnprimes[n-1];}// If there are more then one prime is missing calculate those// In this question this should not be neededif(n+1>primes.size()){getNthPrime(n-1);}intcandidate=primes.back()+2;while(!isPrime(candidate)){candidate+=2;}primes.push_back(candidate);returncandidate;}vector<int>waiter(vector<int>numbers,intq){vector<int>answers;for(inti=1;i<q+1;i++){intprime=getNthPrime(i);autoit=stable_partition(numbers.begin(),numbers.end(),[prime](intn){returnn%prime;});if(i%2==1){answers.insert(answers.end(),it,numbers.end());}else{autorIt=make_reverse_iterator(it);answers.insert(answers.end(),numbers.rbegin(),rIt);}numbers.erase(it,numbers.end());}if(q%2==1){answers.insert(answers.end(),numbers.begin(),numbers.end());}else{answers.insert(answers.end(),numbers.rbegin(),numbers.rend());}returnanswers;}
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 →
Problem description is a bit of a hustle to understand and worded unnecessarily complicated.
A simple solution with Modern C++ and horrendous code otherwise.