views:

1558

answers:

11

Given a list of integers, how can I best find an integer that is not in the list?

The list can potentially be very large, and the integers might be large (i.e. BigIntegers, not just 32-bit ints).

If it makes any difference, the list is "probably" sorted, i.e. 99% of the time it will be sorted, but I cannot rely on always being sorted.

Edit -

To clarify, given the list {0, 1, 3, 4, 7}, examples of acceptable solutions would be -2, 2, 8 and 10012, but I would prefer to find the smallest, non-negative solution (i.e. 2) if there is an algorithm that can find it without needing to sort the entire list.

+2  A: 

Unless it is sorted you will have to do a linear search going item by item until you find a match or you reach the end of the list. If you can guarantee it is sorted you could always use the array method of BinarySearch or just roll your own binary search.

Or like Jason mentioned there is always the option of using a Hashtable.

William Edmondson
+6  A: 

One easy way would be to iterate the list to get the highest value n, then you know that n+1 is not in the list.

Edit:

A method to find the smallest positive unused number would be to start from zero and scan the list for that number, starting over and increase if you find the number. To make it more efficient, and to make use of the high probability of the list being sorted, you can move numbers that are smaller than the current to an unused part of the list.

This method uses the beginning of the list as storage space for lower numbers, the startIndex variable keeps track of where the relevant numbers start:

public static int GetSmallest(int[] items) {
 int startIndex = 0;
 int result = 0;
 int i = 0;
 while (i < items.Length) {
  if (items[i] == result) {
   result++;
   i = startIndex;
  } else {
   if (items[i] < result) {
    if (i != startIndex) {
     int temp = items[startIndex];
     items[startIndex] = items[i];
     items[i] = temp;
    }
    startIndex++;
   }
   i++;
  }
 }
 return result;
}

I made a performance test where I created lists with 100000 random numbers from 0 to 19999, which makes the average lowest number around 150. On test runs (with 1000 test lists each), the method found the smallest number in unsorted lists by average in 8.2 ms., and in sorted lists by average in 0.32 ms.

(I haven't checked in what state the method leaves the list, as it may swap some items in it. It leaves the list containing the same items, at least, and as it moves smaller values down the list I think that it should actually become more sorted for each search.)

Guffa
+1  A: 

Unless you are 100% sure it is sorted, the quickest algorithm still has to look at each number in the list at least once to at least verify that a number is not in the list.

AnnaR
+2  A: 

"probably sorted" means you have to treat it as being completely unsorted. If of course you could guarantee it was sorted this is simple. Just look at the first or last element and add or subtract 1.

Brian Ensink
Not true: there are algorithms which perform much better under the assumption of sorted input, and marginally worse if the input turns out not to be sorted after all (behaviour inverse to QuickSort, which performs worst on sorted input).
Konrad Rudolph
+1  A: 

Theoretically, find the max and add 1. Assuming you're constrained by the max value of the BigInteger type, sort the list if unsorted, and look for gaps.

Chris Doggett
+5  A: 

If the number doesn't have any restrictions, then you can do a linear search to find the maximum value in the list and return the number that is one larger.

If the number does have restrictions (e.g. max+1 and min-1 could overflow), then you can use a sorting algorithm that works well on partially sorted data. Then go through the list and find the first pair of numbers v_i and v_{i+1} that are not consecutive. Return v_i + 1.

To get the smallest non-negative integer (based on the edit in the question), you can either:

  • Sort the list using a partial sort as above. Binary search the list for 0. Iterate through the list from this value until you find a "gap" between two numbers. If you get to the end of the list, return the last value + 1.

  • Insert the values into a hash table. Then iterate from 0 upwards until you find an integer not in the list.

marcog
+1  A: 

Are you looking for an on-line algorithm (since you say the input is arbitrarily large)? If so, take a look at Odds algorithm.

Otherwise, as already suggested, hash the input, search and turn on/off elements of boolean set (the hash indexes into the set).

dirkgently
+1  A: 

There are several approaches:

  • find the biggest int in the list and store it in x. x+1 will not be in the list. The same applies with using min() and x-1.

  • When N is the size of the list, allocate an int array with the size (N+31)/32. For each element in the list, set the bit v&31 (where v is the value of the element) of the integer at array index i/32. Ignore values where i/32 >= array.length. Now search for the first array item which is '!= 0xFFFFFFFF' (for 32bit integers).

Aaron Digulla
+1  A: 

If you can't guarantee it is sorted, then you have a best possible time efficiency of O(N) as you have to look at every element to make sure your final choice is not there. So the question is then:

  1. Can it be done in O(N)?
  2. What is the best space efficiency?

Chris Doggett's solution of find the max and add 1 is both O(N) and space efficient (O(1) memory usage)

If you want only probably the best answer then it is a different question.

Nick Fortescue
+1  A: 

Assuming this is the problem I'm thinking of:

You have a set of all ints in the range 1 to n, but one of those ints is missing. Tell me which of int is missing.

This is a pretty easy problem to solve with some simple math knowledge. It's known that the sum of the range 1 .. n is equal to n(n+1) / 2. So, let W = n(n+1) / 2 and let Y = the sum of the numbers in your set. The integer that is missing from your set, X, would then be X = W - Y.

Note: SO needs to support MathML

If this isn't that problem, or if it's more general, then one of the other solutions is probably right. I just can't really tell from the question since it's kind of vague.

Edit: Well, since the edit, I can see that my answer is absolutely wrong. Fun math, none-the-less.

dustyburwell
A: 

You need the list to be sorted. That means either knowing it is sorted, or sorting it.

  • Sort the list. Skip this step if the list is known to be sorted. O(n lg n)
  • Remove any duplicate elements. Skip this step if elements are already guaranteed distinct. O(n)
  • Let B be the position of 1 in the list using a binary search. O(lg n)
  • If 1 isn't in the list, return 1. Note that if all elements from 1 to n are in the list, then the element at B+n must be n+1. O(1)
  • Now perform a sortof binary search starting with min = B, max = end of the list. Call the position of the pivot P. If the element at P is greater than (P-B+1), recurse on the range [min, pivot], otherwise recurse on the range (pivot, max]. Continue until min=pivot=max O(lg n)
  • Your answer is (the element at pivot-1)+1, unless you are at the end of the list and (P-B+1) = B in which case it is the last element + 1. O(1)

This is very efficient if the list is already sorted and has distinct elements. You can do optimistic checks to make it faster when the list has only non-negative elements or when the list doesn't include the value 1.

Strilanc