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.
Basic OperationsTraverse − print all the array elements one by one. Insertion − Adds an element at the given index. Deletion − Deletes an element at the given index. Search − Searches an element using the given index or by the value.
If we know the majority element in the left and right halves of an array, we can determine which is the global majority element in linear time. Here, we apply a classical divide & conquer approach that recurses on the left and right halves of an array until an answer can be trivially achieved for a length-1 array.
The majority element of an array is the element that occurs repeatedly for more than half of the elements of the input. If we have a sequence of numbers then the majority element appears at least times in the sequence.
Remember up to three elements, together with counters.
Small constant additional space, O(n), no sorting.
create a histogram of the entries, sort it, and take the three largest entries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With