0

Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins, how many ways can we make the change? The order of coins doesn’t matter. For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5.

The Link of the Question is : https://www.geeksforgeeks.org/coin-change-dp-7/

I did come across all the solutions.

I've used the Matrix method to come up with a solution for this :

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <climits>
#include <map>
#include <set>
#include <queue>
#include <unordered_map>
#define ll long long
//Author: Nilargha Roy (neel13)
using namespace std;
int coinchangeways(int arr[],int n,int sum)
{

        int dp[n+1][sum+1];
        memset(dp,-1,sizeof(dp));


        for(int j=1;j<n;j++)
        {
            dp[0][j]=0;
        }
        for(int i=0;i<n;i++)
        {
            dp[i][0]=1;
        }



    for(int i=1;i<n+1;i++)
    {
        for(int j=1;j<sum+1;j++)
        {

            if(arr[i-1] <= j)
                dp[i][j]=dp[i][j-arr[i-1]] + dp[i-1][j];
            else
                dp[i][j]=dp[i-1][j];

        }
    }

    return dp[n][sum];

}

int main() 
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);

#ifdef __APPLE__
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
        cin>>arr[i];
    int sum; cin>>sum;
    cout<<coinchangeways(arr,n,sum);
}

However, when I'm putting in an array = {1,2,3} and SUM = 5 as input, The Output should be : 5 while I'm getting 1. Can anyone pinpoint the logical error in my code?

  • 1
    You will have to try to find the logical error in your code using your debugger, that's what it's there for. Those meaningless online contest sites typically produce code that's hard for other people to read, and help you with, so you're on your own here. It's full of useless macros, and non-standard C++, like is the case here. You will find it beneficial to spend more time with a quality C++ textbook, studying core C++ concepts like natural 0-based array indexing. The shown code attempts to use unnatural 1-based array indexing, which just makes it even harder to read and understand its logic. – Sam Varshavchik Jun 19 '20 at 11:18
  • my new favorite punchline: competitive coding to learn a language is like going to a punk concert to prepare for a waltz dancing contest. One bad thing about them is that they give you the impression that programming is about writing correct code. It is not. A tiny part of programming is writing code, and a huge part is debugging and testing. You wrote some code, now you need to do the rest also – 463035818_is_not_an_ai Jun 19 '20 at 11:37
  • @idclev463035818 sorry to burst your bubble but I wasn't using CP as a medium to learn any language, I simply wanted a helping hand with my solution. Thank you. God speed. – Nilargha Roy Jun 19 '20 at 11:43
  • 1
    Try printing out the first line of your array after you've initialized it. – Paul Hankin Jun 19 '20 at 12:02
  • @PaulHankin I did. All good. – Nilargha Roy Jun 19 '20 at 12:07
  • 1
    sorry, to burst your bubble, but using a debugger is an essential skill and it seems like you need to catch up on that. – 463035818_is_not_an_ai Jun 19 '20 at 12:20
  • Sorry for my previous erroneous comment. It seems that the "-1' are propagating through the DP array. You may try to initialize it to 0 and not -1 – Damien Jun 19 '20 at 12:35
  • Are you aware that 1d-array is sufficient for this task? – MBo Jun 19 '20 at 12:47
  • @MBo Hey man Can you elucidate how ? – Nilargha Roy Jun 19 '20 at 15:17
  • I added answer with 1d array code – MBo Jun 19 '20 at 15:55

2 Answers2

2

Version that use 1d array:

int coinchangeways(int arr[], int n, int sum)
{
    int dp[sum+1];
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;

    for (int i = 0; i < n; i++)
    {
        for (int j = arr[i]; j <= sum; j++)
        {
                dp[j] += dp[j - arr[i]];
        }
    }

    return dp[sum];
}
MBo
  • 77,366
  • 5
  • 53
  • 86
1

when you run your function after all of the for loop. you will have DP shown below.

 1   0   0  -1  -1  -1

 1   1   1   0  -1  -2

 1   1   2   1   1  -1

 -1  1   2   0   2   1

dp[3][5] = 1 that's why you are getting 1.

Nilesh Solanki
  • 336
  • 4
  • 19