[Hacker Rank] Counter game

Counter game

  • Time : O(logn)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
string counterGame(long n) {
bool louise = true;
while(n != 1) {
long lg = log2(n);
if(1ll<<lg == n) n>>=1;
else n -= 1ll<<lg;
louise = !louise;
}
return !louise ? "Louise" : "Richard";
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/14/PS/HackerRank/counter-game/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.