Using right = sortedArray.length - 1:
It's commonly used when you want the right pointer to represent the index of the last element in the array.
The condition in the while loop would be while (left <= right).
Using right = sortedArray.length:
It's used when you want the right pointer to be one position beyond the last element in the array.
The condition in the while loop is while (left < right).
public static int countNumbers(int[] sortedArray, int lessThan) {
int left = 0;
int right = sortedArray.length;
while (left < right) {
int mid = left + (right - left) / 2;
if (sortedArray[mid] < lessThan) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}