Friday, October 22, 2010

A Wonderful Puzzle

(thanks to Wendy for giving this to me)

You read in a stream of integers one by one. You don't know in advance how long the sequence will be, though, of course, you can recognize the EOF char when you get to it. You have to find which integer is the majority, where the majority is defined to be the integer that appears at least (n/2) + 1 times, where n is the length of the sequence.

Do it in constant memory. And, linear time, of course.

No comments:

Post a Comment