Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
Egg Dropping Puzzle with 2 Eggs and K Floors
Next article icon

Egg Dropping Puzzle with 2 Eggs and K Floors

Last Updated : 27 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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


 


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<bits/stdc++.h> 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
<?php // PHP program to find optimal number  // of trials for k floors and 2 eggs.  function twoEggDrop($k) {     return ceil((-1.0 +             sqrt(1 + 8 * $k)) / 2.0); }  // Driver Code $k = 100; echo twoEggDrop($k);  // This code is contributed  // by Akanksha Rai 
JavaScript
<script>  // JavaScript program to find optimal number of trials // for k floors and 2 eggs.  function twoEggDrop(k) {    return Math.ceil((-1.0 + Math.sqrt(1 + 8*k))/2.0); }  var k = 100; document.write( twoEggDrop(k));  </script>  

Output: 

14


Time Complexity : O(log(k))

Space complexity : O(1)
 


Next Article
Egg Dropping Puzzle with 2 Eggs and K Floors

K

kartik
Improve
Article Tags :
  • DSA

Similar Reads

    Eggs dropping puzzle | Set 2
    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 =
    8 min read
    Egg Dropping Puzzle | DP-11
    You are given n identical eggs and you have access to a k-floored building from 1 to k.There exists a floor f where 0 <= f <= k such that any egg dropped from a floor higher than f will break, and any egg dropped from or below floor f will not break. There are a few rules given below:An egg th
    15+ min read
    Eggs dropping puzzle (Binomial Coefficient and Binary Search Solution)
    Given n eggs and k floors, find the minimum number of trials needed in 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 see n eggs and k floors. for complete statements Example Input : n = 2, k = 10 Output : 4 We
    13 min read
    Coin change problem with limited coins
    Given three integers n, k, target, and an array of coins[] of size n. Find if it is possible to make a change of target cents by using an infinite supply of each coin but the total number of coins used must be exactly equal to k.Examples:Input: n = 5, k = 3, target = 11, coins = {1, 10, 5, 8, 6}Outp
    12 min read
    Rat in a Maze with multiple steps or jump allowed
    You are given an n × n maze represented as a matrix. The rat starts from the top-left corner (mat[0][0]) and must reach the bottom-right corner (mat[n-1][n-1]).The rat can move forward (right) or downward.A 0 in the matrix represents a dead end, meaning the rat cannot step on that cell.A non-zero va
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences