Given N eggs and K floors, the task is to find the minimum number of trials needed in the worst case to find the floor below which all floors are safe. A floor is safe if dropping an egg from it does not break the egg. Please refer n eggs and k floors for more insight.
Input: N = 2, K = 10
The first trial is from 4th floor. Two cases arise after this:
- If egg breaks, we have one egg left so we need three more trials.
- If the egg does not break, the next try is from 7th floor. Again two cases arise.
Notice that if 4th floor is chosen as first floor, 7th as the next floor
and 9 as last floor, the number of trials can never exceed 4.
Input: N = 2, K = 100
Prerequisites: Egg Dropping Puzzle
Approach: Consider this problem in a different way:
Let dp[x][n] is the maximum number of floors that can be checked with given n eggs and x moves.
Then the equation is:
dp[x][n] = dp[x – 1][n – 1] + dp[x – 1][n] + 1
- If the egg breaks, then we can check dp[x – 1][n – 1] floors.
- If the egg doesn’t break, then we can check dp[x – 1][n] + 1 floors.
which means we take 1 move to a floor,
Since we need to cover k floors, dp[x][n] >= k.
dp[x][n] is similar to the number of combinations and it increases exponentially to k
Below is the implementation of the above approach:
Time Complexity: O(NlogK)
Space Complexity: O(N * K)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Egg Dropping Puzzle with 2 Eggs and K Floors
- Puzzle | The Egg Vendor and his Eggs
- Puzzle | Set 35 (2 Eggs and 100 Floors)
- Egg Dropping Puzzle | DP-11
- Puzzle 51| Cheryl’s Birthday Puzzle and Solution
- Puzzle 34 | (Prisoner and Policeman Puzzle)
- Puzzle | 3 Priests and 3 devils Puzzle
- Puzzle 15 | (Camel and Banana Puzzle)
- Puzzle 85 | Chain Link Puzzle
- Puzzle 81 | 100 people in a circle with gun puzzle
- Puzzle 33 | ( Rs 500 Note Puzzle )
- Puzzle 39 | (100 coins puzzle)
- Puzzle 31 | (Minimum cut Puzzle)
- Puzzle 36 | (Matchstick Puzzle)
- Puzzle 29 | (Car Wheel Puzzle)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.