Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Counter game problem solution

YASH PAL, 31 July 2024

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("Richardn");
        else
            {
            while(n!=1)
                {
                if(isPow2(n))
                    n>>=1;
                else
                    n-=largePow(n);
                ++win;
            }
        }
        if(win%2==0)
            printf("Richardn");
        else
            printf("Louisen");
    }
    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;
}

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes