If the total number of people is 2^n, the winner will always be the person at the starting point.
You can think of this 2^n behavior with example of 2, 4, 8 etc as a sample.
For the case of 100, the highest 2^n number inside 100 is 64, so we need to eliminate 36 and the next starting person will be the winner.
For eliminating 36 numbers, the last number being removed will be 72 and next starting point will be 73.
So, the answer is 73.
A java program to solve this puzzle is as follows:
@Test
public void testPuzzle () {
int numOfPeople = 8;
int powOfTwo = 1;
while (powOfTwo < numOfPeople) {
powOfTwo = powOfTwo * 2;
}
int winnerIndex = 1;
if (powOfTwo > numOfPeople) {
powOfTwo = powOfTwo/2;
int diff = numOfPeople - powOfTwo;
winnerIndex = diff * 2 + 1;
}
System.out.println(winnerIndex);
}
No comments:
Post a Comment