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.
publicstaticStringcounterGame(longn){intmoves=0;// Reduce n to 1while(n>1){// Check if n is a power of 2if((n&(n-1))==0){n/=2;}else{n-=Long.highestOneBit(n);}moves++;}// Find the winner based on the number of movesif(moves%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 →
Java, time complexity is O(log n)