Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Ways to make an amount from coins

    MediumAbout 25 minFull AccessLearn it: Coin Change: the first minimum DP, and a famous greedy trap

    The task

    A cashier training app asks how many different handfuls of coins pay an amount exactly. A handful is a collection: the order of its coins does not matter.

    Complete CountCoinCombinations(int[] coins, int amount). Coins come in the given values, which are positive and distinct, with as many of each as you like. Return how many different collections of coins add up to exactly amount: 1 + 2 and 2 + 1 are the same collection. Amount 0 has exactly one collection, the empty one, even with no coin values. The answer fits in a long.

    Signature

    public static long CountCoinCombinations(int[] coins, int amount)

    Examples

    • [1, 2, 5], 7 → 6. 5+2, 5+1+1, 2+2+2+1, 2+2+1+1+1, 2+1+1+1+1+1 and seven 1s.
    • [4], 6 → 0. Fours cannot make 6.

    Limits: amount up to 5,000; up to 20 coin values.

    The tests check: amount 0 with and without coin values, one coin value, the order of coins not counting twice, larger amounts, and six coin values with amount 2,000 in time, a count beyond int.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // How many different collections of coins (order does not matter) add up to exactly amount.
          public static long CountCoinCombinations(int[] coins, int amount)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.