Wednesday, August 30, 2006

Shooting Neighbours Puzzle

I came across this puzzle on the web and solved it after some deligence. The reason I am capturing its solution here is not to loose my today's thoughts and have to re-work this on a rainy day -

Statement - There are N people sitting around a circular table and are numbered 1 to N in clockwise fashion. Person 1 suddenly shoots down person to his right, i.e. person N, and hands over the gun to person N-1. Person N-1 shoots down the person to his right, i.e. person N-2 and hands over the gun to Person N-3. This process repeats until only 1 person is left from the group. Who finally survived the process?

Solution - I solved this by establishing a recurrence relation. Let Sn[1, 2, ..., n] = K denote the number of the person who survives the massacre among n people seated clockwise from 1 to n and person 1 has the gun to start with. So,

Sn[1, 2, ..., n-1, n] = K ........(1)

Now calculate Sn+1[1, 2, ..., n, n+1]. Person 1 first shoots down person n+1. Then the problem reduces to Sn, but with a small change. After shooting down n+1, the gun is now with person n. So rearrange the seating to look as if person (n+1) is in first position and the clockwise order is preserved. So,

Sn+1[1, 2, ..., n-2, n-1, n, n+1] = Sn[n, 1, 2, ..., n-1, n-1]....(2).

From equation (1) we know that solution to Sn[1, ..., n] is position K. So after accounting for position shift, we get,

Sn[n, 1, 2,..., n-1] = K-1, for K > 1 and,
= n, for K = 1 ..............(3)

We know, S1 = 1, S2 =1, S3 = 2. By applying equation (3) now we can get the series,

n : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
Sn: 1 1 2 1 4 3 2 1 8 7 6 5 4 3 2 1 16 15 ...

I don't know if any better method to establish a formula, except for looking at the series...we can see that the required formula (after simplification) is

Sn[1, 2, ..., n] = 2^(upper_bound(lg n)) - (n-1)