Missing Coin Sum

You have n coins with positive integer values. What is the smallest sum you cannot create using a subset of the coins?

Example:

Input: n = 5, arr = {2, 9, 1, 2, 7}
Output: 6

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long missingCoinSum(long long n,
                         vector<long long&arr)
{

    sort(arr.begin(), arr.end());
    long long sum = 0;
    for (long long x : arr)
    {
        if (x > sum + 1)
        {
            return sum + 1;
        }
        sum += x;
    }
    return sum + 1;
}

int main()
{
    long long n = 5;
    vector<long longarr = {29127};

    cout << missingCoinSum(narr<< "\n";

    return 0;
}


No comments:

Post a Comment