Find all elements that appear more than n / 4 times in linear time.

This issue is 4-11 from Skiena. The decision to find elements of the majority — a repetition of more than one and a half times — is a majority algorithm. Can we use this to find all numbers repeated n / 4 times?

+5
source share
4 answers

Misra and Gris describe a couple of approaches. I do not quite understand their article, but the key idea is to use a bag.

The original Boyer and Moore algorithm contains a lot of obscure evidence and a discussion of formal verification of the FORTRAN code, but it has a very good start explaining how the majority algorithm works. The key concept starts with the idea that if most of the elements Aand you delete one at a time, a copy Aand a copy of something else, then in the end you will only have copies A. It should then be clear that removing two different elements, none of which is A, can only increase most, whichA. Therefore, it is safe to remove any pairs of objects if they are different. Then this idea can be made concrete. Take the first item from the list and paste it into the box. Take the next item and insert it into the box. If they are the same, let them both sit. If the new one is different, throw it along with the item out of the box. Repeat until all items are in the box or basket. Since only one element is allowed in a field, it can be represented very effectively as a pair (item type, count).

, n/k, , , , . , k, . ? w > n/k, w-1 > (n-k)/k. , , k-1 , !

: , , k-1 . , k ( k-1), ), , , . ""? , , ! , , O (log k) O (n log k). , ( , O (k log k)), , . , , , , ( ). , . , , , - O (nk).

+3

. , , 3 , , n/4 . : , , , , - , , , , n/4 . , , 3 , , n/4 , , - ( ), .

+2

, n/2 times Moore-Voting

. 3 Moore Voting Algo (http://www.geeksforgeeks.org/majority-element/).

: ()

, , remove the majority element -1.

: ()

( -1, ). n/4 times.

: ()

: O (n)

: O (1)

, , n/8, n/16,.... times

EDIT:

, :

, {3, 1, 2, 2, 1, 2, 3, 3}, [2, 3].

n k , , n/k

: fooobar.com/questions/571246/...

:

http://www.cs.utexas.edu/~moore/best-ideas/mjrty/

+1

, hashtable , , , .

0

Source: https://habr.com/ru/post/1548019/


All Articles