Open In App

Find the player to reach at least N by multiplying with any value from given range

Given an integer N, the task for two players A and B is to make the value of X ( initialized with 1) at least N by multiplying X with any number from the range [2, 9] in alternate turns. Assuming both players play optimally. the task is to find the player to obtain a value ? N first.

Examples:



Input: N = 12
Output: Player B
Explanation: 
Initially, X = 1.
A multiplies X with 9. Therefore, X = 1 * 9 = 9.
In second turn, B multiplies X with 2. Therefore, X = 9*2 = 18
Therefore, B wins.

Input: N = 10
Output: Player B



Approach: The idea is to use the concept of combinatorial game theory. Find the positions from where, if a number is multiplied with X leads to victory and also the positions which lead to a loss. Below are the steps:

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the winner
char Winner(int N)
{
    bool player = true;
 
    // Backtrack from N to 1
    while(N > 1)
    {
        int den = (player) ? 9 : 2;
        int X = N/den, Y = N%den;
        N = (Y)? X + 1: X;
        player = !player;
    }
    if (player)
      return 'B';
    else
      return 'A';
}
 
// Driver Code
int main()
{
  int N = 10;
  cout << Winner(N);
  return 0;
}
 
// This code is contributed by mohit kumar 29.




// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to find the winner
  static char Winner(int N)
  {
    boolean player = true;
 
    // Backtrack from N to 1
    while(N > 1)
    {
      int den = (player) ? 9 : 2;
      int X = N / den;
      int Y = N % den;
      N = (Y > 0) ? X + 1: X;
      player = !player;
    }
    if (player)
      return 'B';
    else
      return 'A';
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 10;
    System.out.print(Winner(N));
  }
}
 
// This code is contributed by 29AjayKumar




# Python program for the above approach
 
# Function to find the winner
def Winner(N):
    player = True
     
    # Backtrack from N to 1
    while N > 1:
                 
        X, Y = divmod(N, (9 if player else 2))
         
        N = X + 1 if Y else X
        player = not player
     
    if player:
      return 'B'
    else:
      return 'A'
 
# Driver Code
N = 10
print(Winner(N))




// C# program for the above approach
using System;
class GFG
{
 
  // Function to find the winner
  static char Winner(int N)
  {
      bool player = true;
 
      // Backtrack from N to 1
      while (N > 1)
      {
          int den = (player) ? 9 : 2;
          int X = N / den;
          int Y = N % den;
          N = (Y > 0) ? X + 1 : X;
          player = !player;
      }
      if (player)
          return 'B';
      else
          return 'A';
  }
 
  // Driver Code
  static public void Main()
  {
    int N = 10;
    Console.WriteLine(Winner(N));
  }
}
 
// This code is contributed by Dharanendra L V




<script>
// Javascript program for the above approach
function Winner(N)
{
    var player = Boolean(true);
 
    // Backtrack from N to 1
    while(N > 1)
    {
        var den = (player) ? 9 : 2;
        var X = parseInt(N/den), Y = parseInt(N%den);
        N = (Y)? X + 1: X;
        player = !player;
    }
    if (player)
      document.write( 'B');
    else
      document.write( 'A');
}
var N = 10;
Winner(N);
 
// This code is contributed by SoumikMondal
</script>

Output: 
B

 

Time Complexity: O(log N)
Auxiliary Space: O(1)


Article Tags :