Closed rygh4775 closed 5 years ago
We can map all possible integers to a distinct bit with available memory
void solution(String fileName) throws FileNotFoundException {
long numOfInts = ((long) Integer.MAX_VALUE) + 1;
byte[] bytes = new byte[(int) (numOfInts / 8)];
Scanner scanner = new Scanner(new FileReader(fileName));
while(scanner.hasNextInt()) {
int num = scanner.nextInt();
bytes[num / 8] |= (1 << (num % 8));
}
for (int i = 0; i < bytes.length; i++) {
for (int j = 0; j < 8; j++) {
if((bytes[i] & (1 << j)) == 0) {
System.out.println(i * 8 + j);
return;
}
}
}
}
N is 4 billion.
Best case: O(N) + 1
Worst case: O(N) + O(2^31+1)
O(2^31+1)
2^31+1 bits
Missing Int: Given an input file with four billion non-negative integers, provide an algorithm to generate an integer that is not contained in the file. Assume you have 1 GB of memory available for this task. FOLLOW UP What if you have only 10MB of memory? Assume that all the values are distinct and we now have no more than one billion non-negative integers.