CSES Solutions – Missing Coin Sum
Last Updated :
08 Mar, 2024
You have N coins with positive integer values. What is the smallest sum you cannot create using a subset of the coins?
Examples:
Input: N = 5, 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: N = 3, 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: To solve the problem, follow the below idea:
The problem can be solved by using Greedy approach. We can start from the smallest value coin and move to higher values. The main idea is to 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 X, so the maximum value of the next coin can be X + 1. If the next coin has a value greater than X + 1 then (X + 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 (X + 1) we can add it to the total value to get the new total sum X.
Step-by-step algorithm:
- Sort the array of coins in increasing order of values.
- Maintain a variable X which stores the maximum value of the next coin.
- Iterate from 0 to N – 1, and check if arr[i] > X or not.
- If arr[i] > X, return X as the answer.
- Else if arr[i] <= X, update X = X + arr[i].
- After all the iterations if we haven’t returned yet, return (X + 1) as the answer.
C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll solve(ll* arr, ll N)
{
ll X = 1LL;
sort(arr, arr + N);
for ( int i = 0; i < N; i++) {
if (arr[i] > X) {
return X;
}
X += arr[i];
}
return X;
}
int main()
{
ll N = 5;
ll arr[] = { 2, 9, 1, 2, 7 };
cout << solve(arr, N);
}
|
Java
import java.util.Arrays;
public class MaximumSumNotConstructible {
static long solve( long [] arr, int N) {
long X = 1 ;
Arrays.sort(arr);
for ( int i = 0 ; i < N; i++) {
if (arr[i] > X) {
return X;
}
X += arr[i];
}
return X;
}
public static void main(String[] args) {
int N = 5 ;
long [] arr = { 2 , 9 , 1 , 2 , 7 };
System.out.println(solve(arr, N));
}
}
|
Python
def solve(arr, N):
X = 1
arr.sort()
for coin in arr:
if coin > X:
return X
X + = coin
return X
if __name__ = = "__main__" :
N = 5
arr = [ 2 , 9 , 1 , 2 , 7 ]
print (solve(arr, N))
|
C#
using System;
class Program
{
static long Solve( long [] arr, long N)
{
long X = 1;
Array.Sort(arr);
for ( int i = 0; i < N; i++)
{
if (arr[i] > X)
{
return X;
}
X += arr[i];
}
return X;
}
static void Main()
{
long N = 5;
long [] arr = { 2, 9, 1, 2, 7 };
Console.WriteLine(Solve(arr, N));
}
}
|
Javascript
function solve(arr) {
let X = 1;
arr.sort((a, b) => a - b);
for (let i = 0; i < arr.length; i++) {
if (arr[i] > X) {
return X;
}
X += arr[i];
}
return X;
}
let arr = [2, 9, 1, 2, 7];
console.log(solve(arr));
|
Time Complexity: O(N), where N is the number of coins.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...