Java Implementation Lower_bound () and Upper_bound () Lower_bound ()
The function Lower_bound () makes a binary lookup of [begin, end] and returns the position of the first element greater than or equal to tar. Returns end If all elements are less than tar.
Public classlowerbound { Public Static int Lower_bound(int[] arr,intBeginintEndintTAR) { while(Begin < End) {intMID = begin + (End-begin)/2;//When the mid element is less than tar if(Arr[mid] < tar)//Begin is Mid + 1, Arr[begin] value is less than or equal to tarBegin = Mid +1;//When the mid element is greater than or equal to tar Else if(Arr[mid] >= tar) end = mid; }returnBegin }}
Upper_bound ()
The function Upper_bound () makes a binary lookup of [begin, end] and returns the position of the first element greater than tar. Returns end If all elements are less than tar.
publicclass UpperBound { publicstaticintupper_bound(intintintint tar) { while(begin < end) { int2; if(arr[mid] <= tar) 1; elseif(arr[mid] < tar) end = mid; } return begin; }}
Reference links
[sumy的博客](http://sumygg.com/2017/09/08/upper-bound-and-lower-bound-in-java/)
Java Implementation Lower_bound () and Upper_bound ()