Missing Coin Sum
Explanation
Here, we are given n coins and we need to find the smallest value of the sum that cannot be created using a subset of the coins.
This can be done using a greedy algorithm. We can sort the coins in ascending order and then iterate through them. At each step, we can check if the current coin is greater than the current sum plus 1. If it is, then we have found the smallest value that cannot be created using a subset of the coins.
For example, if we have the coins [1, 2, 3, 8, 9], then we can iterate through them as follows:
1is less than or equal to0 + 1, so we can create1using a subset of the coins. (we increment s by1)2is less than or equal to1 + 1, so we can create2using a subset of the coins. (we increment s by2)3is less than or equal to3 + 1, so we can create3using a subset of the coins. (we increment s by3)8is greater than6 + 1, so we cannot create4using a subset of the coins. Therefore,4is the smallest value that cannot be created using a subset of the coins.
Code
n = int(input())
arr = sorted(list(map(int, input().split())))
s = 0
for e in arr:
if e > s + 1:
break
s += e
print(s + 1)