How to implement coin change problem using bottom-up approach using C#?

The coin change problem is a classic dynamic programming challenge where we need to find the minimum number of coins required to make a given amount. The bottom-up approach builds solutions incrementally, starting from the smallest amounts and working up to the target amount.

This approach uses dynamic programming to avoid recalculating the same subproblems multiple times, storing previously computed results in an array.

Time complexity − O(n × t) where n is the amount and t is the number of coin types

Space complexity − O(n) for the dynamic programming array

Syntax

Following is the basic structure for the bottom-up coin change approach −

public int CoinChange(int amount, int[] coins) {
   int[] dp = new int[amount + 1];
   // Fill dp array with maximum values except dp[0] = 0
   // For each amount from 1 to target amount
   // For each coin type, find minimum coins needed
   return dp[amount];
}

How the Bottom-Up Approach Works

The algorithm builds up solutions from smaller amounts to larger ones −

Bottom-Up DP Array Building Process Amount: 0 1 2 3 4 5 6 7 dp[]: 0 1 2 3 4 5 6 1 Coins: [1, 7] - For amount 7, we can use one coin of value 7 Each dp[i] represents minimum coins needed for amount i Build from smaller to larger amounts

Example

using System;
using System.Linq;

public class DynamicProgramming {
    public int CoinChangeBottomUpApproach(int amount, int[] coins) {
        int[] dp = new int[amount + 1];
        
        // Initialize dp array - 0 coins needed for amount 0
        for (int i = 1; i <= amount; i++) {
            dp[i] = int.MaxValue;
        }
        
        // Fill dp array bottom-up
        for (int i = 1; i <= amount; i++) {
            for (int j = 0; j < coins.Length; j++) {
                if (i >= coins[j] && dp[i - coins[j]] != int.MaxValue) {
                    dp[i] = Math.Min(dp[i], dp[i - coins[j]] + 1);
                }
            }
        }
        
        return dp[amount] == int.MaxValue ? -1 : dp[amount];
    }
}

class Program {
    static void Main(string[] args) {
        DynamicProgramming dp = new DynamicProgramming();
        int[] coins = { 1, 7, 10 };
        int result = dp.CoinChangeBottomUpApproach(15, coins);
        
        if (result != -1) {
            Console.WriteLine("Minimum coins needed: " + result);
        } else {
            Console.WriteLine("Amount cannot be made with given coins");
        }
    }
}

The output of the above code is −

Minimum coins needed: 3

Using Different Coin Sets

Example

using System;

public class CoinChangeExample {
    public static int CoinChange(int amount, int[] coins) {
        int[] dp = new int[amount + 1];
        
        for (int i = 1; i <= amount; i++) {
            dp[i] = amount + 1; // Use amount + 1 as "impossible" marker
        }
        
        for (int i = 1; i <= amount; i++) {
            foreach (int coin in coins) {
                if (coin <= i) {
                    dp[i] = Math.Min(dp[i], dp[i - coin] + 1);
                }
            }
        }
        
        return dp[amount] > amount ? -1 : dp[amount];
    }
    
    static void Main(string[] args) {
        int[] coins1 = { 1, 3, 4 };
        int amount1 = 6;
        Console.WriteLine($"Coins {string.Join(", ", coins1)}, Amount {amount1}: {CoinChange(amount1, coins1)} coins");
        
        int[] coins2 = { 2, 5 };
        int amount2 = 11;
        Console.WriteLine($"Coins {string.Join(", ", coins2)}, Amount {amount2}: {CoinChange(amount2, coins2)} coins");
        
        int[] coins3 = { 2 };
        int amount3 = 3;
        int result3 = CoinChange(amount3, coins3);
        Console.WriteLine($"Coins {string.Join(", ", coins3)}, Amount {amount3}: {(result3 == -1 ? "Impossible" : result3 + " coins")}");
    }
}

The output of the above code is −

Coins 1, 3, 4, Amount 6: 2 coins
Coins 2, 5, Amount 11: 3 coins
Coins 2, Amount 3: Impossible

Key Points

  • Initialize dp[0] = 0 because 0 coins are needed to make amount 0

  • Use int.MaxValue or amount + 1 to represent impossible combinations

  • For each amount, try all possible coins and take the minimum

  • Return -1 if the target amount cannot be achieved with given coins

Conclusion

The bottom-up approach to the coin change problem efficiently finds the minimum number of coins needed by building solutions incrementally. This dynamic programming technique avoids redundant calculations and provides an optimal solution with O(n × t) time complexity.

Updated on: 2026-03-17T07:04:36+05:30

652 Views

Advertisements