public class Main { public static int fo(int arr[], int s, int e, int k, int i){ if(s>e){ return i; } int m=(s+e)/2; if(arr[m]==k){ i=m; e=m-1; return fo(arr,s,e,k,i); } if(arr[m]<k){ return fo(arr,m+1,e,k,i); } return fo(arr,s,m-1,k,i); } public static int lo(int arr[], int s, int e, int k, int i){ if(s>e){ return i; } int m=(s+e)/2; if(arr[m]==k){ i=m; s=m+1; return lo(arr,s,e,k,i); } if(arr[m]<k){ return lo(arr,m+1,e,k,i); } return lo(arr,s,m-1,k,i); } public static void main(String[] args) { System.out.println("Hello World "); int arr[]={1,2,3,3,3,3,3,4,5}; System.out.println(fo(arr,0,8,3,-1)+" "+lo(arr,0,8,3,-1)); } } For calculating total occurances total = (last occ - first occ) + 1
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter