public class ClosestNumberInSortedArray {
public int closestNumber(int[] arr, int target) {
if (arr == null) return -1;
int length = arr.length;
if (length == 0) return -1;
int start = 0;
int end = length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (arr[mid] < target) start = mid;
else if (arr[mid] > target) end = mid;
else return mid;
}
if(Math.abs(arr[start] - target) > Math.abs(arr[end] - target)) return end;
else return start;
}
public static void main(String[] args) {
}
}