Give a sorted array of elements, what is the minimum number of comparisons needed to check whether a certain element occurs more than half of the time?
Details and Assumptions
-No auxiliary data structure is allowed.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
We do a binary search on the array. Since the array is sorted, if an element appears for more than half of the time, the middle array will necessarily be that element, which means if the middle element is not the element, then it can't appear more than half of the time. If it is, then we need to find the first and last appearances of that element, which will take O ( l o g ( n ) ) time respectively, and then subtract the indexes to compare to 2 n .