I wonder what will happen if the size of the array is Integer.MAX_VALUE, and the item that we are looking for is the last one.
Will we have an overflow of the ‘end’ variable when we do the root ‘while’?
Let’s say we have an array […, Integer.MAX_VALUE]. The key is ‘Integer.MAX_VALUE’. Will the algorithm work?
public static int search(ArrayReader reader, int key) {
// find the proper bounds first
int start = 0, end = 1;
while (reader.get(end) < key) {
int newStart = end + 1;
end += (end - start + 1) * 2; // <- can overflow be here?
start = newStart;
}
return binarySearch(reader, key, start, end);
}