Given an array **arr[]** consisting of **N** integers, each representing size of a pile of stones. The task is to determine the winner of the game when two players, **A** and **B**, play a game optimally as per the following conditions:

- Player
**A**always starts the game. - In one move, a player may remove any number of stones (at least 1), from the first non-empty pile with minimal index.
- The first player who cannot make a move loses the game.

Print “**A”** if player A wins the game. Otherwise, print “**B”**.

**Examples:**

Input:arr[] = {1, 1, 1, 1, 1, 1}Output:BExplanation:

Here, each pile has only one stone so A and B will alternatively remove one stone each.

A removes 1 stone from 1st pile, B removes 1 from 2nd pile and so on.

Last stone in 6th pile is removed by B.

Since A has no choice left, B wins the game.

Input:arr[] = {1, 1, 2, 1}Output:AExplanation:

Here,

A removes 1 stone from 1st pile,

B removes 1 from 2nd pile,

A removes only 1 from 3rd pile,

and now B is forced to remove 1 from 3rd pile.

Last stone in 4th pile is removed by A.

Since B has no choice left, A wins the game.

**Approach:** The idea is to figure out the optimal ways that players should follow to win the game. Below are two ways to play optimally:

- For all piles except the last one, if there are
**K**stones on a pile then the current player will pick only (**K – 1**) stones (only if K > 1) so that another player is forced to pick the remaining**1**stone. This guarantees the current player to get a chance to pick stones from the next pile and eventually win. - For the last pile, if it has
**K**stones, then all**K**stones will be picked by the current player so that the other player does not get a chance to pick stones. This eventually guarantees the current player to win.

Follow the steps below to solve this problem:

- Initially, assuming that the player will remove all stones from the current pile,
**A**will be the winner if the size of the array is**odd**and**B**if the size is**even**. - Now, iterate over the range
**[0, N – 2]**and check the current index and number of stones present on the current pile to decide which player will win. - While traversing, if the index is even, A will get a chance to pick a stone. Otherwise, B will get a chance to pick stones.
- If the current index is even and the number of stones is greater than
**1**, then the winner is set to**B**. Update winner to**A**. - If the current index is odd and the number of stones exceeds
**1**, the winner is set to be**A**. Then, update the winner to**B**. - Finally, print the winner of the game.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the winner` `// of game between A and B` `void` `findWinner(` `int` `a[], ` `int` `n)` `{` ` ` `// win = 1 means B is winner` ` ` `// win = 0 means A is winner` ` ` `int` `win = 0;` ` ` `// If size is even, winner is B` ` ` `if` `(n % 2 == 0)` ` ` `win = 1;` ` ` `// If size is odd, winner is A` ` ` `else` ` ` `win = 0;` ` ` `for` `(` `int` `i = n - 2; i >= 0; i--) {` ` ` `// Stone will be removed by B` ` ` `if` `(i % 2 == 1) {` ` ` `// If B wants to win` ` ` `// B will take n-1 stones` ` ` `// from current pile having` ` ` `// n stones and force A to` ` ` `// pick 1 stone` ` ` `if` `(win == 0 && a[i] > 1)` ` ` `win = 1;` ` ` `}` ` ` `// Stone will be removed by A` ` ` `else` `{` ` ` `// If A wants to win` ` ` `// A will take n-1 stones from` ` ` `// current pile having n stones` ` ` `// and force B to pick 1 stone` ` ` `if` `(win == 1 && a[i] > 1)` ` ` `win = 0;` ` ` `}` ` ` `}` ` ` `// Print the winner accordingly` ` ` `if` `(win == 0)` ` ` `cout << ` `"A"` `;` ` ` `else` ` ` `cout << ` `"B"` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given piles of stone` ` ` `int` `arr[] = { 1, 1, 1, 2 };` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `// Function Call` ` ` `findWinner(arr, N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to find the winner` `// of game between A and B` `static` `void` `findWinner(` `int` `a[], ` `int` `n)` `{` ` ` ` ` `// win = 1 means B is winner` ` ` `// win = 0 means A is winner` ` ` `int` `win = ` `0` `;` ` ` `// If size is even, winner is B` ` ` `if` `(n % ` `2` `== ` `0` `)` ` ` `win = ` `1` `;` ` ` `// If size is odd, winner is A` ` ` `else` ` ` `win = ` `0` `;` ` ` `for` `(` `int` `i = n - ` `2` `; i >= ` `0` `; i--)` ` ` `{` ` ` ` ` `// Stone will be removed by B` ` ` `if` `(i % ` `2` `== ` `1` `) ` ` ` `{` ` ` ` ` `// If B wants to win` ` ` `// B will take n-1 stones` ` ` `// from current pile having` ` ` `// n stones and force A to` ` ` `// pick 1 stone` ` ` `if` `(win == ` `0` `&& a[i] > ` `1` `)` ` ` `win = ` `1` `;` ` ` `}` ` ` `// Stone will be removed by A` ` ` `else` ` ` `{` ` ` ` ` `// If A wants to win` ` ` `// A will take n-1 stones from` ` ` `// current pile having n stones` ` ` `// and force B to pick 1 stone` ` ` `if` `(win == ` `1` `&& a[i] > ` `1` `)` ` ` `win = ` `0` `;` ` ` `}` ` ` `}` ` ` `// Print the winner accordingly` ` ` `if` `(win == ` `0` `)` ` ` `System.out.print(` `"A"` `);` ` ` `else` ` ` `System.out.print(` `"B"` `);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Given piles of stone` ` ` `int` `arr[] = { ` `1` `, ` `1` `, ` `1` `, ` `2` `};` ` ` `int` `N = arr.length;` ` ` `// Function call` ` ` `findWinner(arr, N);` `}` `}` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function to find the winner` `# of game between A and B` `def` `findWinner(a, n):` ` ` ` ` `# win = 1 means B is winner` ` ` `# win = 0 means A is winner` ` ` `win ` `=` `0` ` ` `# If size is even, winner is B` ` ` `if` `(n ` `%` `2` `=` `=` `0` `):` ` ` `win ` `=` `1` ` ` `# If size is odd, winner is A` ` ` `else` `:` ` ` `win ` `=` `0` ` ` `for` `i ` `in` `range` `(n ` `-` `2` `, ` `-` `1` `, ` `-` `1` `):` ` ` `# Stone will be removed by B` ` ` `if` `(i ` `%` `2` `=` `=` `1` `):` ` ` `# If B wants to win` ` ` `# B will take n-1 stones` ` ` `# from current pile having` ` ` `# n stones and force A to` ` ` `# pick 1 stone` ` ` `if` `(win ` `=` `=` `0` `and` `a[i] > ` `1` `):` ` ` `win ` `=` `1` ` ` `# Stone will be removed by A` ` ` `else` `:` ` ` `# If A wants to win` ` ` `# A will take n-1 stones from` ` ` `# current pile having n stones` ` ` `# and force B to pick 1 stone` ` ` `if` `(win ` `=` `=` `1` `and` `a[i] > ` `1` `):` ` ` `win ` `=` `0` ` ` ` ` `# Print the winner accordingly` ` ` `if` `(win ` `=` `=` `0` `):` ` ` `print` `(` `"A"` `)` ` ` `else` `:` ` ` `print` `(` `"B"` `)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given piles of stone` ` ` `arr ` `=` `[ ` `1` `, ` `1` `, ` `1` `, ` `2` `]` ` ` `N ` `=` `len` `(arr)` ` ` `# Function call` ` ` `findWinner(arr, N)` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program for the ` `// above approach` `using` `System;` `class` `GFG{` `// Function to find the winner` `// of game between A and B` `static` `void` `findWinner(` `int` `[]a, ` ` ` `int` `n)` `{ ` ` ` `// win = 1 means B is winner` ` ` `// win = 0 means A is winner` ` ` `int` `win = 0;` ` ` `// If size is even, winner is B` ` ` `if` `(n % 2 == 0)` ` ` `win = 1;` ` ` `// If size is odd, winner is A` ` ` `else` ` ` `win = 0;` ` ` `for` `(` `int` `i = n - 2; i >= 0; i--)` ` ` `{` ` ` `// Stone will be removed by B` ` ` `if` `(i % 2 == 1) ` ` ` `{` ` ` `// If B wants to win` ` ` `// B will take n-1 stones` ` ` `// from current pile having` ` ` `// n stones and force A to` ` ` `// pick 1 stone` ` ` `if` `(win == 0 && a[i] > 1)` ` ` `win = 1;` ` ` `}` ` ` `// Stone will be removed by A` ` ` `else` ` ` `{` ` ` `// If A wants to win` ` ` `// A will take n-1 stones from` ` ` `// current pile having n stones` ` ` `// and force B to pick 1 stone` ` ` `if` `(win == 1 && a[i] > 1)` ` ` `win = 0;` ` ` `}` ` ` `}` ` ` `// Print the winner accordingly` ` ` `if` `(win == 0)` ` ` `Console.Write(` `"A"` `);` ` ` `else` ` ` `Console.Write(` `"B"` `);` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `// Given piles of stone` ` ` `int` `[]arr = {1, 1, 1, 2};` ` ` `int` `N = arr.Length;` ` ` `// Function call` ` ` `findWinner(arr, N);` `}` `}` ` ` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output:**

B

**Time Complexity:*** O(N)***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 the winner of the game with N piles of boxes
- Find the winner of the Game of removing odd or replacing even array elements
- Game of N stones where each player can remove 1, 3 or 4
- Find the winner of the Game to Win by erasing any two consecutive similar alphabets
- Remove Minimum coins such that absolute difference between any two piles is less than K
- 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)
- Find the winner in nim-game
- Find the winner of the Game
- 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
- Winner in the Rock-Paper-Scissor game using Bit manipulation
- Determine winner of the Game by arranging balls in a row
- Determine the winner of a game of deleting Characters from a String
- Find the player who wins the game by removing the last of given N cards
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)

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.