Coin Change Ways

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This challenge is about counting the ways of making change for a given amount of money, using an array of distinct coins. The goal is to find the number of unique combinations that can be used to make the change.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/recursion_dynamic("`Recursion Dynamic`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/recursion_dynamic -.-> lab-268855{{"`Coin Change Ways`"}} python/conditional_statements -.-> lab-268855{{"`Coin Change Ways`"}} python/for_loops -.-> lab-268855{{"`Coin Change Ways`"}} python/lists -.-> lab-268855{{"`Coin Change Ways`"}} python/tuples -.-> lab-268855{{"`Coin Change Ways`"}} python/function_definition -.-> lab-268855{{"`Coin Change Ways`"}} python/build_in_functions -.-> lab-268855{{"`Coin Change Ways`"}} end

Coin Change Ways

Problem

Given an integer n and an array of distinct coins, write a function to count the number of ways of making change for n using the coins in the array. A coin can be used any number of times, and we are counting unique combinations.

For example, if n = 4 and coins = [1, 2], there are 3 ways of making change: 1+1+1+1, 1+2+1, and 2+2.

Requirements

To solve this problem, you will need to:

  • Write a function that takes two arguments: an integer n and an array of distinct coins.
  • Use dynamic programming to count the number of ways of making change for n using the coins in the array.
  • Return the number of unique combinations.

Example

Input: n = 4, coins = [1, 2]

Output: 3. 1+1+1+1, 1+2+1, 2+2, would be the ways of making change.

Summary

In this challenge, we learned how to count the ways of making change for a given amount of money using an array of distinct coins. By using dynamic programming, we were able to efficiently calculate the number of unique combinations.

Other Algorithm Tutorials you may like