Given positive integers from 1 to N, where N can go up to 10 ^ 9 . Some K integers from these given integers are missing. K can be no more than 10 ^ 5 elements . I need to find the minimum amount that cannot be formed from the remaining NK elements in an effective way.
Example; let's say we have N = 5, which means we have {1,2,3,4,5}, and K = 2 and the missing elements: {3,5}, then the remaining array is now {1,2,4} the minimum amount , which cannot 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 do you find this non-cumulative minimum?
I know how to find this if I can save all other elements using this approach:
We can use something similar to the Sieve of Eratosthenes used to search for primes. Same idea, but with different rules for a different purpose.
- Keep numbers from 0 to the sum of all numbers and reset 0.
- Then do the numbers, one at a time, without replacement.
- When we take the number Y, we discard each number equal to Y plus some previously crossed number.
- When we did this for each remaining number, the smallest number without interception is our answer.
However, his need for space is high. Could there be a better and faster way to do this?
source share