forked from supershivam13/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoney.cpp
44 lines (29 loc) · 769 Bytes
/
money.cpp
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
#include<iostream>
#include<algorithm>
using namespace std;
int indian_currency[] = {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000};
int n = sizeof(indian_currency) / sizeof(int);
bool compare(int a, int b) {
return a <= b;
}
void count_notes(int money) {
// Base Case
if (money == 0) {
return;
}
//Recursive Case
// lower_bound will return an iterator
// Log (T) where T is the type of coins you have!
int largest_idx = lower_bound(indian_currency, indian_currency + n, money, compare) - indian_currency - 1;
int m = indian_currency[largest_idx];
cout << m << " " << endl;
//Recursive Call
count_notes(money - m);
return;
}
int main() {
int money;
cin >> money;
count_notes(money);
return 0 ;
}