Counter game

  • + 0 comments

    Java solution class Result {

    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";
    }
    

    }