C Program for Egg Dropping Puzzle - DP-11?

The egg dropping puzzle is a classic dynamic programming problem. Given n floors and m eggs, we need to find the minimum number of drops required to determine the highest floor from which an egg can be dropped without breaking.

There are some important points to remember −

  • When an egg does not break from a given floor, then it will not break for any lower floor also.
  • If an egg breaks from a given floor, then it will break for all upper floors.
  • When an egg breaks, it must be discarded, otherwise we can use it again.

Syntax

int eggTrialCount(int eggs, int floors);

Algorithm

The dynamic programming approach works as follows −

Begin
   define matrix of size [eggs+1, floors+1]
   for i:= 1 to eggs, do
      minTrial[i, 1] := 1
      minTrial[i, 0] := 0
   done
   for j := 1 to floors, do
      minTrial[1, j] := j
   done
   for i := 2 to eggs, do
      for j := 2 to floors, do
         minTrial[i, j] := ?
         for k := 1 to j, do
            res := 1 + max of minTrial[i-1, k-1] and minTrial[i, j-k]
            if res < minTrial[i, j], then minTrial[i,j] := res
         done
      done
   done
   return minTrial[eggs, floors]
End

Example

Here's the complete C implementation of the egg dropping puzzle −

#include <stdio.h>
#define MAX_VAL 9999

int max(int a, int b) {
    return (a > b) ? a : b;
}

int eggTrialCount(int eggs, int floors) {
    int minTrial[eggs+1][floors+1];
    int res, i, j, k;
    
    // Base cases: one trial for first floor, no trial for 0th floor
    for (i = 1; i <= eggs; i++) {
        minTrial[i][1] = 1;
        minTrial[i][0] = 0;
    }
    
    // When we have only one egg, we need j trials for j floors
    for (j = 1; j <= floors; j++)
        minTrial[1][j] = j;
    
    // Fill the rest of the table using optimal substructure
    for (i = 2; i <= eggs; i++) {
        for (j = 2; j <= floors; j++) {
            minTrial[i][j] = MAX_VAL;
            for (k = 1; k <= j; k++) {
                res = 1 + max(minTrial[i-1][k-1], minTrial[i][j-k]);
                if (res < minTrial[i][j])
                    minTrial[i][j] = res;
            }
        }
    }
    
    return minTrial[eggs][floors];
}

int main() {
    int egg = 4, maxFloor = 10;
    printf("Number of eggs: %d<br>", egg);
    printf("Max Floor: %d<br>", maxFloor);
    printf("Minimum number of trials: %d<br>", eggTrialCount(egg, maxFloor));
    return 0;
}
Number of eggs: 4
Max Floor: 10
Minimum number of trials: 4

How It Works

The algorithm uses a 2D DP table where minTrial[i][j] represents the minimum number of trials needed with i eggs and j floors. For each floor k, we consider two cases −

  • Egg breaks: We have i-1 eggs left and need to check floors below k
  • Egg doesn't break: We have i eggs and need to check floors above k

Conclusion

The egg dropping puzzle demonstrates optimal substructure in dynamic programming. With 4 eggs and 10 floors, the minimum number of trials needed is 4, achieved through strategic floor selection.

Updated on: 2026-03-15T10:55:54+05:30

414 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements