Can anyone think of a linear time algorithm for determining a majority element in a list of elements? The algorithm should use O(1)
space.
If n is the s
This is a popular challenge question, and the answer is that it's impossible. The language of strings with majority elements is not regular (this is easily proven by the pumping lemma) so there is no way it can be recognized in constant space.
Of course the trick is that you need a counter variable which takes O(log n)
space, but since n
is boundd by 2^32 or 2^64 and your computer is really a finite state machine with ~8^(ramsize+hddsize) states, everything is O(1)
.