jayati

Minimum number of Coins

Oct 27th, 2023
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. //{ Driver Code Starts
  2. // Initial Template for C++
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. // } Driver Code Ends
  8. // User function Template for C++
  9.  
  10. class Solution{
  11. public:
  12.     vector<int> solve(int arr[],vector<int> &coins,int N)
  13.     {
  14.         if(N==0)
  15.         {
  16.             return coins;
  17.         }
  18.         int i,k;
  19.         for(int i=0;i<=9;i++)
  20.         {
  21.             if(arr[i]>N)
  22.             {
  23.             k=arr[i-1];
  24.             coins.push_back(k);
  25.             break;
  26.             }
  27.             else if(i==9 && arr[i]<N)
  28.             {
  29.                 k=arr[9];
  30.                 coins.push_back(k);
  31.                 break;
  32.             }
  33.            
  34.         }
  35.         return solve(arr,coins,N-k);
  36.        
  37.     }
  38.     vector<int> minPartition(int N)
  39.     {
  40.         // code here
  41.         int arr[]={ 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 };
  42.         vector<int> coins;
  43.         return solve(arr,coins,N);
  44.     }
  45. };
  46.  
  47. //{ Driver Code Starts.
  48.  
  49. int main(){
  50.     int t;
  51.     cin>>t;
  52.     while(t--){
  53.         int N;
  54.         cin>>N;
  55.        
  56.         Solution ob;
  57.         vector<int> numbers = ob.minPartition(N);
  58.         for(auto u: numbers)
  59.             cout<<u<<" ";
  60.         cout<<"\n";
  61.     }
  62.     return 0;
  63. }
  64. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment