Input: arr[] = {1, 2, 3, 4, 5, 7}, X = 7
Output: A
Explanation: Player A choose arr[5] = 7 from the arr[] and update X to 7 - 7 = 0.
Therefore, A wins the game because B can't make any move on 0.
Input: arr[] = {1}, X = 1
Output: A
Explanation: Player A choose arr[0] = 1 from the arr[] and update X to 1 - 1 = 0.
Therefore, A wins the game because B can't make any move on 0.
Let's see the cases for X = [1, 2, 3, . . . ., 7]
For X = 1: arr[] will be: {1}
- Player A can subtract 1 from X .So, that X becomes 0 and player B can't make any move on X further.Therefore, player A wins.
For X = 2: arr[] will be: {1, 2}
- Player A can subtract 2 from X .So, that X becomes 0 and player B can't make any move on X further.Therefore, player A wins.
For X = 3: arr[] will be: {1, 2, 3}
- Player A can subtract 3 from X .So, that X becomes 0 and player B can't make any move on X further.Therefore, player A wins.
For X = 4: arr[] will be: {1, 2, 3, 4}
- Player A can subtract 4 from X .So, that X becomes 0 and player B can't make any move on X further.Therefore, player A wins.
For X = 5: arr[] will be: {1, 2, 3, 4, 5}
- Player A can subtract 5 from X .So, that X becomes 0 and player B can't make any move on X further.Therefore, player A wins.
For X = 6: arr[] will be: {1, 2, 3, 4, 5}
All possible cases:
- (i) Player A makes X as = (6-1) = 5, Player B can directly subtract 5.Hence B wins.
- (ii) Player A makes X as = (6-2) = 4, Player B can directly subtract 4.Hence B wins.
- (iii) Player A makes X as = (6-3) = 3, Player B can directly subtract 3.Hence B wins.
- (iv) Player A makes X as = (6-4) = 2, Player B can directly subtract 2.Hence B wins.
- (v) Player A makes X as = (6-5) = 1, Player B can directly subtract 1.Hence B wins.
In all above cases it can be verified that there is no such move possible so that A can be winner of the game.
For X = 7: arr[] will be: {1, 2, 3, 4, 5, 7}
- Player A can subtract 7 from X .So, that X becomes 0 and player B can't make any move on X further.Therefore, player A wins.
- Till, Now We observe a pattern that if X is a multiple of 6, Then B wins else A wins.
Below is the implementation of the above approach.