Suppose you are given a sorted array. How will you count the number of occurrence of a given key?
Since the array is sorted and we are finding the number of occurrences of a given key, the key will be distributed in the array in a certain range. Therefore, we have to find the beginning and end of that range. The length of the range will determine the number. We can find that modifying the binary search. public static int findBeginIndex(int[] array,int key,int low,int high) { |