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

HackerRank Counter game problem solution


Problem solution in Python.

from math import *

def get_turn(turns):
    return "Richard" if turns % 2 == 0 else "Louise"

def npot(x):
    exp = floor(log(x, 2))
    r = int(pow(2, exp))
    return r
    
def run_game(n):
    turns = 0
    while(n > 1):
        np = npot(n)
        if np == n:
            n >>= 1
        else:
            n -= np
        turns += 1
    print(get_turn(turns))

t = int(input())
for i in range(t):
    n = int(input())
    run_game(n)


Problem solution in Java.

import java.io.*;
import java.util.*;
import java.math.BigInteger;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int numTests = in.nextInt();
        for (int i = 0; i < numTests; ++i) {
            findWinner(in);
        }
    }
    
    private static void findWinner(Scanner in) {
        String counterAsString = in.next();
        BigInteger counter = new BigInteger(counterAsString);
        int bits = counter.bitLength();
        int moves = -1;
        for (int i = 0; i < bits; ++i) {
            if (!counter.testBit(i)) {
                ++moves;
            } else {
                moves += counter.bitCount();
                break;
            }
        }
        if (moves % 2 == 0) {
            System.out.println("Richard");
        } else {
            System.out.println("Louise");
        }
    }
}


Problem solution in C++.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ll;

ll lpless(ll N){
    ll pow = 1;
    while(pow <= N/2) pow*=2;
    return pow;
}

ll next(ll N){
    ll pow = lpless(N);
    if(N==pow) return N/2;
    return N-pow; 
}

int main() {
    int T;
    cin >> T;

    for(int t=0;t<T;t++){
        ll N;
        cin >> N;
        int ans = 0;
        while(N!=1){
            ans++;
            N=next(N);
        }
        if(ans%2==0) cout << "Richard" << endl;
        else cout << "Louise" << endl;
    }
    
    return 0;
}


Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int isPow2(long unsigned  int);
unsigned long int largePow(long unsigned int);
int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    int t,i,win;
    long unsigned int n;
    scanf("%d",&t);
    for(i=0;i<t;++i)
        {
        win=0;
        scanf("%lu",&n);
        if(n==1)
            printf("Richard\n");
        else
            {
            while(n!=1)
                {
                if(isPow2(n))
                    n>>=1;
                else
                    n-=largePow(n);
                ++win;
            }
        }
        if(win%2==0)
            printf("Richard\n");
        else
            printf("Louise\n");
    }
    return 0;
}
int isPow2(long unsigned int n)
    {
    return !(n&(n-1));
}
long unsigned int largePow(long unsigned int n)
    {
    long unsigned int m;
    while(n)
        {
        m=n;
        n=n&(n-1);
    }
    return m;
}