Puzzle: using only 2 variables find which element is most frequent in an array -



Puzzle: using only 2 variables find which element is most frequent in an array -

you have stream can contain n integers. can contain 2 various values not know upfront ones are. ex: (7,7,4,4,7,7,4), (-1, 6, -1, -1, -1), (0,1,0,0,1,0,1).

using only 2 named variables (the problem limited in memory space), can find of 2 numbers repeated (that means n = m, n > m or viceversa)? suppose have api hasnext() homecoming true if there more elements in array , next() move on next element, , inspect() returns current element in array. there no way go or rewind stream.

yes. 1 variable records number in lead, , other records size of lead:

leading = stream.inspect() lead = 0 while stream.hasnext(): stream.next() if leading == stream.inspect(): lead += 1 elif lead > 0: lead -= 1 else: leading = stream.inspect() lead = 1

arrays puzzle

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -