Egg dropping Puzzle in C++24 Mar 2025 | 8 min read The Egg Dropping Puzzle problem is a well-known problem, which needs dynamic programming to solve optimally. The case of this well-known Puzzle that involves N = 2 eggs and a structure with K = 36 floors is described below. Consider the situation where we want to determine which floors in a 36-story building are safe to drop eggs from and which will result in the eggs breaking when they land. We assume the following:
There is only one way to experiment if there is only one egg available, and we want to ensure that we get the desired outcome. If the egg survives, drop it from the second-floor window after dropping it from the first-floor window. Continue going up until it snaps. In the worst-case scenario, this technique might need 36 droppings. Finding the key floor is not the issue at hand; rather, the issue is deciding which floors eggs should be dropped from in order to reduce the overall Number of attempts. In this post, we'll talk about a general solution to the problem of 'N' eggs and 'K' floors. Approach 1: Egg Dropping Puzzle using Recursion:
What is the worst-case scenario?The user is guaranteed to be on the threshold floor in the worst-case situation. For instance, if we have "1" egg and "K" floors, we shall drop the egg from the first floor until it breaks, assuming it does on the "Kth" floor. Hence, the Number of trials to provide us with certainty is "K". Optimal Substructure:When we drop an egg from floor x, there can be two cases (1) The egg breaks (2) The egg doesn't break.
We only pick a maximum of two cases because we need to reduce the Number of trials in the worst-case scenario. We take into account the maximum of the two situations above for each level and select the floor that results in the fewest trials. Example:Let us take an example to illustrate the Egg Dropping Puzzle in C++ using Recursion. Output: ![]() Complexity Analysis: Time Complexity: The time complexity of this program is exponential because overlapping subproblems are present. Auxiliary Space: The space complexity is O(1) because no data structure was used to store values. Approach 2: Egg Dropping Puzzle using Dynamic Programming:This problem has the Overlapping Subproblems attribute because the same subproblems are called repeatedly. Therefore, the Egg Dropping Puzzle contains both characteristics of a dynamic programming issue. Recomputations of the same subproblems can be avoided, like with other common Dynamic Programming (DP) problems, by building a temporary array eggFloor[][] from the bottom up. Formally, for completing DP[i][j] state, where 'i' is the number of eggs and 'j' is the number of floors: For each floor 'x', we must travel from '1' to 'j' and discover at least: Example:Let us take another example to illustrate the Egg Dropping Puzzle in C++ using Dynamic Programming. Output: ![]() Complexity Analysis: Time Complexity: The time complexity is O(N * K2). As we use a nested for loop 'k^2' times for each egg Auxiliary Space: The time complexity is O(N * K). A 2-D array of size n*k is used for storing elements. Approach 3: Egg Dropping Puzzle using Memoization:In the first recursive technique, we can utilize a 2D dp table to record the outcomes of overlapping subproblems, which will assist in lowering the time complexity from exponential to quadratic. dp table state -> dp[i][j], where 'i' denotes the quantity of eggs and 'j' denotes the quantity of floors: Follow the below steps to solve the problem:
Example:Let us take another example to illustrate the Egg Dropping Puzzle in C++ using Memoization. Output: ![]() Complexity Analysis: Time Complexity: O(n*k*k) where n is Number of eggs and k is Number of floors. Auxiliary Space: O(n*k) as 2D memoisation table has been used. Approach: 4To solve the problem follow the below concept: It has been mentioned before how to use a method with O(N * K2), where dp[N][K] = 1 + max(dp[N - 1][i - 1], dp[N][K - i]) for i in 1...K. We thoroughly investigated every option in that strategy. dp[m][x] denotes the maximum Number of levels that can be checked with x eggs and m moves. Follow the below steps to solve the problem:
Example:Let us take another example to illustrate the Egg Dropping Puzzle in C++. Output: ![]() Complexity Analysis: Time Complexity: The time complexity is O(N * K). Auxiliary Space: The space complexity is O(N * K). Approach 5: Egg Dropping Puzzle using space-optimized DP:In this method, the method can be improved to 1-D DP because we only need the data from the previous row to calculate the current row of the DP table and nothing more. To solve the issue, follow the instructions listed below:
Example:Let us take another example to illustrate the Egg Dropping Puzzle in C++ using space-optimized DP. Output: ![]() Time Complexity: The time complexity is (N * log K). Auxiliary Space: The space complexity is O(N). Next TopicPack-indexing-in-cpp |
Carmichael Numbers in C++
In number theory, Carmichael numbers (also known as pseudoprimes) are composite numbers that exhibit prime-like behavior with respect to Fermat’s Little Theorem. Fermat's theorem states that for a prime number p and any integer a (where a is not divisible by p), the following condition...
10 min read
Std::is_pointer Template in C++
In this article, we will discuss the std::is_pointer template in C++ with its syntax, parameters, and examples. Before discussing the is_pointer template, we must know about the pointers. What are Pointers? The memory address of an object is stored in a variable called a pointer. Pointers are...
3 min read
std::exponential_distribution in C++
Introduction The main topic of this article is the std::exponential_distribution class in C++, which is quite a useful tool from the Standard Library to form random numbers with exponential distribution. This distribution finds an application whenever the time between events in a Poisson process is of interest,...
6 min read
Range-based for loop in C++
In this topic, we will discuss the range-based for loop in the C++ programming language. The C++ language introduced a new concept of the range-based for loop in C++11 and later versions, which is much better than the regular For loop. A range-based for loop...
5 min read
Difference between Strategy and State Patterns in C++
C++ has two behavioral design patterns: the State Pattern and the Strategy Pattern. However, their functions are distinct. An object can choose from a variety of algorithms at runtime to change its behavior due to the Strategy Pattern. Disregarding the internal state of the object, its...
11 min read
FizzBuzz Problem in C++
The FizzBuzz problem is one of the classic coding challenges frequently utilized to screen a programmer for general knowledge of programming languages, control structures, and problem-solving abilities in technical interviews. Although it may seem simple, it would demonstrate whether we know the basics, including loops, conditionals,...
6 min read
What is a monad in C++?
Introduction: A monad in C++ (borrowed from functional programming languages like Haskell) represents a design pattern that allows the chaining of operations while managing values, contexts, or side effects in a controlled manner. In C++, monads are not natively built-in, but the concept can be applied by...
7 min read
Crossword Puzzle Solver in C++
A is a program designed to automatically fill a given crossword grid using a predefined list of words. Problem Statement: A crossword puzzle consists of: A grid of cells (usually square or rectangular), some of which may be blacked out. A word list that contains the words to...
10 min read
Belphegor Number in C++
Belphegor numbers are an interesting number concept in the field of number theory and are typically characterized by the properties that give it such distinction. Numbers that are linked to the demon Belphegor have digits that adhere to a particular pattern. In this article, we...
6 min read
Luhn Algorithm in C++
Understanding the The Luhn algorithm, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula for validating identification numbers, such as credit card numbers, IMEI numbers, and many more. It is well-implanted in vast financial and telecommunication systems because it...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India




