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.
public static String counterGame(long n) {
if (n == 1) return "Richard";
int turn = 0;
while (n > 1) {
if ((n & (n - 1)) == 0) { // Check if n is a power of two
n /= 2;
} else {
n -= Long.highestOneBit(n); // Get the largest power of 2 less than or equal to n
}
turn++;
}
return (turn % 2 == 0) ? "Richard" : "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 →
Java solution class Result {
}