We are in the endgame now. Tony and Thanos are fighting. Before the war, Captain Marvel secretly gave Tony a superpower that she took from the other universe. They went through some days of the war but Tony’s power was enough to fight against Thanos but not to defeat him and vice versa. So, they decided to play a number game.
In the game, there are two integers - ‘maximumNumber’, and ‘target’. In each turn, one player can choose an integer and add it to ‘runningTotal’. The player who makes the ‘runningTotal’ equal to or more than the ‘target’ wins.
The rules of the game are:
1. One can use any number between 1 and ‘maximumNumber’.
2. One integer can’t be used more than once.
3. The running total = 0 initially.
4. Both players play optimally.
Thanos offered Tony to make the first move and he will choose the two integers. But the avengers are not satisfied. So, all came to a conclusion that after choosing the integers if Tony is not satisfied with the two integers, he will tell Thanos to rechoose two different integers. Tony is completely exhausted but doesn’t want to lose as it can cost us the complete universe. Help Tony to figure out if he can win or not from the two integers that Thanos chose.
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first line contains two space-separated integers ‘maximumNumber’, and ‘target’, which denotes the maximum number that a player can choose, and the target number which whoever takes the running total more than the target wins.
Output Format:
For each test case, print 1 if it is possible to win the game with the chosen integers. Otherwise, print 0.
Print the output of each test case in a separate line.
Note:
You are not required to print the expected output, it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= maximumNumber <= 20
1 <= target <= 200
Time Limit: 1 sec
2
5 2
5 6
1
0
In the first test case, It’s Tony’s turn, he can choose any number between 1 and 5. Initially runningTotal = 0. He will choose any number between 2 and 5 and win the game. So, the output is 1.
In the second test case, Initially runningTotal = 0. It’s Tony’s turn, he has to choose any number between 1 and 5. Whichever number he will choose runningTotal will lie in the range 1 to 5. In the next turn, Thanos will choose a number between 1 and 5 which Tony didn’t choose in his turn, and win the game. So, the output is 0.
2
10 15
10 22
1
0
Can you figure out the base conditions for the game? Under what conditions one can win?
The idea is to use a recursive approach. We will use 'TARGET' to find what is the total sum remaining in the running total. For example, if 'TARGET' = 20 and player 1 chooses 5, then we will subtract 5 from the 'TARGET'.
If the 'TARGET' becomes less than or equal to 0, it means that the current player loses and the player who played the last turn has won already.
As there are different states and players are playing optimally, we need to traverse from 1 to the 'MAXIMUM_NUMBER' and check for the number which we haven’t used till now. If any of the further recursive calls return false it means the opponent can’t win, it means the current player wins.
But there is a base condition:
What if the sum of all the integers from 1 to 'MAXIMUM_NUMBER' is not sufficient to add up to 'TARGET'?
Base Condition 1: In this case, both the players can’t win. So, the answer will be false. We will check the given condition before calling the recursive function.
There are some more base conditions:
The steps are as follows:
O(M^(M+1)), where M is the maximum number given.
As we are having recursive calls of maximum depth M and for each call there are at most M calls. Hence, the overall time complexity is O(M^(M+1)).
O(M^(M+1)), where M is the maximum number given.
As we are having recursive calls of maximum depth M and for each call there are at most M calls. Hence, the overall space complexity is O(M^(M+1)).