A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/competitive-programming/cses-solutions-missing-coin-sum/ below:

CSES Solutions - Missing Coin Sum

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:

Input: coins[] = [1, 2, 3]
Output: 7
Explanation:

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:

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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4