CSES Solutions - Coin Combinations I
Last Updated :
02 Apr, 2024
Consider a money system consisting of N coins. Each coin has a positive integer value. Your task is to calculate the number of distinct ways you can produce a money sum X using the available coins.
Examples:
Input: N = 3, X = 9, coins[] = {2, 3, 5}
Output: 8
Explanation: There are 8 number of ways to make sum = 9.
- {2, 2, 5} = 2 + 2 + 5 = 9
- {2, 5, 2} = 2 + 5 + 2 = 9
- {5, 2, 2} = 5 + 2 + 2 = 9
- {3, 3, 3} = 3 + 3 + 3 = 9
- {2, 2, 2, 3} = 2 + 2 + 2 + 3 = 9
- {2, 2, 3, 2} = 2 + 2 + 3 + 2 = 9
- {2, 3, 2, 2} = 2 + 3 + 2 + 2 = 9
- {3, 2, 2, 2} = 3 + 2 + 2 + 2 = 9
Input: N = 2, X = 3, coins[] = {1, 2}
Output: 3
Explanation: There are 3 ways to make sum = 3
- {1, 1, 1} = 1 + 1 + 1 = 3
- {1, 2} = 1 + 2 = 3
- {2, 1} = 2 + 1 = 3
Approach: To solve the problem, follow the below idea:
The problem can be solved using Dynamic Programming. We can maintain a dp[] array, such that dp[i] stores the number of distinct ways to produce sum = i. We can iterate i from 1 to X, and find the number of distinct ways to make sum = i. For any sum i, we assume that the last coin used was the jth coin where j will range from 0 to N - 1. For jth coin, the value will be coins[j], so the number of distinct ways to make sum = i, if the last coin used was the jth coin is equal to dp[i - coins[j]] + 1. Similarly, for every coin we can assume that this coin was the last coin used to make sum = i, and add the number of ways to the final answer. The formula for the ith sum will be: dp[i] = sum(dp[i - coins[j]]), for all j from 0 to N - 1.
Also, the above formula will be true only when coins[j] >= i.
Step-by-step algorithm:
- Maintain a dp[] array such that dp[i] stores the number of distinct ways to make sum = i.
- Initialize dp[0] = 1 as there is only 1 way to make sum = 0, that is to not take any coin.
- Iterate i from 1 to X and calculate number of ways to make sum = i.
- Iterate j over all possible coins assuming the jth coin to be last coin which was selected to make sum = i and add dp[i – coins[j]] to dp[i].
- After all the iterations, return the final answer as dp[X].
Below is the implementation of the algorithm:
C++
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
// function to find the number of distinct ways to make sum
// = X
ll solve(ll N, ll X, vector<ll>& coins)
{
// dp[] array such that dp[i] stores the number of
// distinct ways to make sum = i
ll dp[X + 1] = {};
// There is only 1 way to make sum = 0, that is to not
// select any coin
dp[0] = 1;
// Iterate over all possible sums from 1 to X
for (int i = 1; i <= X; i++) {
// Iterate over all the N coins
for (int j = 0; j < N; j++) {
// Check if it is possible to have jth coin as
// the last coin to construct sum = i
if (coins[j] > i)
continue;
dp[i] = (dp[i] + dp[i - coins[j]]) % mod;
}
}
// Return the number of ways to make sum = X
return dp[X];
}
int main()
{
// Sample Input
ll N = 3, X = 9;
vector<ll> coins = { 2, 3, 5 };
cout << solve(N, X, coins) << "\n";
}
Java
import java.util.*;
public class CoinChangeWays {
static final int MOD = 1000000007;
// Function to find the number of distinct ways to make sum = X
static long solve(int N, int X, List<Integer> coins) {
// dp[] array such that dp[i] stores the number of distinct ways
// to make sum = i
long[] dp = new long[X + 1];
// There is only 1 way to make sum = 0, that is to not select any coin
dp[0] = 1;
// Iterate over all possible sums from 1 to X
for (int i = 1; i <= X; i++) {
// Iterate over all the N coins
for (int j = 0; j < N; j++) {
// Check if it is possible to have jth coin as
// the last coin to construct sum = i
if (coins.get(j) > i) {
continue;
}
dp[i] = (dp[i] + dp[i - coins.get(j)]) % MOD;
}
}
// Return the number of ways to make sum = X
return dp[X];
}
public static void main(String[] args) {
// Sample Input
int N = 3, X = 9;
List<Integer> coins = Arrays.asList(2, 3, 5);
System.out.println(solve(N, X, coins));
}
}
// This code is contributed by shivamgupta0987654321
C#
using System;
using System.Collections.Generic;
public class CoinChangeWays
{
static readonly int MOD = 1000000007;
// Function to find the number of distinct ways to make sum = X
static long Solve(int N, int X, List<int> coins)
{
// dp[] array such that dp[i] stores the number of distinct ways
// to make sum = i
long[] dp = new long[X + 1];
// There is only 1 way to make sum = 0, that is to not select any coin
dp[0] = 1;
// Iterate over all possible sums from 1 to X
for (int i = 1; i <= X; i++)
{
// Iterate over all the N coins
for (int j = 0; j < N; j++)
{
// Check if it is possible to have jth coin as
// the last coin to construct sum = i
if (coins[j] > i)
{
continue;
}
dp[i] = (dp[i] + dp[i - coins[j]]) % MOD;
}
}
// Return the number of ways to make sum = X
return dp[X];
}
public static void Main(string[] args)
{
// Sample Input
int N = 3, X = 9;
List<int> coins = new List<int> { 2, 3, 5 };
Console.WriteLine(Solve(N, X, coins));
}
}
JavaScript
// Function to find the number of distinct ways to make sum = X
function solve(N, X, coins) {
// Initialize a dp array such that dp[i] stores the number of
// distinct ways to make sum = i
let dp = new Array(X + 1).fill(0);
// There is only 1 way to make sum = 0, that is to not
// select any coin
dp[0] = 1;
// Iterate over all possible sums from 1 to X
for (let i = 1; i <= X; i++) {
// Iterate over all the N coins
for (let j = 0; j < N; j++) {
// Check if it is possible to have jth coin as
// the last coin to construct sum = i
if (coins[j] > i)
continue;
dp[i] = (dp[i] + dp[i - coins[j]]) % 1000000007;
}
}
// Return the number of ways to make sum = X
return dp[X];
}
// Sample Input
let N = 3, X = 9;
let coins = [2, 3, 5];
console.log(solve(N, X, coins));
Python3
# Importing the required libraries
from typing import List
# Defining the mod constant
mod = 1000000007
def solve(N: int, X: int, coins: List[int]) -> int:
# dp[] list such that dp[i] stores the number of
# distinct ways to make sum = i
dp = [0] * (X + 1)
# There is only 1 way to make sum = 0, that is to not
# select any coin
dp[0] = 1
# Iterate over all possible sums from 1 to X
for i in range(1, X + 1):
# Iterate over all the N coins
for j in range(N):
# Check if it is possible to have jth coin as
# the last coin to construct sum = i
if coins[j] > i:
continue
dp[i] = (dp[i] + dp[i - coins[j]]) % mod
# Return the number of ways to make sum = X
return dp[X]
def main():
# Sample Input
N = 3
X = 9
coins = [2, 3, 5]
print(solve(N, X, coins))
# Calling the main function
if __name__ == "__main__":
main()
Time Complexity: O(N * X), where N is the size of array coins[] and X is the sum we make using the coins.
Auxiliary Space: O(X)
Similar Reads
CSES Solutions - Coin Combinations II Consider a money system consisting of N coins. Each coin has a positive integer value. Your task is to calculate the number of distinct ordered ways you can produce a money sum X using the available coins.Examples:Input: N = 3, X = 9, coins[] = {2, 3, 5}Output: 3Explanation: There are three ways to
8 min read
CSES Solutions - Dice Combinations Your task is to count the number of ways to construct sum N by throwing a dice one or more times. Each throw produces an outcome between 1 and 6. Examples: Input: N = 3Output: 4Explanation: There are 4 ways to make sum = 3. Using 1 die {3}, sum = 3.Using 2 dice {1, 2}, sum = 1 + 2 = 3.Using 2 dice {
8 min read
CSES Solutions - Coin Piles You are given Q queries. In each query, you have two coin piles containing A and B coins. On each move, you can either remove one coin from the left pile and two coins from the right pile, or two coins from the left pile and one coin from the right pile. Your task is to efficiently find out if you c
7 min read
CSES Solutions - Minimizing Coins Consider a money system consisting of N coins. Each coin has a positive integer value. Your task is to produce a sum of money X using the available coins in such a way that the number of coins is minimal. Examples: Input: N = 3, X = 11, coins[] = {1, 5, 7}Output: 3Explanation: We need minimum 3 coin
7 min read
CSES Solutions - Missing Coin Sum You are given an array of positive integers coins[] of size n, representing n coins of different denominations. The task is to find the smallest sum that can not be created using a subset of the coins[].Note: Each coin can only be used once.Examples: Input: coins[] = [2, 9, 1, 2, 7]Output: 6Explanat
5 min read