-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoin Change - Minimum number of coins(TLE).cpp
More file actions
72 lines (58 loc) · 1.56 KB
/
Copy pathCoin Change - Minimum number of coins(TLE).cpp
File metadata and controls
72 lines (58 loc) · 1.56 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//{ Driver Code Starts
//Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//Complete this function
class Solution
{
public:
//Function to find the minimum number of coins to make the change
//for value using the coins of given denominations.
long long solve(int coins[],int numberOfCoins,int val,int curr){
if(curr>val) return 1e8;
if(curr==val) return 0;
long long mini=INT_MAX;
for(int i=0;i<numberOfCoins;i++){
mini=min(mini,1+solve(coins,numberOfCoins,val,curr+coins[i]));
}
return mini;
}
long long minimumNumberOfCoins(int coins[],int numberOfCoins,int value)
{
// your code here
int ans=solve(coins,numberOfCoins,value,0);
if(ans>=1e8) return -1;
return ans;
}
};
//{ Driver Code Starts.
int main() {
//taking total count of testcases
int testcases;
cin>>testcases;
while(testcases--)
{
//taking value
int value;
cin>>value;
//taking number of coins
int numberOfCoins;
cin>>numberOfCoins;
int coins[numberOfCoins];
//taking value of each coin
for(int i=0;i<numberOfCoins;i++)
cin>>coins[i];
Solution obj;
//calling function minimumNumberOfCoins()
int answer=obj.minimumNumberOfCoins(coins,numberOfCoins,value);
//printing "Not Possible" if answer is -1
//else printing answer
if(answer==-1)
cout<<"Not Possible"<<endl;
else
cout<<answer<<endl;
}
return 0;
}
// } Driver Code Ends