The following is a description of the instance of this famous puzzle involving 2 eggs and a building with 100 floors.
Suppose that we wish to know which stories in a 100-storey building are safe to drop eggs from, and which will cause the eggs to break on landing. What strategy should be used to drop eggs such that a total number of drops in the worst case is minimized and we find the required floor
We may make a few assumptions:
- An egg that survives a fall can be used again.
- A broken egg must be discarded.
- The effect of a fall is the same for all eggs.
- If an egg breaks when dropped, then it would break if dropped from a higher floor.
- If an egg survives a fall then it would survive a shorter fall.
We strongly recommend you to minimize your browser and try this yourself first
If only one egg is available and we wish to be sure of obtaining the right result, the experiment can be carried out in only one way. Drop the egg from the first-floor window; if it survives, drop it from the second-floor window. Continue upward until it breaks. In the worst case, this method may require 100 droppings.
Suppose 2 eggs are available. What is the least number of egg droppings that are guaranteed to work in all cases?
The problem is not actually to find the critical floor, but merely to decide floors from which eggs should be dropped so that the total number of trials is minimized.
If we use Binary Search Method to find the floor and we start from the 50’th floor, then we end up doing 50 comparisons in the worst case. The worst-case happens when the required floor is 49’th floor.
Optimized Method: The idea is to do optimize the solution using the below equation:
Let us make our first attempt on x'th floor.
If it breaks, we try remaining (x-1) floors one by one.
So in worst case, we make x trials.
If it doesn't break, we jump (x-1) floors (Because we have
already made one attempt and we don't want to go beyond
x attempts. Therefore (x-1) attempts are available),
Next floor we try is floor x + (x-1)
Similarly, if this drop does not break, next need to jump
up to floor x + (x-1) + (x-2), then x + (x-1) + (x-2) + (x-3)
and so on.
Since the last floor to be tried is 100'th floor, sum of
series should be 100 for optimal value of x.
x + (x-1) + (x-2) + (x-3) + .... + 1 = 100
x(x+1)/2 = 100
x = 13.651
Therefore, we start trying from 14'th floor. If Egg breaks on 14th floor
we one by one try remaining 13 floors, starting from 1st floor. If egg doesn't break
we go to 27th floor.
If egg breaks on 27'th floor, we try floors form 15 to 26.
If egg doesn't break on 27'th floor, we go to 39'th floor.
An so on...
The optimal number of trials is 14 in the worst case.
See below for a programming solution for general k eggs and n floors.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!