Open In App

CSES Solutions - Missing Coin Sum

Last Updated : 19 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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));

Output
6

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.


Next Article
Practice Tags :

Similar Reads