Skip to content
Related Articles

Related Articles

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 : 11 Sep, 2020

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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;
}

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

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

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
Output: 
B


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

My Personal Notes arrow_drop_up
Recommended Articles
Page :