Determine winner of the Game by arranging balls in a row

Given the number of small and large balls N and M respectively, the task is to find which player wins if both the player X and Y play optimally by making the following two moves:

The player who cannot make a move loses the game. The task is to find which player won the game the given number of balls. It is given that player X always starts first.

Examples:

Input: N = 3 M = 1
Output:
Explanation:    
Since there is only 1 large ball, player X will puts it first. 
Player Y puts a small ball adjacent to large one, because he can put a different type of ball adjacently. 
Then player X puts a small ball (same type). Now player Y can’t keep a ball and hence cannot make a move
Sequence = {large, small, small}, hence the score would X = 2 and Y = 1.
Hence, player X wins.

Input: N = 4 M = 4 
Output: Y     
Explanation:  
Player X first move = small, Player Y first move = large ( N = 4, M = 3 )
Player X second move = large, Player Y second move = small ( N = 3, M = 2 )
Player X third move = small, Player Y third move = large ( N = 2, M = 1 )
Player X fourth move = large, Player Y fourth move = small ( N = 1, M = 0 ).
Hence, player Y wins as player X can’t make a move. 
 



Approach: Follow the steps given below to solve the problem:

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Function to find the winner of the
// Game by arranging the balls in a row
void findWinner(int n, int m)
{
    int X = 0;
    int Y = 0;
 
    // Check if small balls are greater
    // or equal to the large ones
    if (n >= m) {
 
        // X can place balls
        // therefore scores n-1
        X = n - 1;
        Y = m;
    }
 
    // Condition if large balls
    // are greater than small
    else {
 
        // X can have m-1 as a score
        // since greater number of
        // balls can only be adjacent
        X = m - 1;
        Y = n;
    }
 
    // Compare the score
    if (X > Y)
        cout << "X";
 
    else if (Y > X)
        cout << "Y";
 
    else
        cout << "-1";
}
 
// Driver Code
int main()
{
    // Given number of small balls(N)
    // and number of large balls(M)
    int n = 3, m = 1;
 
    // Function call
    findWinner(n, m);
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
class GFG{
   
// Function to find the winner of the
// Game by arranging the balls in a row
static void findWinner(int n, int m)
{
    int X = 0;
    int Y = 0;
  
    // Check if small balls are greater
    // or equal to the large ones
    if (n >= m)
    {
  
        // X can place balls
        // therefore scores n-1
        X = n - 1;
        Y = m;
    }
  
    // Condition if large balls
    // are greater than small
    else
    {
  
        // X can have m-1 as a score
        // since greater number of
        // balls can only be adjacent
        X = m - 1;
        Y = n;
    }
  
    // Compare the score
    if (X > Y)
        System.out.print("X");
  
    else if (Y > X)
        System.out.print("Y");
  
    else
        System.out.print("-1");
}
  
// Driver Code
public static void main(String[] args)
{
    // Given number of small balls(N)
    // and number of large balls(M)
    int n = 3, m = 1;
  
    // Function call
    findWinner(n, m);
}
}
 
// This code is contributed by rock_cool
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to find the winner of the
# Game by arranging the balls in a row
def findWinner(n, m):
    X = 0;
    Y = 0;
 
    # Check if small balls are greater
    # or equal to the large ones
    if (n >= m):
 
        # X can place balls
        # therefore scores n-1
        X = n - 1;
        Y = m;
 
    # Condition if large balls
    # are greater than small
    else:
 
        # X can have m-1 as a score
        # since greater number of
        # balls can only be adjacent
        X = m - 1;
        Y = n;
     
    # Compare the score
    if (X > Y):
        print("X");
 
    elif(Y > X):
        print("Y");
 
    else:
        print("-1");
 
# Driver Code
if __name__ == '__main__':
   
    # Given number of small balls(N)
    # and number of large balls(M)
    n = 3;
    m = 1;
 
    # Function call
    findWinner(n, m);
 
# This code is contributed by Rohit_ranjan
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
class GFG{
   
// Function to find the winner of the
// Game by arranging the balls in a row
static void findWinner(int n, int m)
{
    int X = 0;
    int Y = 0;
  
    // Check if small balls are greater
    // or equal to the large ones
    if (n >= m)
    {
  
        // X can place balls
        // therefore scores n-1
        X = n - 1;
        Y = m;
    }
  
    // Condition if large balls
    // are greater than small
    else
    {
  
        // X can have m-1 as a score
        // since greater number of
        // balls can only be adjacent
        X = m - 1;
        Y = n;
    }
  
    // Compare the score
    if (X > Y)
        Console.Write("X");
  
    else if (Y > X)
        Console.Write("Y");
  
    else
        Console.Write("-1");
}
  
// Driver Code
public static void Main(String[] args)
{
    // Given number of small balls(N)
    // and number of large balls(M)
    int n = 3, m = 1;
  
    // Function call
    findWinner(n, m);
}
}
 
// This code is contributed by sapnasingh4991
chevron_right

 
 

Output: 
X







 

 

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

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :