We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def counterGame(n: Long): String = {
// Write your code here
var turn = 0
var currentValue = BigInt(n)
// if counter 1, richard wins
while (currentValue > 1) {
// check if its a power of 2. if it is then divide it by 2
if((currentValue & (currentValue-1)) == 0) currentValue /= 2
// else reduce by next number which is value of2
else {
val highestPowerOf2 = BigInt(1) << (currentValue.bitLength - 1)
currentValue -= highestPowerOf2
}
turn += 1
}
if(turn % 2 == 0) return "Richard" else return "Louise"
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Counter game
You are viewing a single comment's thread. Return to all comments →
Scala
def counterGame(n: Long): String = { // Write your code here var turn = 0 var currentValue = BigInt(n) // if counter 1, richard wins