Number of Coins

PROBLEM :
Given a value V, if we want to make change for V cents,
and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins,
what is the minimum number of coins to make the change?

Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is V and N,V is the value of cents and N is the number of coins.
The second line of each test case contains N input C[i],value of available coins.

Output:
Print the minimum number of coins to make the change, if not possible print -1.

Constraints:
1 = T = 100
1 = V = 10000
1 = N = 50
1 = C[i] = 100

Example:
Input
1
7 2
2 1

Output
4

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<limits.h>
#include<iostream>
using namespace std;
int Number_Coins(int [],int ,int ) ;
int min_of(int ,int ) ;
#define max 100
int main()
{
    int t,no,i,k ;
    int arr[max] ;
    cin>>t ;
    while(t--)
    {
        cin>>k ;
        cin>>no ;
       
        for(i=0;i<no;i++)
            cin>>arr[i] ;
           
        cout<<Number_Coins(arr,no,k)<<endl ;
    }
return 0;
}

int Number_Coins(int arr[],int no,int final)
{
    int i,j ;
    int temp[final+1] ;
   
    temp[0]=0 ;
    for(i=1;i<=final;i++)
        temp[i]=INT_MAX-40 ;
       
    for(i=0;i<no;i++)
    {
        for(j=1;j<=final;j++)
        {
            if(j>=arr[i])
                temp[j]=min_of(temp[j],1+temp[j-arr[i]]) ;
        }
    }

    if(temp[final]>100000)
    return -1 ;
   
    return temp[final] ;
}

int min_of(int a,int b)
{
    return a<b?a:b ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )