3 December 2014

Quiz 65: Find index of boy holding the ball

Problem:

There are N boys playing a game. They form a circle and have index from 1 to N clockwise. At the start, boy-1 is holding the ball. He says 1 and gives next person. next person says 2 and gives ball to next to next ie 2nd boy to him and so on. Games ends after N-1 passes. Print N-1 indexes showing index of boy holding the ball

Input Format: 

N


Output Format: 

N-1 indexes showing index of boy holding the ball


Constraints: 

None

Sample Input

11

Sample Output:

2 4 7 11 5 11 7 4 2 1

Explanations:

1+1=2,2+2=4,4+3=7,7+4=11,11+5=16-11=5 and so on



Solution:

chomp($n=<STDIN>);
$c=1;
for($i=1;$i<$n;$i++)
{
$c=$c+$i;
$c=$c%$n;
if($c==0){$c=$n;}
print "$c ";
}

Tips:

Take care of condition when ball is with last person to reset counter to 1.

No comments:

Post a Comment