Minimum Sum that cannot be obtained from the 1....n with some missing numbers

270 Views Asked by At

Given positive integers from $1$ to $N$ where $N$ can go upto $10^9$. Some $K$ integers from these given integers are missing. $K$ can be at max $10^5$ elements. I need to find the minimum sum that can't be formed from remaining $N-K$ elements in an efficient way.

Example; say we have $N=5$ it means we have ${(1,2,3,4,5)}$ and let $K=2$ and missing elements are: ${(3,5)}$ then remaining array is now ${(1,2,4)}$ the minimum sum that can't be formed from these remaining elements is 8 because :

$1=1$

$2=2$

$3=1+2$

$4=4$

$5=1+4$

$6=2+4$

$7=1+2+4$

So how to find this un-summable minimum?

I did find a link but that is the reverse of this problem :-

https://stackoverflow.com/questions/21060873/minimum-sum-that-cant-be-obtained-from-a-set

There are a few dirty cases that can directly check say if 1 is missing then 1 cannot be formed similarly if 2 is missing then 2 cannot be formed