Counter Game HackerRank Solved!

https://www.hackerrank.com/challenges/counter-game

My idea is following:
1. check if input is power of 2.
2. if it is a power of 2, shift the input until it reach to 1, and winner is who ever reaches to 1 at the moment.
3. if it is not, try to find the max of a power of 2 less than input and subtract it from the input. then repeat steps.


        static void Main(String[] args)
        {
            var q = Int32.Parse(Console.ReadLine());
            for (int i = 0; i < q; i++)
            {
                ulong input = UInt64.Parse(Console.ReadLine());
                int count = 0;

                while (true)
                {

                    if (input == 1)
                    {
                        break;
                    }

                    if ((input & (input - 1)) == 0)
                    {
                        while (input > 1)
                        {
                            input >>= 1;
                            count++;
                        }
                    }
                    else
                    {
                        ulong nextValue = 1;
                        ulong temp = input;
                        while (temp > 1)
                        {
                            nextValue <<= 1;
                            temp >>= 1;
                        }
                        input -= nextValue;
                        count++;
                        //Console.WriteLine(input);
                    }
                }
                Console.WriteLine(count%2 == 1 ? "Louise" : "Richard");
            }
        }

Comments

Popular Posts