0% found this document useful (0 votes)
18 views

Input: (Plaintext Link)

The chief of thieves tricks 40 thieves into a cave filled with wolves. With no weapons, the thieves decide to kill themselves by having every third person die, but the chief wants to survive. For a new game based on this story, every second person will leave the game instead of die. The program takes the number of participants as input and outputs the position of the surviving participant.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Input: (Plaintext Link)

The chief of thieves tricks 40 thieves into a cave filled with wolves. With no weapons, the thieves decide to kill themselves by having every third person die, but the chief wants to survive. For a new game based on this story, every second person will leave the game instead of die. The program takes the number of participants as input and outputs the position of the surviving participant.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1:

Ali baba did a trick on the forty thieves and was able to trap them inside a big cave which was the home
of wild wolves. The thieves are without any weapons, only the chief of the thieves has knife. With no
weapons they will not be able to fight with the wolves, so they decide to kill themselves rather than being
eaten alive.

They all decide that they will stand in a circle and they every third person will kill himself but the chief of
the thieves does not like this idea and has no intention of killing himself. He calculates where should he
stand so that he is the last one left.

HackerMan wants to build a game based on this story, but instead of killing he decides that the participant
will leave the game, and instead of every 3rd position it will be every 2nd position. Of course the number
of participants will be much more than 40 in this game.

Input

The first line of input is an integer N (1 <= N <= 1000) that specifies the number of test cases. After that
every line contains an integer X (5 <= X <= 100000000) which is the number of participants in the game.

Output

For each test case generate a line containing the position of the participant who survives. Assume that the
participants have serial numbers from 1 to n and that the counting starts with person 1, i.e., the first
person leaving is the one with number 2.

Sample Input (Plaintext Link)


4
5
11
45
23987443
Sample Output (Plaintext Link)
3
7
27
14420455

Solution:

import java.util.ArrayList;
import java.util.Scanner;
class Game2 {

/**
* @param args
*/
public static long calculateLeftOVer(long totalNumberOfThieves){

if(totalNumberOfThieves == 1) return 1;
long currentThief = 1;
long diffOfPositions = 2;

while(totalNumberOfThieves!=1){
if(totalNumberOfThieves % 2!=0){
currentThief = currentThief + diffOfPositions;
}
totalNumberOfThieves = totalNumberOfThieves/2;
diffOfPositions = diffOfPositions * 2;
}
return currentThief;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int TestCase=sc.nextInt();
for(int i=0;i<TestCase;i++)
{
long size=sc.nextInt();
System.out.println(calculateLeftOVer(size));
}

You might also like