View algorithms on Arrays
Question: Given a sorted array of n elements, possibly with duplicates, find the number of occurrences of an element. Input: 4, 4, 8, 8, 8, 15, 16, 23, 23, 42. Find 8 Output: Count = 3 The most basic methodology to solve this problem is linear search. Just scan the array and count the number of occurrences. But this takes time O(n). We can improve the time complexity by using binary search. The steps involved in this method are:- Do a binary search and search for the data in the …