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:

- Player X will try to keep the
**same type of ball**i.e., small followed by another small ball or large ball is followed by a large ball. - Player Y will put
**different types of ball**i.e., small followed by a large ball or vice-versa.

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 = 1Output:XExplanation:

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 = 4Output:YExplanation:

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:

- Check if the
**number of small balls is greater than or equals the****number of large balls**, then player**X**will have the score**N – 1**since the player**X**will place small ball first then player**Y**will immediately put a different type of ball and now player**X**will put the same as player**Y**type’s ball. This process will continue until the end of the game. - Otherwise, if the
**large balls are greater than small balls**then player X will have**M – 1**score and player**Y**will have a score as**N**, the approach will be the same as mentioned above. Here the player**X**will first start by keeping large ball first. - In the end, compare the scores of both the players and print the winner as output.

Below is the implementation of the above approach:

`// 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*

`// 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*

`# 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*

`// 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*

*filter_none*

**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.

## Recommended Posts:

- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Determine the winner of a game of deleting Characters from a String
- Ways to arrange Balls such that adjacent balls are of different types
- Game Theory (Normal form game) | Set 2 (Game with Pure Strategy)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Two Balls Reachability Game
- Find the winner in nim-game
- Find the winner of the Game to Win by erasing any two consecutive similar alphabets
- Find the winner of the game with N piles of boxes
- Predict the winner of the game | Sprague-Grundy
- Predict the winner in Coin Game
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Find the winner of the Game
- Winner in the Rock-Paper-Scissor game using Bit manipulation
- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- Find the winner of the game based on greater number of divisors
- Find the winner of the Game of removing odd or replacing even array elements
- Predict the winner of a card game of removing K cards in each turn such that Bitwise AND of K and size of pile is 0

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.