Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8364447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:25:59+00:00 2026-06-09T12:25:59+00:00

The problem requires to count number of coin changes for a particular cost. For

  • 0

The problem requires to count number of coin changes for a particular cost.

For example, if I have coin values of 50, 20, 10, 5, 1, I can form costs of:

5 => (5), (11111), which are 2 ways.

10 => (10), (5, 5), (5, 11111), (11111, 11111), which are 4 ways.

Here is my function. It is returning wrong results begging from cost of 10 (returns 9 ways while the actual number of ways is only 4)

int dp[10000];
int coins[] = { 50, 20, 10, 5, 1 };
int rec(int n)
{
  if (n == 0) return 1;
  if (dp[n] != -1) return dp[n];
  int cnt = 0;
  for (int i = 0; i < 5; i++)
    if (coins[i] <= n) cnt += rec(n - coins[i]);
  return dp[n] = cnt;
}

How can I fix this function to give the correct number of ways? Is this algorithm correct even? see the complete code and its output here

NOTE: my problem is not with dp array initialization. I am using memset to initialize it to -1 each time before calling rec.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:26:00+00:00Added an answer on June 9, 2026 at 12:26 pm

    (5, 1, 1, 1, 1, 1) and (1, 1, 1, 5, 1, 1) is different way in you algorithm, you should keep it decreasing.

    int dp[10000][5];  // dp[20][2] means, if the biggest coin is coins[2],
                       // how much ways for 20 ?
    int coins[] = { 1, 5, 10, 20, 50 }; // here
    int rec(int n, int m)
    {
      int cnt = 0;
      int i;
      if (n == 0) return 1;
      //if (m == 0) return 1;
      if (dp[n][m] != -1) return dp[n][m];
      for (i = 0; i <= m; i++)
        if (coins[i] <= n) cnt += rec(n - coins[i], i);
      return dp[n][m] = cnt;
    }
    
    int main()
    {
            memset(dp, -1, sizeof(dp));
            printf("%d\n", rec(10, 4));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem which requires a reversable 1:1 mapping of keys to values.
I have a problem here that requires to design a data structure that takes
I have a problem. Part of my app requires text to be shown in
Can I add values to an instance of an object after I have used
I have a very simple problem which requires a very quick and simple solution
I am trying to count the number of objects with have a foreign key
I'm currently working a problem that requires my web application to generate a chart
Hello I'm working on a problem that requires me to change an set array
I am a Perl newbie, stuck with another bioinformatics problem that requires some help
Is there a problem to using javascript for styling content? My website requires javascript.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.