Search in a Sorted Array of Unknown Size

Honestly, this should be an easy-level problem as it only involves binary search.

class Solution {
    public int search(ArrayReader reader, int target) {
        int low = 0;
        int high = Integer.MAX_VALUE;
        while(low <= high){
            int mid = (low + high) >>> 1;
            int x = reader.get(mid);
            if(x == Integer.MAX_VALUE || x > target) {
                high = mid - 1;
            }else if(x < target){
                low = mid + 1;
            }else if(x == target) return mid;       
        }
     
        return -1;
    }
}
Runtime: 2 ms, faster than 100.00% of Java online submissions for Search in a Sorted Array of Unknown Size.
Memory Usage: 39.3 MB, less than 100.00% of Java online submissions for Search in a Sorted Array of Unknown Size.
comments powered by Disqus