Counter game – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
1 Month Preparation Kit Solutions – HackerRank
C++ Counter game HackerRank Solution
#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;
}
Java Counter game HackerRank Solution
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");
}
}
}
Python 3 Counter game HackerRank Solution
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)
JavaScript Counter game HackerRank Solution
C Counter game HackerRank Solution
#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;
}
Leave a comment below