Counter game HackerRank Solution

In this Counter game HackerRank solution, Louise and Richard have developed a numbers game. They pick a number and check to see if it is a power of . If it is, they divide it by . If not, they reduce it by the next lower number which is a power of . Whoever reduces the number to  wins the game. Louise always starts.

Given an initial value, determine who wins the game.

Example

It’s Louise’s turn first. She determines that  is not a power of . The next lower power of  is , so she subtracts that from  and passes  to Richard.  is a power of , so Richard divides it by  and passes  to Louise. Likewise,  is a power so she divides it by  and reaches . She wins the game.

Update If they initially set counter to , Richard wins. Louise cannot make a move so she loses.

Function Description

Complete the counterGame function in the editor below.

counterGame has the following parameter(s):

  • int n: the initial game counter value

Returns

  • string: either Richard or Louise

Input Format

The first line contains an integer , the number of testcases.
Each of the next  lines contains an integer , the initial value for each game.

Constraints

Sample Input

1
6

Sample Output

Richard

Explanation

  • As  is not a power of , Louise reduces the largest power of  less than  i.e., , and hence the counter reduces to .
  • As  is a power of , Richard reduces the counter by half of  i.e., . Hence the counter reduces to .

As we reach the terminating condition with , Richard wins the game.

Counter game HackerRank solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in C#

public static string counterGame(long n)
    {
        bool louiseWins = false;
        while (n>1)
        {
            var log = Math.Log2(n);
            if ((log % 1) != 0)
            {
                log = Math.Floor(log);
                var next = (long)Math.Pow(2, log);
                n = (n - next);
            }
            else
            {
                 n = n / 2;
            }
            louiseWins = !louiseWins;
        }
        return louiseWins ? "Louise" : "Richard";
    }

Problem Solution in Python

def counterGame(n):
    louise = False
    while n > 1:
        if n & (n-1) == 0:
            # is power of 2
            n = n >> 1
            louise = not louise
        else:
            x = 1
            while x < n:
                x = x << 1 # next power of 2
            x = x >> 1
            n = n - x
            louise = not louise
    return 'Louise' if louise else 'Richard'

Problem Solution in JavaScript

function counterGame(n) {
    let turn = 0;
    while (n > 1) {
        turn++;
        // if is power of 2, divide by 2
        if ((n & (n - 1)) === 0) n >>>= 1;
        // else subtract next lowest power of 2
        else {
            let p = n;
            while ((p & (p - 1)) !== 0) {
                p = p & (p - 1);
            }
            n -= p;
        }
    }
    return turn % 2 === 0 ? 'Richard' : 'Louise';
}

Problem Solution in C++

string counterGame(long n) {
    bool player = true;
    while(fabs(floor(log2(n)) - log2(n)) > 0.001)
    {
        n = n - pow(2, floor(log2(n)));
        player = !player;
    }
   
    if (int(log2(n)) % 2 == 1)
        player = !player;
   
    return player ? "Richard" : "Louise";
}
Solve original Problem on HackerRank here. Checkout more HackerRank Problems

Leave a Comment