Challenge: The Coin Change Problem

Solve the coin change problem using dynamic programming.

Problem statement

Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents), and pennies (1 cent), write some code to calculate the number of ways to represent nn cents.

Input

The values of the coins available to represent the cents with (the denominations), the number of denominations, and the number of cents.

Output

The number of ways that the given number of cents can be represented.

Sample input

denoms = {25, 10, 5, 1};
denomsLength = 4;
amount = 10; // num of cents

Sample output

result = 4; // num of ways to make change for 10 cents

The change for 10 cents can be made in the following four ways:

  • {1,1,1,1,1,1,1,1,1,1}

  • {5,1,1,1,1,1}

  • {5,5}

  • {10}

Coding challenge

First, take a close look at this problem and design a step-by-step algorithm before jumping to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided in the solution section. Good luck!

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.