n people (numbered 1 to n) around a circle,eliminate every second remaining person until only one survives.
The problem — given the number of people, starting point, direction, and number to be skipped — is to choose the position in the initial circle to avoid execution.
Solution:
In the following, denotes the number of people in the initial circle, and denotes the count for each step, that is, people are skipped and the -th is executed.
The people in the circle are numbered from to .
k=2
We explicitly solve the problem when every second person will be killed, i.e. . (For the more general case , we outline a solution below.) We express the solution recursively.
Let denote the position of the survivor when there are initially people (and ).
The first time around the circle, all of the even-numbered people die.
The second time around the circle, the new 2nd person dies, then the new 4th person, etc.;
it's as though there were no first time around the circle.
If the initial number of people was even, then the person in position during the second time around the circle was originally in position (for every choice of ).
Let . The person at who will now survive was originally in position .
This gives us the recurrence
If the initial number of people was odd, then we think of person 1 as dying at the end of the first time around the circle.
Again, during the second time around the circle, the new 2nd person dies, then the new 4th person, etc. In this case, the person in position was originally in position .
This gives us the recurrence
When we tabulate the values of and we see a pattern:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | |
1 | 1 | 3 | 1 | 3 | 5 | 7 | 1 | 3 | 5 | 7 | 9 | 11 | 13 | 15 | 1 |
This suggests that is an increasing odd sequence that restarts with whenever the index n is a power of 2.
Therefore, if we choose m and l so that and , then .
It is clear that values in the table satisfy this equation. Or we can think that after people are dead there are only people and we go to the th person.
He must be the survivor. So . Below, we give a proof by induction.
Theorem: If and , then .
Proof: We use strong induction on .
The base case is true.
We consider separately the cases when is even and when is odd.
If is even, then choose and such that and .Note that .
We have , where the second equality follows from the induction hypothesis.
If is odd, then choose and such that and . Note that .
We have , where the second equality follows from the induction hypothesis.
This completes the proof.
We can solve for to get an explicit expression for :
The most elegant form of the answer involves the binary representation of size : can be obtained by a one-bit left cyclic shift of itself.
If we represent in binary as , then the solution is given by .
The proof of this follows from the representation of as or from the above expression for .
Implementation:
If n denotes the number of people, the safe position is given by the function ,where and .
Now if we represent the number in binary format, the first bit denotes and remaining bits will denote .
For example, when n=41, its binary representation is
n = 1 0 1 0 0 1
2m = 1 0 0 0 0 0
l = 0 1 0 0 1
/** * * @param n the number of people standing in the circle * @return the safe position who will survive the execution * f(N) = 2L + 1 where N =2^M + L and 0 <= L < 2^M */ public int getSafePosition(int n) { // find value of L for the equation int valueOfL = n - Integer.highestOneBit(n); int safePosition = 2 * valueOfL + 1; return safePosition; }
The general case:
The easiest way to solve this problem in the general case is to use dynamic programming by performing the first step and then using the solution of the remaining problem. When the index starts from one, then the person at shifts from the first person is in position , where n is the total number of persons.
Let denote the position of the survivor. After the -th person is killed, we're left with a circle of , and we start the next count with the person whose number in the original problem was .
The position of the survivor in the remaining circle would be if we start counting at ;
shifting this to account for the fact that we're starting at yields the recurrence
which takes the simpler form
if we number the positions from to instead.
This approach has running time , but for small and large there is another approach.
The second approach also uses dynamic programming but has running time .
It is based on considering killing k-th, 2k-th, ...,-th people as one step, then changing the numbering.
This improved approach takes the form