package arrays;
public class lowerAndUpperBound {
public static int ub(int[] arr, int n) {
int low=0;
int high=arr.length-1;
int ans=-1;
while(low<=high) {
int mid=(low+high)/2;
if(arr[mid]==n) {
ans=mid;
low=mid-1;
}
else if(arr[mid]>n) {
high=mid-1;
}
else {
low=mid+1;
}
}
return ans;
}
public static int lb(int[] arr, int n) {
int low=0;
int high=arr.length-1;
int ans=-1;
while(low<=high) {
int mid=(low+high)/2;
if(arr[mid]==n) {
ans=mid;
high=mid-1;
}
else if(arr[mid]>n) {
high=mid-1;
}
else {
low=mid+1;
}
}
return ans;
}
public static void main(String[] args) {
int[] arr= {3,5,6,7,6,3};
System.out.println(lb(arr,3));
System.out.println(ub(arr,3));
System.out.println(lb(arr,6));
System.out.println(ub(arr,6));
}
}