TopcoderArchive
Archive
Algorithm problem

WhoWillWin

Problem statement, definition, constraints, and public examples.

Problem Statement

Several children are playing a game. They form a circle and start counting from the first person. Starting with the number 1, and going in a clockwise direction, each person in the circle counts 1 higher than the previous person (so the first person says 1, the second person says 2, etc...). Each person who says an even number immediately goes out of the circle. The game stops when all the players but one are out. That child is the winner.
For example:
If there are 5 children, they will start counting from the first, so the second and fourth children will both be out. Then, the 5th will have number 5, and, continuing from the beginning, the first will be out. Now only the 3rd and 5th are left, so the 3rd will receive number 7 and win, because the 5th will receive number 8.

Person  Number
------  ------
1	1
2       2        Person 2 is out
3       3
4       4        Person 4 is out
5       5
1       6        There are only 5 people, so we continue with Person 1. He is out.
3       7        Now only Person 3 and Person 5 are inside the circle.
5       8        Person 5 is out.
3       9        Person 3 is the winner.
You will be given int N, the number of the children, and your method should return the the winner.

Definition

Class:
WhoWillWin
Method:
determineWinner
Parameters:
int
Returns:
int
Method signature:
int determineWinner(int N)
(be sure your method is public)

Constraints

  • N will be between 1 and 1000 inclusive

Examples

  1. 5
    Returns: 3
    Example from the problem statement
  2. 1
    Returns: 1
    Only one person - he already won this game.
  3. 130
    Returns: 5
  4. 723
    Returns: 423
  5. 512
    Returns: 1
Back to all problems