Tuesday, 16 July 2013

Musical Pillow Game - Program in C#



Musical Pillow 

Problem Statement

        people are playing the musical pillow game in which people sit in a circle. Starting from the first person, they transfer the pillow to the person to their Right in the clockwise manner.Pillow is passed in increasing sequence like from 1 to 2 , 2 to 3, last  again start from n to 1 game starts,by playing  a song people has to pass the pillow to next one , If the song is stopped at a particular time for example every 2 sec . the person who have the pillow at the time of song stopped would be  eliminated. like wise keep on doing finally one person will be winner.


   Input : total number of people playing the game and the duration of the song in seconds

Examples

Example 1: Let there are 5 people and duration of the song is 3 seconds, then people will be eliminated in the following order: 3,1, 5,2 and person in the  4 will be the winner.

Example 2: If there are 6 people in the game, and song duration is 7 seconds then people will be eliminated in following order: 1,3,6,2,4 and winner will be the person with named as. 5.



class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Check(6, 7));
        Console.Read();
    }


    public static int Check(int tot, int sg)
    {
            List arr = new List();
            for (int i = 0; i < tot; i++)
            {
                arr.Add(i + 1);
            }

            int indx = -1;
            int count = 0;
            int removed = 0;
           
            while (arr.Count > 1)
            {
                indx++;
                count++;
                if (count == sg)
                {
                    arr.Remove(arr[indx]);
                    indx = indx - 1;
                    count = 0;
                }
                if (indx == arr.Count - 1)
                    indx = -1;
            }
            return arr[0];
    }
}




Output : 5