I recently faced a problem in an interview in which I was given a size of a crocodile and an array representing the size of fishes in a pond. The crocodile can eat the fishes less than its size and every time it eats that fish its size gets increases by the size of that fish. We can perform two types operations on the array any number of times.
- Remove any fish from the pond
- Add fish of any size to the pond
Given the initial size of the crocodile and the array of fishes in the pond, we have to find the minimum no. of such operations so as to empty the array representing the pond.
For eg -
The initial size of the crocodile is 20 and the array of fishes is
[10,15,20,30,100]
In this case the crocodile eats all the fishes except the 100 and its size becomes 85 so one of the solution might be removing the fish with size 100. So the output will be 1.
What is the best possible algorithm to implement the above problem, can it be solved recursively?