Skip to content

Commit e6d52b6

Browse files
authored
Egg Dropping algorithms in C++
1 parent e9af63e commit e6d52b6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

egg_dropping.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
5+
int max(int a, int b) { return (a > b)? a: b; }
6+
7+
8+
int eggDrop(int n, int k)
9+
{
10+
11+
if (k == 1 || k == 0)
12+
return k;
13+
14+
15+
if (n == 1)
16+
return k;
17+
18+
int min = INT_MAX, x, res;
19+
20+
21+
for (x = 1; x <= k; x++)
22+
{
23+
res = max(eggDrop(n-1, x-1), eggDrop(n, k-x));
24+
if (res < min)
25+
min = res;
26+
}
27+
28+
return min + 1;
29+
}
30+
31+
32+
int main()
33+
{
34+
int n = 2, k = 10;
35+
cout << "Minimum number of trials in worst case with "
36+
<< n << " eggs and " << k << " floors is "
37+
<< eggDrop(n, k) << endl;
38+
return 0;
39+
}

0 commit comments

Comments
 (0)