In this problem
there are two cases:
case 1 : when any element of n denominations can give result like P % denomination!= 0.
where a[i] means element at index i which is 1<=i<=n, in that case we take a[i] coins only at put all other coins 0
so quantity of a[i] is (P/a[i])+1. And quantity of any other coin is 0.
case 2 : when no element of n denomination give a result of form P%denomination ==0
than we have to find pair of coins which fulfill the required than YES otherwise result and if cant fulfill than ans is NO.
Code for this problem:
/*no change required*/
/****killing like kamikaze****/
/*tanuj yadav*/
/**********************/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
long int sum;
for(int i=0;i<t;i++)
{
long int x,index;
int n,c=0,c1=1;
cin>>n>>sum;
long int a[n];
for(int j=0;j<n;j++)
{
cin>>a[j];
if(sum%a[j]!=0)
{index=j;
c=1;
}
}
int rep[n+1] ={0};
if(c==1)
{
rep[index] = (sum/a[index])+1;
c1=0;
}
else
{
for(int j=n-1;j>=0;j--)
{
if(sum%a[j]==0)
{
x = sum/a[j];
sum = sum -a[j]*(x-1);
rep[j] = x-1;
}
else if(sum%a[j]!=0)
{
x=sum/a[j];
c1=0;
rep[j] = x+1;
break; }
}}
if(c1==0)
{cout<<"YES ";
for(int j=0;j<n;j++)
{cout<<rep[j]<<" ";}
cout<<endl;}
else if(c1==1)
{cout<<"NO"<<endl;
}}
return 0;}
better solution for this problem is always appreciated