This is a premium problem. We're working on making it available for free soon.
Use these hints if you're stuck. Try solving on your own first.
Binary search the answer.
To check if an answer is possible, for the ith hen check how far right it can go after ensuring that it eats the leftmost uneaten grain.
If the last grain is eaten in this process, the answer is possible.
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Problems involving binary search on the answer and greedy assignment patterns are common in FAANG-style interviews. While this exact problem may vary, similar questions testing binary search with feasibility checks appear frequently.
Binary search works because the problem has a monotonic property: if all grains can be eaten in time T, they can also be eaten in any time greater than T. This allows us to efficiently search the minimum feasible time instead of checking every possibility.
The optimal approach combines sorting, binary search, and a greedy feasibility check. After sorting hens and grains, we binary search the minimum time and verify if hens can cover all grains within that time using a two-pointer greedy strategy.
The problem mainly uses arrays along with sorting and a two-pointer traversal. No advanced data structures are required, but careful pointer management is important for the greedy coverage check.