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.

**Examples:**

Input:N = 2, K = 10

Output:4

Explanation:

The first trial is from 4^{th}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

Output:14

**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:

- 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.

**dp[x][n] = dp[x – 1][n – 1] + dp[x – 1][n] + 1**

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:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum number ` `// of trials needed in the worst case ` `// with n eggs and k floors ` `int` `eggDrop(` `int` `n, ` `int` `k) ` `{ ` ` ` `vector<vector<` `int` `> > dp(k + 1, vector<` `int` `>(n + 1, 0)); ` ` ` ` ` `int` `x = 0; ` ` ` ` ` `// Fill all the entries in table using ` ` ` `// optimal substructure property ` ` ` `while` `(dp[x][n] < k) { ` ` ` ` ` `x++; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `dp[x][i] = dp[x - 1][i - 1] + dp[x - 1][i] + 1; ` ` ` `} ` ` ` ` ` `// Return the minimum number of moves ` ` ` `return` `x; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 2, k = 36; ` ` ` ` ` `cout << eggDrop(n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum number ` `// of trials needed in the worst case ` `// with n eggs and k floors ` `static` `int` `eggDrop(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `dp[][] = ` `new` `int` `[k + ` `1` `][n + ` `1` `]; ` ` ` ` ` `int` `x = ` `0` `; ` ` ` ` ` `// Fill all the entries in table using ` ` ` `// optimal substructure property ` ` ` `while` `(dp[x][n] < k) ` ` ` `{ ` ` ` ` ` `x++; ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `dp[x][i] = dp[x - ` `1` `][i - ` `1` `] + ` ` ` `dp[x - ` `1` `][i] + ` `1` `; ` ` ` `} ` ` ` ` ` `// Return the minimum number of moves ` ` ` `return` `x; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `2` `, k = ` `36` `; ` ` ` ` ` `System.out.println( eggDrop(n, k)); ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the approach ` ` ` `# Function to return the minimum number ` `# of trials needed in the worst case ` `# with n eggs and k floors ` `def` `eggDrop(n, k): ` ` ` `dp ` `=` `[[` `0` `for` `i ` `in` `range` `(n ` `+` `1` `)] ` `for` ` ` `j ` `in` `range` `(k ` `+` `1` `)] ` ` ` ` ` `x ` `=` `0` `; ` ` ` ` ` `# Fill all the entries in table using ` ` ` `# optimal substructure property ` ` ` `while` `(dp[x][n] < k): ` ` ` ` ` `x ` `+` `=` `1` `; ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` `dp[x][i] ` `=` `dp[x ` `-` `1` `][i ` `-` `1` `] ` `+` `\ ` ` ` `dp[x ` `-` `1` `][i] ` `+` `1` `; ` ` ` ` ` `# Return the minimum number of moves ` ` ` `return` `x; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `2` `; k ` `=` `36` `; ` ` ` ` ` `print` `(eggDrop(n, k)); ` ` ` `# This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum number ` `// of trials needed in the worst case ` `// with n eggs and k floors ` `static` `int` `eggDrop(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `[,]dp = ` `new` `int` `[k + 1, n + 1]; ` ` ` ` ` `int` `x = 0; ` ` ` ` ` `// Fill all the entries in table using ` ` ` `// optimal substructure property ` ` ` `while` `(dp[x, n] < k) ` ` ` `{ ` ` ` ` ` `x++; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `dp[x, i] = dp[x - 1, i - 1] + ` ` ` `dp[x - 1, i] + 1; ` ` ` `} ` ` ` ` ` `// Return the minimum number of moves ` ` ` `return` `x; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `n = 2, k = 36; ` ` ` ` ` `Console.WriteLine(eggDrop(n, k)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

8

**Time Complexity:** O(NlogK)

**Space Complexity:** O(N * K)

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.

## Recommended Posts:

- Egg Dropping Puzzle with 2 Eggs and K Floors
- Puzzle | Set 35 (2 Eggs and 100 Floors)
- Puzzle | The Egg Vendor and his Eggs
- Egg Dropping Puzzle | DP-11
- 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 34 | (Prisoner and Policeman Puzzle)
- Puzzle 36 | (Matchstick Puzzle)
- Puzzle 38 | (Tic Tac Toe Puzzle)
- Puzzle 39 | (100 coins puzzle)
- Puzzle 51| Cheryl’s Birthday Puzzle and Solution
- Puzzle 81 | 100 people in a circle with gun puzzle
- Puzzle 85 | Chain Link Puzzle
- Puzzle | Elevator Puzzle
- Puzzle | 3 Priests and 3 devils 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 contribute@geeksforgeeks.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.