Method
search
(int[] arr, int target,boolean isStart)
Source from the content-addressed store, hash-verified
| 12 | } |
| 13 | |
| 14 | static int search(int[] arr, int target,boolean isStart){ |
| 15 | int start=0,end=arr.length-1;int ans=-1; |
| 16 | while(end>=start){ |
| 17 | int mid=start+(end-start)/2; |
| 18 | if(target>arr[mid]){ |
| 19 | start=mid+1; |
| 20 | } |
| 21 | else if(target<arr[mid]){ |
| 22 | end=mid-1; |
| 23 | } |
| 24 | else if(target==arr[mid]){ |
| 25 | ans=mid; |
| 26 | if(isStart){ |
| 27 | end=mid-1; |
| 28 | } |
| 29 | else{ |
| 30 | start=mid+1; |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | return ans; |
| 35 | } |
| 36 | } |
Tested by
no test coverage detected