Using java with binary search

Comment body goes here.

    int st = 0;
    int end = A.length-1;
    int []ans = {-1,-1};
    while(st <= end) {
        int mid = (st+end)/2;
        int stPos = -1;
        int endPos = -1;
        if(A[mid] == B) {
            int k = mid;
            while(k-1 >= 0 && A[k-1] == B) {
                k--;
            }
             stPos = k;
            k = mid;
            while(k+1 <= A.length-1 && A[k+1] == B) {
                k++;
            }
             endPos = k;
            ans[0] = stPos;
            ans[1] = endPos;
            return ans;
        } else if (A[mid] < B) st = mid+1;
        else end = mid-1;
    }
    return ans;
Click here to start solving coding interview questions