Problem Link:
Difficulty:
Easy-MediumPrerequisites:
Binary SearchProblem:
Find the maximum number x for which the sum of x natural numbers don't exceeds the litres of water available.Explanation:
Given that the first bucket has a capacity of 1 litre, second bucket has capacity of 2 litres and so on. So we just need the maximum number x so that $1+2+3+....+x <=n$, where n is the litres of the water available. Thus, problem is a searching problem, we are just searching for the required x.First approach is we can use linear search to find the value of x but it will exceed the time limit. So, we have to think of a more efficient searching approach.
Next approach is to use the binary search as it is very efficient and very well within the time limit of the problem.
Solution:
Author's solution can be found here.