CSES Solutions - Missing Coin Sum
Last Updated :
19 Feb, 2025
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: 6
Explanation:
- Sum = 1, we can take the coin with value 1.
- Sum = 2, we can take the coin with value 2.
- Sum = 3, we can take coins with value 1 and 2.
- Sum = 4, we can take coins with value 2 and 2.
- Sum = 5, we can take coins with value 1, 2 and 2.
- Sum = 6, no possible subset of coins can sum up to 6.
Input: coins[] = [1, 2, 3]
Output: 7
Explanation:
- Sum = 1, we can take the coin with value 1.
- Sum = 2, we can take the coin with value 2.
- Sum = 3, we can take coins with value 3.
- Sum = 4, we can take coins with value 1 and 3.
- Sum = 5, we can take coins with value 2 and 3.
- Sum = 6, we can take coins with value 1, 2 and 3.
- Sum = 7, no possible subset of coins can sum up to 7.
Approach:
The idea is to use Greedy approach to solve the problem. We start from the coins of smallest value and build up the sum of coins we can form. Let's say we have some coins whose total value is val, so the maximum value of the next coin can be val + 1. If the next coin has a value greater than val + 1 then (val + 1) is the smallest sum which we cannot create using any subset of coins. Otherwise, if the value of the next coin is less than (val + 1) we can add it to the total value to get the new total sum val.
Step-by-step algorithm:
- Sort the array of coins in increasing order of values.
- Maintain a variable val, initialized with value 1, which stores the maximum value of the next coin.
- Iterate from 0 to n - 1, and check if arr[i] > val or not.
- If arr[i] > val, return val as the answer.
- Else if arr[i] <= val, update val = val + arr[i].
- After all the iterations if we haven't returned yet, return (val) as the answer.
Below is given the implementation:
C++
// C++ program to find the missing coin sum
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum sum which we cannot
// construct using given coins
int minCoinSum(vector<int> &coins) {
// sort the coins in ascending order of values
sort(coins.begin(), coins.end());
// to store maximum value of next coin
int val = 1;
for(auto i:coins) {
// if current coin is greater than val
// then val can't be formed
if(i > val)
return val;
// else update the minimum coin sum
val += i;
}
return val;
}
int main() {
vector<int> coins = { 2, 9, 1, 2, 7 };
cout << minCoinSum(coins);
return 0;
}
Java
// Java program to find the missing coin sum
import java.util.*;
class GfG {
// Function to find the minimum sum which we cannot
// construct using given coins
static int minCoinSum(ArrayList<Integer> coins) {
// sort the coins in ascending order of values
Collections.sort(coins);
// to store maximum value of next coin
int val = 1;
for (int i : coins) {
// if current coin is greater than val
// then val can't be formed
if (i > val)
return val;
// else update the minimum coin sum
val += i;
}
return val;
}
public static void main(String[] args) {
// Sample Input
ArrayList<Integer> coins = new ArrayList<>(Arrays.asList(2, 9, 1, 2, 7));
System.out.println(minCoinSum(coins));
}
}
Python
# Python program to find the missing coin sum
def minCoinSum(coins):
# Function to find the minimum sum which we cannot
# construct using given coins
coins.sort()
# sort the coins in ascending order of values
# to store maximum value of next coin
val = 1
for i in coins:
# if current coin is greater than val
# then val can't be formed
if i > val:
return val
# else update the minimum coin sum
val += i
return val
if __name__ == "__main__":
coins = [2, 9, 1, 2, 7]
print(minCoinSum(coins))
C#
// C# program to find the missing coin sum
using System;
using System.Collections.Generic;
class GfG {
// Function to find the minimum sum which we cannot
// construct using given coins
static int minCoinSum(List<int> coins) {
// sort the coins in ascending order of values
coins.Sort();
// to store maximum value of next coin
int val = 1;
foreach (int i in coins) {
// if current coin is greater than val
// then val can't be formed
if (i > val)
return val;
// else update the minimum coin sum
val += i;
}
return val;
}
static void Main() {
// Sample Input
List<int> coins = new List<int> { 2, 9, 1, 2, 7 };
Console.WriteLine(minCoinSum(coins));
}
}
JavaScript
// JavaScript program to find the missing coin sum
function minCoinSum(coins) {
// Function to find the minimum sum which we cannot
// construct using given coins
coins.sort((a, b) => a - b);
// sort the coins in ascending order of values
// to store maximum value of next coin
let val = 1;
for (let i of coins) {
// if current coin is greater than val
// then val can't be formed
if (i > val)
return val;
// else update the minimum coin sum
val += i;
}
return val;
}
// Driver Code
let coins = [2, 9, 1, 2, 7];
console.log(minCoinSum(coins));
Time Complexity: O(n * log(n)), where n is the number of coins. We are sorting the array and then traversing it, thus the overall complexity will be O(n* log(n) + n)) ~= O(n * log(n))
Auxiliary Space: O(1), as we are using no extra space.
Similar Reads
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 - 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 - Money Sums You have N coins with certain values. Your task is to find all money sums you can create using these coins. Examples: Input: N = 4, coins[] = {4, 2, 5, 2}Output: 92 4 5 6 7 8 9 11 13Explanation: To create sum = 2, we can use the coin with value = 2.To create sum = 4, we can use both the coins with v
8 min read
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 - Coin Combinations I 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: 8Explanation: There are 8 number of ways to
8 min read