After losing in battle, a ruthless Roman General has N men left, but is cornered on all sides. His only way to escape is a small boat which can carry only two people. He has no option but to select 1 out of the remaining N men to accompany him back to Rome.
He chooses to devise a plan to decide a random winner.
He orders all N soldiers to be numbered and to form a circle in order of their numbers. Number 1 is given a sword and he must kill the next soldier (i.e. soldier Number 2) and give the sword to the next surviving soldier (i.e. soldier Number 3). All soldiers have t do the same until one of them is left. Which soldier survives in the end?
INPUT:
The number N denoting the number of soldiers.
OUTPUT:
The number of the last surviving soldier.
CONSTRAINTS: 0<N<1000000
SAMPLE INPUT #1: 100
SAMPLE OUTPUT #1: 73
SAMPLE INPUT #2: 10
SAMPLE OUTPUT #2: 5
This question is from HP Code Wars 2019 Bangalore.
My issue:
I wrote the code for odd and even inputs of N separately and it works so logically I do know the solution but do I? I tried making a general code for both odd and even inputs and it works but only partially i.e. only for odd inputs and not the even inputs i.e. shows the wrong output. I'm not able to figure out the mistake in my logic and I need help with the entire code for a general case whether ODD or EVEN input.
Don't suggest to use my own ODD and EVEN code like this:
Thanks!Code:
int N=cin.get();
if ( N%2==0 )
{
//EVEN NUMBER CODE
}
else
{
//ODD NUMBER CODE
}