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 7868085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:53:28+00:00 2026-06-03T00:53:28+00:00

I am basically trying to solve the coin change problem through recursion and here

  • 0

I am basically trying to solve the coin change problem through recursion and here is what i have so far -:

#include<iostream>
#include<conio.h>
using namespace std;

int a[]={1,2,5,10,20,50,100,200},count=0;

//i is the array index we are working at
//a[] contains the list of the denominations
//count keeps track of the number of possibilities

void s(int i,int sum) //the function that i wrote
{
    if (!( i>7 || sum<0 || (i==7 && sum!=0) )){

    if (sum==0) ++count; 

    s(i+1,sum);
    s(i,sum-a[i]);

    }
}


int c(int sum,int  i ){  //the function that I took from the algorithmist
    if (sum == 0)
        return 1;
    if (sum < 0)
        return 0;
    if (i <= 0 && sum > 0 )
        return 1;

    return (c( sum - a[i], i ) + c( sum, i - 1 ));
}
int main()
{
    int a;
    cin>>a;

    s(0,a);
    cout<<c(a,7)<<endl<<count;

    getch();
    return 0;
}

The first function that is s(i,sum) has been written by me and the second function that is c(sum,i) has been taken from here – (www.algorithmist.com/index.php/Coin_Change).

The problem is that count always return a way higher value than expected. However, the algorithmist solution gives a correct answer but I cannot understand this base case

if (i <= 0 && sum > 0 ) return 1;

If the index (i) is lesser than or equal to zero and sum is still not zero shouldn’t the function return zero instead of one?

Also I know that the algorithmist solution is correct because on Project Euler, this gave me the correct answer.

  • 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-03T00:53:30+00:00Added an answer on June 3, 2026 at 12:53 am

    I guess that your problem is “Assuming that I have unlimited support of coins, on how many ways can I change the given sum”?
    The algoritimists solution you gave assumes also, that the smallest denomination is 1. Otherwise it will won’t work correctly.
    Now your question:

    if (i <= 0 && sum > 0 ) return 1;
    

    Notice, that the only possibility that i<0 is that you called it with this value – no recursive call will be made with negative value of i. Such case (i<0) is an error so no result is proper (maybe assertion or exception would be better).
    Now if i=0, assuming that at index 0 there is coin of value 1 means that there is only one way to exchange sum with this denomination – give sum coins of value 1. Right?

    After a moment of thought I found out how to remove assumption that a[0] == 1. Change

    if (i <= 0 && sum > 0 ) return 1;
    

    into

    if (i <= 0 && sum > 0 ) return sum % a[0] == 0 ? 1 : 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT Here is the problem I am trying to solve: I have a string
I have been trying to solve a problem involving thread communication using wait() and
I have special problem which I am trying to solve using CSS. My HTML
I am trying to solve a problem that involves basically implementing a logical AND
I have been trying to solve this problem for three days now, it's really
For some time I have been trying to solve fairly common problem consisting of
I am trying to solve a problem where i have a WCF system that
I have been trying to solve this problem for several weeks now and it
im stuck with a problem im basically trying to create a function where I
EXPLAINING WHAT I'M TRYING TO SOLVE: I have a webpage ( file_list.php ) showing

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.