#### CONCEPTS USED:

Dynamic programming

#### DIFFICULTY LEVEL:

Hard

#### PROBLEM STATEMENT`(`

SIMPLIFIED`)`

:

Himanshu visited a building with N floors and he carried with him k eggs, he found out that the eggs are rotten, so he plays a trick he wants to know the maximum floor from which the egg can be dropped without breaking the egg.

- An egg that survives the fall can be used again.
- A broken egg cannot be used again.

You know Himanshu is very lazy, help him find the minimum number of moves required to find the answer.- If egg breaks when dropped from some floor then it will break if dropped from any higher floor.
- If egg does not break when dropped from some floor then it will not break if dropped from any lower floor.

#### For Example :

```
2
30 4
6 2
5
3
```

**See original problem statement here**

#### SOLVING APPROACH:

**Recursion**: try dropping an egg from each floor from 1 to n and calculate the minimum number of dropping needed in worst case.

`Base cases`

â€“

>Eggs â€“ 1, floors â€“ x: play safe and drop from floor 1, if egg does not break then drop from floor 2 and so on. So in worst case x times an egg needs to be dropped to find the solution.

>Floors = 0: No trials are required.

>Floors = 1: 1 trails is required.For rest of the case, if an egg is dropped from xth floor then there are only 2 outcomes which are possible. Either egg will break OR egg will not break.

>If Egg breaks â€“ check the floors lower than x. So problem is reduced is`n-1`

eggs and`x-1`

floors.

>If egg does not break â€“ check the floors higher than x floors with all the n eggs are remaining. So problem is reduced to n eggs and k-x floors.

```
getDrops (k,n) =
1 + Min(x = 1,2,â€¦.n) [(drops(k-1, n-1), drops(k, n-x)]
```

**Time Complexity**: 2^k

Now look at this recursion tree.We are solving many subproblems several times with the help of websites to learn programming.

#### Dynamic Programming:

**Bottom-up:**

Solve it in bottom up manner, means start from the smallest sub problem possible (here it is 0 eggs 0 floors) and solve it. Store the result in some temporary storage.

Recursive equation will be same as above. Start solving from smallest sub problem and move towards final problem. Use the temporary result being stored instead of solving the sub problems again.

See the code for more understanding.

*

Time Complexity: nk^2**

[TABS_R id=2168]

#### EFFICIENT SOLUTION:

How many floors we can cover with

`x`

trials?When we drop an egg, two cases arise.

- If egg breaks, then we are left with
`x-1`

trials and`n-1`

eggs.- If egg does not break, then we are left with
`x-1`

trials and n eggsLet

`mf(x, n)`

be the maximum number of floors

that we can cover with x trials and n eggs. From above

two cases, we can write.

`mf(x, n)`

=`mf(x-1, n-1)`

+`mf(x-1, n)`

+ 1

For all x >= 1 and n >= 1Base cases :

We can’t cover any floor with 0 trials or 0 eggs

`mf(0, n)`

=`0`

`mf(x, 0)`

=`0`

Since we need to cover k floors,

`mf(x, n)`

>=`k`

———-(1)

`maxFloors(x, n)`

= ∑xCi

1 <= i From above two equations, we can say.

∑xCj >=`k`

1 <= i <= n

Basically we need to find minimum value of x

that satisfies above inequality. We can find

such x usingBinary Search.

[TABS_R id=2169]

[forminator_quiz id=2170]