Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
Last Updated :
17 Jun, 2021
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: B
Explanation:
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: A
Explanation:
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++
#include <bits/stdc++.h>
using namespace std;
void findWinner( int a[], int n)
{
int win = 0;
if (n % 2 == 0)
win = 1;
else
win = 0;
for ( int i = n - 2; i >= 0; i--) {
if (i % 2 == 1) {
if (win == 0 && a[i] > 1)
win = 1;
}
else {
if (win == 1 && a[i] > 1)
win = 0;
}
}
if (win == 0)
cout << "A" ;
else
cout << "B" ;
}
int main()
{
int arr[] = { 1, 1, 1, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
findWinner(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findWinner( int a[], int n)
{
int win = 0 ;
if (n % 2 == 0 )
win = 1 ;
else
win = 0 ;
for ( int i = n - 2 ; i >= 0 ; i--)
{
if (i % 2 == 1 )
{
if (win == 0 && a[i] > 1 )
win = 1 ;
}
else
{
if (win == 1 && a[i] > 1 )
win = 0 ;
}
}
if (win == 0 )
System.out.print( "A" );
else
System.out.print( "B" );
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , 1 , 2 };
int N = arr.length;
findWinner(arr, N);
}
}
|
Python3
def findWinner(a, n):
win = 0
if (n % 2 = = 0 ):
win = 1
else :
win = 0
for i in range (n - 2 , - 1 , - 1 ):
if (i % 2 = = 1 ):
if (win = = 0 and a[i] > 1 ):
win = 1
else :
if (win = = 1 and a[i] > 1 ):
win = 0
if (win = = 0 ):
print ( "A" )
else :
print ( "B" )
if __name__ = = '__main__' :
arr = [ 1 , 1 , 1 , 2 ]
N = len (arr)
findWinner(arr, N)
|
C#
using System;
class GFG{
static void findWinner( int []a,
int n)
{
int win = 0;
if (n % 2 == 0)
win = 1;
else
win = 0;
for ( int i = n - 2; i >= 0; i--)
{
if (i % 2 == 1)
{
if (win == 0 && a[i] > 1)
win = 1;
}
else
{
if (win == 1 && a[i] > 1)
win = 0;
}
}
if (win == 0)
Console.Write( "A" );
else
Console.Write( "B" );
}
public static void Main(String[] args)
{
int []arr = {1, 1, 1, 2};
int N = arr.Length;
findWinner(arr, N);
}
}
|
Javascript
<script>
function findWinner(a, n)
{
let win = 0;
if (n % 2 == 0)
win = 1;
else
win = 0;
for (let i = n - 2; i >= 0; i--)
{
if (i % 2 == 1)
{
if (win == 0 && a[i] > 1)
win = 1;
}
else
{
if (win == 1 && a[i] > 1)
win = 0;
}
}
if (win == 0)
document.write( "A" );
else
document.write( "B" );
}
let arr=[1, 1, 1, 2];
let N = arr.length;
findWinner(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...