CSES Solutions - Missing Coin Sum
Last Updated :
23 Jul, 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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Dynamic Programming or DP Dynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
What is an Operating System? An Operating System is a System software that manages all the resources of the computing device. Acts as an interface between the software and different parts of the computer or the computer hardware. Manages the overall resources and operations of the computer. Controls and monitors the execution o
5 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read