if (a[mid] == target) { start = mid; // this is start right = mid - 1; // lets see if there one more on the left } elseif (target > a[mid]) { left = mid + 1; } else { right = mid - 1; } }
return start; }
publicintfindEndPosition(int[] a, int target) { intleft=0; intright= a.length - 1; intend= -1;
while (left <= right) { intmid= left + (right - left) / 2;
if (a[mid] == target) { end = mid; // this is the end left = mid + 1; // lets see if there is one more on the right } elseif (target > a[mid]) left = mid + 1; else right = mid - 1;