How can you guys be so fast on Nim Game (C++)


  • 0
    S

    Here is my code, and i think it is similar to most of the answer.

     bool canWinNim(int n) {
        return n % 4 == 0 ? false : true;
        
    }
    

    but when i subscribe the runtime

    You are here! 
    Your runtime beats 5.50% of cpp submissions.
    

    but i didn't see some others solution that differs from mine? So what's wrong?


  • 1
    A

    You can check for whether any number is divisible by 2 or 4 or 8... (any power of 2) by doing bit manipulations which are super-fast. A number divisible by 2 will always have last bit = 0, divisible by 4 will have last 2 bits 0, divisible by 8 will have last 3 bits 0 etc...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.