Given a list of n objects, write a function that outputs the minimum set of numbers that sum to at least K. FOLLOW UP: can you beat O(n ln n)?
The minimum set will be a set with 1 element. Don’t we just have to traverse the array and find an element i.e. >= K.
Otherwise for O(nlgn), I understand we have to first sort the array and then we can find pair or triplets which sum >=k.
What if we don’t find such a combination and have to go for bigger sets won’t this problem be same as N sum problem?
Here’s a linear algorithm that uses linear-time median finding as a subroutine:
Each recursive call is done on a list at most half the size of A and all other steps take at most linear time, so this algorithm takes linear time overall.