What's the Number on My Hat?
21 Aug 2009
Puzzle

A goblin forewarns N gnomes as follows: "Tomorrow morning, I shall place one hat on each of you. Each hat shall be labeled with some number drawn from the range [0, N-1]. Duplicates are allowed, so two different hats might have the same label. Any gnome shall be able to see numbers on other hats but not his own! When a bell rings, all gnomes shall simultaneously announce one number each. If any gnome succeeds in announcing the number on his own hat, all gnomes shall be set free." The gnomes have assembled in the evening to discuss their predicament. Can you help them devise a strategy?

Source

From a puzzle collection by K Rustan M Leino at Microsoft Research.

Solution

For N = 2, numbers are drawn from the range [0, 1]. The following strategy works: The first gnome announces the number on the second gnome's hat. The second gnome announces the number that's not on the first gnome's hat.

In general, let gnomes be numbered 0 thru N-1. The i-th gnome should announce (i - s) mod N, where s is the sum of numbers he sees. With this strategy, if the sum of all N numbers equals m mod N, then the m-th gnome is guaranteed to announce the number of his own hat!

Thanks to Sreeram Ramachandran for solving this puzzle and sharing his solution with me.