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.
I'm not sure why this is rated as hard. A moment of thought shows that obviously you want to choose k numbers that are as close as possible, leading to an obvious O(nlogn + nk) solution, and then once you've written that it's trivial to see how to re-group the math you've written to eliminate the factor of k so that the largest few test cases don't time out.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Angry Children 2
You are viewing a single comment's thread. Return to all comments →
I'm not sure why this is rated as hard. A moment of thought shows that obviously you want to choose k numbers that are as close as possible, leading to an obvious O(nlogn + nk) solution, and then once you've written that it's trivial to see how to re-group the math you've written to eliminate the factor of k so that the largest few test cases don't time out.