Given 2 eggs and k floors, find the minimum number of trials needed in worst case. This problem is a specific case of n eggs and k floors.
Input : k = 10 Output : 4 We first try from 4-th floor. Two cases arise, (1) If egg breaks, we have one egg left so we need three more trials. (2) If egg does not break, we try next from 7-th floor. Again two cases arise. We can notice that if we choose 4th floor as first floor, 7-th as next floor and 9 as next of next floor, we never exceed more than 4 trials. Input : k = 100 Output : 14
What is the worst case number of trials if we have only one egg?
The answer is k. We will be trying from 1st floor, then 2nd, then 3rd and in worst case, the egg breaks from top floor.
What would be our first floor that we try if we have two eggs?
We can notice that if our answer is x, then the first floor that we try has to be floor number x. Because in worst case if egg breaks, we have only one egg left and we have to try every floor from 1 to x-1. So total trials become 1 + (x – 1).
What would be our second floor that we try if egg does not break in first attempt?
The next floor that we try has to be x + (x – 1) because our optimal answer is x and if egg breaks from floor number x + (x-1) we have to linearly try from floor number x+1 to x-2.
Can we generalize it?
If first egg has not broken so far, then the i-th trial has to be from floor number x + (x – 1) + … + (x – i – 1).
How many floors we can cover with x trials?
We can observe from above that we can cover x + (x – 1) + (x – 2) …. + 2 + 1 floors with x trials. The value of this expression is x * (x + 1) / 2.
What is the optimal x for a given k?
From above, we know,
x * (x + 1)/2 >= k The optimal value of x can be written as, ⌈((-1 + √(1+8k))/2)⌉
Time Complexity : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Puzzle | Set 35 (2 Eggs and 100 Floors)
- Puzzle | The Egg Vendor and his Eggs
- Egg Dropping Puzzle | DP-11
- Eggs dropping puzzle (Binomial Coefficient and Binary Search Solution)
- Eggs dropping puzzle | Set 2
- Puzzle 34 | (Prisoner and Policeman Puzzle)
- Puzzle 51| Cheryl’s Birthday Puzzle and Solution
- Puzzle | 3 Priests and 3 devils Puzzle
- Puzzle 15 | (Camel and Banana Puzzle)
- Puzzle 24 | (10 Coins Puzzle)
- Puzzle 27 | (Hourglasses Puzzle)
- Puzzle 28 | (Newspaper Puzzle)
- Puzzle 29 | (Car Wheel Puzzle)
- Puzzle 31 | (Minimum cut Puzzle)
- Puzzle 33 | ( Rs 500 Note Puzzle )
- Puzzle 36 | (Matchstick Puzzle)
- Puzzle 38 | (Tic Tac Toe Puzzle)
- Puzzle 39 | (100 coins puzzle)
- Puzzle 81 | 100 people in a circle with gun puzzle
- Puzzle 85 | Chain Link 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 firstname.lastname@example.org. 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.