Skip to content

Instantly share code, notes, and snippets.

@prabhakaran9397
Last active March 25, 2018 19:58
Show Gist options
  • Save prabhakaran9397/f231052ac746ddd24c548f328a97a8c4 to your computer and use it in GitHub Desktop.
Save prabhakaran9397/f231052ac746ddd24c548f328a97a8c4 to your computer and use it in GitHub Desktop.
Egg Dropping Problem

K - Number of eggs - given N - Number of floors - given A - Minimum number of attempts - to find

Egg dropping puzzle follows binomial expansion.

1 egg - N floors - A attempts

N = A/1!

2 eggs - N floors - A attempts

N = A/1! + A(A-1)/2!

3 eggs - N floors - A attempts

N = A/1! + A(A-1)/2! + A(A-1)(A-2)/3!

K eggs - N floors - A attempts

N = A/1! + A(A-1)/2! + ... + A(A-1)(A-2)...(A-K-1)/K!

Now for K eggs, N floors, A attempts, we have to stop computing when it exceeds N - O(K)

Linear Search

Starting from A as 1, we can run the computation and see if it reaches N then for A as 2 ... Finally we will get A which exceeds N - O(N)

Binary Search

Since this finding A is an increasing sequence, we can go for binary search. We have to find A which is immediately greater than N - O(log N)

Hence total running time: O(K log N)

Here is the implementation: Code Reference: Reference

@Deepak16031
Copy link

What is the answer when #floor =2 and #egg=1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment