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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:59:52+00:00 2026-05-14T00:59:52+00:00

I have an algorithm that recursively makes change in the following manner: public static

  • 0

I have an algorithm that recursively makes change in the following manner:

public static int makeChange(int amount, int currentCoin) {

        //if amount = zero, we are at the bottom of a successful recursion
        if (amount == 0){

            //return 1 to add this successful solution
            return 1;           

            //check to see if we went too far
        }else if(amount < 0){

            //don't count this try if we went too far
            return 0;

            //if we have exhausted our list of coin values
        }else if(currentCoin < 0){              

            return 0;

        }else{  

            int firstWay = makeChange(amount, currentCoin-1);
            int secondWay = makeChange(amount - availableCoins[currentCoin], currentCoin);

            return firstWay + secondWay;            
        }       
}   

However, I’d like to add the capability to store or print each combination as they successfully return. I’m having a bit of a hard time wrapping my head around how to do this. The original algorithm was pretty easy, but now I am frustrated. Any suggestions?

CB

  • 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-05-14T00:59:53+00:00Added an answer on May 14, 2026 at 12:59 am

    I used the following:

    /**
         * This is a recursive method that calculates and displays the combinations of the coins included in
         * coinAmounts that sum to amountToBeChanged.
         * 
         * @param coinsUsed is a list of each coin used so far in the total.  If this branch is successful, we will add another coin on it.
         * @param largestCoinUsed is used in the recursion to indicate at which coin we should start trying to add additional ones.
         * @param amountSoFar is used in the recursion to indicate what sum we are currently at.
         * @param amountToChange is the original amount that we are making change for.
         * @return the number of successful attempts that this branch has calculated.
         */private static int change(List<Integer> coinsUsed, Integer currentCoin, Integer amountSoFar, Integer amountToChange)
        {
            //if last added coin took us to the correct sum, we have a winner!
            if (amountSoFar == amountToChange)
            {
                //output 
                System.out.print("Change for "+amountToChange+" = ");
    
                //run through the list of coins that we have and display each.
                for(Integer count: coinsUsed){
    
                    System.out.print(count + " ");
                }
                System.out.println();       
    
                //pass this back to be tallied
                return 1;
            }
    
            /*
             * Check to see if we overshot the amountToBeChanged
             */
            if (amountSoFar > amountToChange)
            {
                //this branch was unsuccessful
                return  0;
            }
    
            //this holds the sum of the branches that we send below it
            int successes=0;
    
            // Pass through each coin to be used
            for (Integer coin:coinAmounts)
            {
                //we only want to work on currentCoin and the coins after it
                if (coin >= currentCoin)
                {
                    //copy the list so we can branch from it
                    List<Integer> copyOfCoinsUsed = new ArrayList<Integer>(coinsUsed);
    
                    //add on one of our current coins
                    copyOfCoinsUsed.add(coin);
    
                    //branch and then collect successful attempts
                    successes += change(copyOfCoinsUsed, coin, amountSoFar + coin, amountToChange);
                }
            }
    
            //pass back the current
            return successes;
    
        } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an algorithm that generates strings based on a list of input words.
I have a two part question Best-Practice I have an algorithm that performs some
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Does anybody have any reference material that details Cauchy-Reed algorithm? Googling for Cauchy-Reed Solomon
I have a tail recursive pathfinding algorithm that I've implemented in JavaScript and would
i have an algorithm that reduces the number of colors in a picture. I
We have several different optimization algorithms that produce a different result for each run.
I have a recursive algorithm which steps through a string, character by character, and
Does anyone have a decent algorithm for calculating axis minima and maxima? When creating
Does anyone have a good algorithm for taking an ordered list of integers, i.e.:

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.