Related Articles
Egg Dropping Puzzle with 2 Eggs and K Floors
• Difficulty Level : Medium
• Last Updated : 16 Apr, 2020

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.

Examples:

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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)⌉```

## C++

 `// CPP program to find optimal number of trials``// for k floors and 2 eggs.``#include``using` `namespace` `std;`` ` `int` `twoEggDrop(``int` `k)``{``   ``return` `ceil``((-1.0 + ``sqrt``(1 + 8*k))/2.0);``}`` ` `int` `main()``{``   ``int` `k = 100;``   ``cout << twoEggDrop(k);``   ``return` `0; ``}`

## Java

 `// Java program to find ``// optimal number of trials``// for k floors and 2 eggs.``import` `java.io.*;`` ` `class` `GFG ``{``    ``static` `int` `twoEggDrop(``int` `k)``    ``{``        ``return` `(``int``)Math.ceil((-``1.0` `+``                    ``Math.sqrt(``1` `+ ``8` `* k)) / ``2.0``);``    ``}``     ` `    ``// Driver code``    ``public` `static` `void` `main (String[] args)``    ``{``        ``int` `k = ``100``;``        ``System.out.println(twoEggDrop(k));``    ``}``}`` ` `// This code is contributed``// by Mahadev.`

## Python3

 `# Python3 program to find optimal number ``# of trials for k floors and 2 eggs.``import` `math as mt `` ` `def` `twoEggDrop(k):``    ``return` `mt.ceil((``-``1.0` `+` `           ``mt.sqrt(``1` `+` `8` `*` `k)) ``/` `2``)`` ` `# Driver Code``k ``=` `100``print``(twoEggDrop(k))`` ` `# This code is contributed``# by Mohit Kumar`

## C#

 `// C# program to find optimal number ``// of trials for k floors and 2 eggs.``class` `GFG ``{``static` `int` `twoEggDrop(``int` `k)``{``    ``return` `(``int``)System.Math.Ceiling((-1.0 +``                ``System.Math.Sqrt(1 + 8 * k)) / 2.0);``}`` ` `// Driver code``public` `static` `void` `Main ()``{``    ``int` `k = 100;``    ``System.Console.WriteLine(twoEggDrop(k));``}``}`` ` `// This code is contributed``// by mits`

## PHP

 `

Output :
`14`

Time Complexity : O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.80713874,"math_prob":0.9868404,"size":3611,"snap":"2021-04-2021-17","text_gpt3_token_len":1112,"char_repetition_ratio":0.12170779,"word_repetition_ratio":0.14749661,"special_character_ratio":0.32816395,"punctuation_ratio":0.124040924,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9984551,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-21T00:34:23Z\",\"WARC-Record-ID\":\"<urn:uuid:cb5e98d0-29f3-429b-9e07-57cf0161c1de>\",\"Content-Length\":\"102024\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e08dd8e1-2790-47e6-9bee-ccae8aebc9e6>\",\"WARC-Concurrent-To\":\"<urn:uuid:b32eb8b8-06cd-410e-a151-d220db3b38fd>\",\"WARC-IP-Address\":\"23.221.73.72\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/egg-dropping-puzzle-with-2-eggs-and-k-floors/?ref=lbp\",\"WARC-Payload-Digest\":\"sha1:JUQZIUAX2C653AVTZBCGC37YAEELQQRG\",\"WARC-Block-Digest\":\"sha1:65F2MW35XVDMP4NW4E2E5OJHCO3QHNZW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618039491784.79_warc_CC-MAIN-20210420214346-20210421004346-00526.warc.gz\"}"}