Very easy JAVA (logN)


#1

public class Solution {
int binarySearch(List Arr,double t){
int start=0 ,mid=0 ,ans=0;

    int end=Arr.size()-1;
    while(start<=end){
        mid=(start+end)/2;
        if((double)Arr.get(mid)==t){
            ans=mid;
            return mid;
        }
        else if((double)Arr.get(mid)<t){
            start=mid+1;
        }
        else if((double)Arr.get(mid)>t){
            end=mid-1;
        }
    }
    return mid;
}
public ArrayList<Integer> searchRange(final List<Integer> A, int B) {
    ArrayList<Integer> ans=new ArrayList<Integer>();
    if(!A.contains(B)){
        ans.add(-1);ans.add(-1);
            return ans;
    }
    if(A.size()==1){
        if(A.get(0)==B){
            ans.add(0);ans.add(0);
            return ans;
        }
    }
    int a=binarySearch(A,B+0.5);
    int b=binarySearch(A,B-0.5);
    int a1=a,b1=b;
    ans.add(A.get(b1)==B ? b1 : b1+1);ans.add(A.get(a1)==B ? a1 : a1-1);
    return ans;
}

}