Reputation: 10855
Let's say there are three elements in a non-sorted array all of which appear more than one-fourth times of the total number of elements.
What is the most efficient way to find these elements? Both for non-online and online versions of this question.
Thank you!
Edit
The non-online version I was referring to is: this array is specified in full. The online version means the array elements are coming one at a time.
I require the space in addition to time complexity to be tight.
disclaimer: THIS IS NOT HOMEWORK! I consider this as research-level question.
Upvotes: 8
Views: 2161
Reputation: 183868
Remember up to three elements, together with counters.
Small constant additional space, O(n), no sorting.
Upvotes: 13
Reputation: 3049
create a histogram of the entries, sort it, and take the three largest entries.
Upvotes: 2