Tom and Jerry | Practice | GeeksforGeeks

Tom and Jerry being bored in this pandemic, decides to play a game. Given an integer N. On each player’s turn, that player makes a move by subtracting a divisor of current N (which is less than N) from current N, thus forming a new N for the next turn. The player who does not have any divisor left to subtract loses the game.
The game begins with Tom playing the first move. Both Tom and Jerry play optimally. The task is to determine who wins the game. Return 1 if Tom wins, else return 0.
 
Example 1:
Input:
N = 2
Output:
1
Explanation:
Tom subtracts 1 from N to make N = 1.
Now, Jerry isn't left with any possible
turn so Tom wins the game, and therefore
the Output is 1.
Example 2:
Input:
N = 4
Output:
1
Explanation:
1st turn: Tom substract 1 from N as 1 is 
a divisor of 4 and  less than 4.

2nd turn: N=3, Jerry has to substract 1 as 1 i
s the only divisor of 3 which is less than 3.

3rd turn: N=2 ,Tom substract 1 as 1 is the 
only divisor of 2 which is less than 2.

4th turn: N=1, Jerry can't substract any value.
So, Tom wins.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function numsGame() which takes an Integer N as input and returns 1 if Tom wins else returns 0.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N ≤ 108
0
prateek jha 13 months ago
Which divisor do we have to subtract from N
-2
shreyanshikunda3 months ago
 PYTHON
 if (N%2==0):           return '1'       else :           return '0'       
0
neeramrutia3 months ago
class Solution { public:   int numsGame(int N) {       // code here       if(N%2==0)       {           return 1;       }       else       {           return 0;       }   }}; 
0
ravipathak9343 months ago
 if(N%2==0)       {           return 1;       }       else       {           return 0;       }
0
ayushnautiyal11103 months ago
C++ ONELINE SOLUTION:)
int numsGame(int N) {       return N%2==0?1:0;   }
0
tadvaith184 months ago
c ++ solution
 
class Solution { public:   int numsGame(int N) {       // code here       if (N % 2 == 0)       {           return 1;       }       return 0;   }}; 
 
 
0
kamal2507s6 months ago
There are other ways for tom to win also. but if the goal is not to win fast and while considering he is playing to win, if he would subtract 1 from an even number everytime ,we will always win.
-4
badgujarsachin837 months ago
  public:
    int numsGame(int N) {
        // code here
        if(N%2==0){
            return 1;
        }else{
            return 0;
        }
    }
};
+3
masfandyar1237 months ago
I think this problem is very poorly explained. Please elaborate how tom wins when integer is 4.
1st turn Tom : divisor = 2 : so 4 - 2 = 2.
2nd turn Jerry : divisor = 1 : so 2 - 1 = 1.
3rd turn Tom : has no divisor left so he loses.
Curious to know if this is a mistake or am I missing something.
0
Subhash Narkedimilli9 months ago
Subhash Narkedimilli
execution in 0.1 secondsif N%2==0:return 1else:return 0
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.