Design and analyze a linear time algorithm to determine whether
there exists an element in a list of n elements which repeats itself
at least n/10 times in the list.
How can I do this? I’m posting my own idea as an answer.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I assume the elements are comparable.
Perform a selection algorithm for the: n/10th, 2n/10th, …, 9n/10th, 10(n/10)th smallest elements1
These are your candidates. Check the #occurrences for each of them, and if one of them repeats at least n/10 times the answer is
true. Otherwise, it isfalse.Proof:
If an element appears at least n/10 times, then it must “intersect” with
k*n/10for somek(in a sorted list)2. Thus, this element will be chosen as a “candidate”, and you will later discover (by counting) exactly how many times it appears, and will returntrue.If no element is repeated
n/10times, the algorithm will trivially returnfalsein the last step of checking each candidate.Complexity:
Each selection algorithm is
O(n), and it is done 10 times. Also for each candidate requires linear scan on the list, which is alsoO(n)totaling inO(n)overall—but with terrible constants.Explanations:
(1) Selection algorithm will find the element which would be in the index n/10, 2n/10,…9n/10 in a sorted list, and the algorithm itself is only
O(n)(2) Let’s look at the example of
[1,2,..,100,100,..,100](11 times 100).Note that the list is sorted and the element 100 appears in:
list[9*n/10](index9*n/10). The idea of the selection algorithm is, that—even if you shuffle the list—select(list,9*n/10)will always return the same element—in this case 100—since it is the9n/10th element in the sorted list (this is what the algorithm does).Now, you can see that for each element (let it be
e) that repeatsn/10times, there is some indexisuch that in the sorted version of the list, all elements in indicesi,i+1,...,i+n/10will bee. One of these indices must be one ofk*n/10for somek(convince yourself why!). Thus, the selection algorithm onk*n/10will yielde.