Given 4 billion numbers, how to find a number which is not in these 4 billion numbers? We have only 1GB of memory.
The numbers can be non-consecutive.
How to do the same in 10MB of memory?
Given 4 billion numbers, how to find a number which is not in these 4 billion numbers? We have only 1GB of memory.
The numbers can be non-consecutive.
How to do the same in 10MB of memory?
Assuming that this is a routine you are just going to run once, use the amount of memory available as a limiting factor. Load the numbers into an array, up to the amount of memory you have available. Sort the array using your favorite sorting algorithm. Do a binary search to see if the value is present. If it's there, you're done, if not, then clear the array and start loading numbers from the file in the last place you left off. Repeat the process until you find a match or you reach the end of the file.
For example, if you have 1 GB to work with and the numbers are 4 bytes large (say, C# int), set the upper array bound at something like 1024 ^ 3 / 4 = 268435456 * i (where i is some value < 1 to make sure we leave a little memory left over for other processes). Fill the array, sort, check, repeat.
If you only have 10 MB to work with, set the upper array bound to 1024 ^ 2 * 10 / 4 = 10485760 * i.
And really, now that I think about it, since the sort has to touch every value anyway, you're better off just scanning the list and leaving out the sort. The sort would be useful though if you wanted to save the list in ordered sets (up to the size of your array) for later processing. In that case you would also want to store the size of the arrays so you could rely on the fact they were sorted for each consecutive run.
Well, with the assumption that I can choose a number which is larger than the largest of the 4 billion numbers:
set i = 0
for each number:
load the number into memory
set i = max(i, number + 1)
Find the maximum of the set of numbers (O(N) in time, constant space) and the number not in the set is max+1.
That doesn't strike me as a very challenging question. Finding the smallest natural number not in the set might be a better question.
If the question is "There are 4 billion random numbers in a list, now choose a new random number that is not in the list." then I would Merge sort the list to put it in order O(n lg n). Then walk down the list comparing the current element to the next one in order O(n). Something like...
MergeSort(thelist);
for(int i = 0; i < thelist.Length; i++)
{
if(thelist[i] + 1 == thelist[i+1])
{
//it's just a duplicate element
continue;
}
else if (thelist[i] + 1 != thelist[i+1])
{
Console.WriteLine("the number is {0}", thelist[i] + 1);
break;
}
Considering the broadness of the OP question, One could venture that there are 4 billion random numbers in there and another random number is chosen and need to check wheter or not that new number is already in the list.
If that's the case, a simple Binary Search (considering that the numbers are ordered) should suffice with a a comparison time of O(logN).