Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find minimum sum that cannot be formed

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 know how to find this if i can store all the remaining elements by this approach:

We can use something similar to Sieve of Eratosthenes, used to find primes. Same idea, but with different rules for a different purpose.

  1. Store the numbers from 0 to the sum of all the numbers, and cross off 0.
  2. Then take numbers, one at a time, without replacement.
  3. When we take the number Y, then cross off every number that is Y plus some previously-crossed off number.
  4. When we have done this for every number that is remaining, the smallest un-crossed-off number is our answer.

However, its space requirement is high. Can there be a better and faster way to do this?

like image 765
user Avatar asked Jan 02 '15 18:01

user


People also ask

How do you find the smallest number that is missing in the array?

If i is not present in the array then return i. If arr[0] is not 0, return 0. Otherwise traverse the input array starting from index 0, and for each pair of elements a[i] and a[i+1], find the difference between them. if the difference is greater than 1 then a[i]+1 is the missing number.

How do you find the smallest positive number?

You have to find the smallest positive number missing from the array in O(n) time using constant extra space. You can modify the original array. A naive method to solve this problem is to search all positive integers, starting from 1 in the given array. We may have to search at most n+1 numbers in the given array.

What is smallest missing positive integer?

Explanation 1: 1 is the smallest positive integer missing from the array.

How do you find the smallest positive integer in Python?

that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5. Given A = [1, 2, 3], the function should return 4. Given A = [−1, −3], the function should return 1.


1 Answers

Here's an O(sort(K))-time algorithm.

Let 1 &leq; x1 &leq; x2 &leq; … &leq; xm be the integers not missing from the set. For all i from 0 to m, let yi = x1 + x2 + … + xi be the partial sum of the first i terms. If it exists, let j be the least index such that yj + 1 < xj+1; otherwise, let j = m. It is possible to show via induction that the minimum sum that cannot be made is yj + 1 (the hypothesis is that, for all i from 0 to j, the numbers x1, x2, …, xi can make all of the sums from 0 to yi and no others).

To handle the fact that the missing numbers are specified, there is an optimization that handles several consecutive numbers in constant time. I'll leave it as an exercise.

like image 168
David Eisenstat Avatar answered Sep 30 '22 12:09

David Eisenstat