forked from sukritishah15/DS-Algo-Point
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinDenominations_GreedyAlgo.c
54 lines (43 loc) · 1004 Bytes
/
MinDenominations_GreedyAlgo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// C program to find minimum
// number of denominations
#include <stdio.h>
#define COINS 9
#define MAX 20
// All denominations of Indian Currency
int coins[COINS] = { 1, 2, 5, 10, 20,
50, 100, 200, 2000 };
void findMin(int cost)
{
int coinList[MAX] = { 0 };
int i, k = 0;
for (i = COINS - 1; i >= 0; i--) {
while (cost >= coins[i]) {
cost -= coins[i];
// Add coin in the list
coinList[k++] = coins[i];
}
}
for (i = 0; i < k; i++) {
// Print
printf("%d ", coinList[i]);
}
return;
}
int main(void)
{
// input value
int n = 93;
printf("Following is minimal number"
"of change for %d: ",
n);
findMin(n);
return 0;
}
/*
Output:
Following is minimal number of change
for 93: 50 20 20 2 1
Complexity Analysis:
Time Complexity: O(V).
Auxiliary Space: O(1) as no additional space is used.
*/