Simple java understandable code


#1

public int[] searchRange(final int[] A, int B) {
int s=-1;
int e=-1;
s=start(A,B,0,A.length-1);
e=end(A,B,0,A.length-1);
int arr[]={s,e};
return arr;
}
int start(int[]a,int b,int l,int r){
if(l<=r){
int mid=l+(r-l)/2;
if(a[mid]>b){
return start(a,b,l,mid-1);
}
else if(a[mid]<b){
return start(a,b,mid+1,r);
}
else if(a[mid]==b){
if(mid-1>=0 && a[mid-1]<b){
return mid;
}
else if(mid-1>=0 && a[mid-1]==b){
return start(a,b,l,mid-1);
}
else return mid;

        }
        
        
        
    }
    return -1;
    
}
int end(int[]a,int b,int l,int r){
    if(l<=r){
        int mid=l+(r-l)/2;
      //  System.out.println("at mid "+mid);
        if(a[mid]>b){
           // System.out.println("found less range is  l"+(l)+" "+(mid-1));

            return end(a,b,l,mid-1);
        }
        else if(a[mid]<b){
            //System.out.println("found less range is  l"+(mid+1)+" "+r);
            return end(a,b,mid+1,r);
        }
        else if(a[mid]==b){

            if(mid+1<a.length && a[mid+1]>b){
              //  System.out.println("ideal case");
                return mid;
            }
            else if(mid+1<a.length && a[mid+1]==b){
              //  System.out.println("found but front same range is "+(mid+1)+" "+r);

                return end(a,b,mid+1,r);
            }
           // System.out.println("found else case");

             return mid;


        }



    }
    return -1;
    
}

#2

this code is working superbly ! i wish i could write such a long code like u man !!!