
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.
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.
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.
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
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:
The idea is to use bit masking as the maximum integer can vary up to 20 only. There are 2 choices available with a state - pick a number or not to pick a number. So, there can be a maximum of 2^20 different states. We will store the integers that are used till now in an array, and can also find the running total with this array.
The rest of the steps are the same as the recursive approach.
The steps are as follows:
The idea is to use bit masking as the maximum integer can vary up to 20 only. There are 2 choices available with a state - pick a number or not to pick a number. So, there can be a maximum of 2^20 different states. We will store the integers that are used till now in an array, and can also find the running total with this array.
The steps are as follows: