The problem is fairly simple and straight .However i cannot solve it exclusively using dp knapsack style . I have solution for that but since the number of coins in each denomination here is limited (in this question it's )it's creating a problem . I want to arrive at a 2d recurrence that i can use to implement the knapsack . My actual solution which runs fine goes below :
#include<iostream>
using namespace std ;
int main ()
{ int flag ;
int N,i ;
int sum ;
int counter ;
while (cin >> N >>sum )
{
counter = 0;
flag= 0 ;
int count =0;
for( i = N ; i>=1;i--){
count += i ;
counter++;
if (count >sum){
count-=i ;
counter -- ;
}
if(count == sum){
flag = 1 ;
break ;
}
}
if (flag==1)
cout << counter<<"\n" ;
else cout <<-1<<"\n" ;
}
return 0 ;
}