Find the winner of the game with N piles of boxes
Last Updated :
11 Feb, 2025
Given an array of strings arr[] of size n representing n piles containing white (W) and Black (B) boxes. Two players are playing a game with these piles, Player1 and Player2. Both take turns with Player1 starting first. In their respective turn, Player1 can remove any number of boxes from the top of the pile with the topmost box white (W) and Player2 can remove boxes from the pile with the topmost box black (B). The game ends when the player has no boxes to remove and the player who made the last move loses. The task is to find the winner of the game, given that both the players play optimally and alternatively.
Note: If there are no piles of boxes, Player2 wins the game.
Examples:
Input: arr[] = {"WBW", "BWB"}
Output: Player1
Explanation: Player1 can remove all boxes from pile 1. Now Player2 has to make a choice from pile 2. Whatever choice Player2 makes, he/she has to make the last move.
Input: arr[] = {"WWWW", "WBWB", "WBBW"}
Output: Player2
Approach:
If a pile has consecutive identical boxes, compress it to a simplified form like "BWB" or "WBW", as it’s optimal to remove all identical top boxes.
For piles where the top and bottom boxes differ, these do not affect the outcome. When one player makes a move on such a pile, the other player can mirror it, leaving the first player in the same position.
For piles where the top and bottom boxes are the same, they require an extra move. Count the extra moves each player must make. Player with less count of such moves wins the game.
C++
// C++ program to find the winner
// of game of N piles of boxes
#include <bits/stdc++.h>
using namespace std;
// Function to find the winner of the game
void findWinner(int n, vector<string> &arr) {
int a = 0, b = 0;
for (int i = 0; i < n; ++i) {
int l = arr[i].length();
// If pile begins and ends with White box
if (arr[i][0] == arr[i][l - 1]
&& arr[i][0] == 'W')
a++;
// If pile begins and ends with Black
if (arr[i][0] == arr[i][l - 1]
&& arr[i][0] == 'B')
b++;
}
if (a <= b)
cout<<"Player1";
else
cout<<"Player2";
}
int main() {
int n = 3;
vector<string> arr = { "WWWW", "WBWB", "WBBW" };
findWinner(n, arr);
return 0;
}
C
// C program to find the winner
// of game of N piles of boxes
#include <stdio.h>
#include <string.h>
// Function to find the winner of the game
void findWinner(int n, char arr[][100]) {
int a = 0, b = 0;
for (int i = 0; i < n; ++i) {
int l = strlen(arr[i]);
// If pile begins and ends with White box
if (arr[i][0] == arr[i][l - 1]
&& arr[i][0] == 'W')
a++;
// If pile begins and ends with Black
if (arr[i][0] == arr[i][l - 1]
&& arr[i][0] == 'B')
b++;
}
if (a <= b)
printf("Player1");
else
printf("Player2");
}
int main() {
int n = 3;
char arr[3][100] = { "WWWW", "WBWB", "WBBW" };
findWinner(n, arr);
return 0;
}
Java
// Java program to find the winner
// of game of N piles of boxes
class GfG {
// Function to find the winner of the game
static void findWinner(int n, String[] arr) {
int a = 0, b = 0;
for (int i = 0; i < n; ++i) {
int l = arr[i].length();
// If pile begins and ends with White box
if (arr[i].charAt(0) == arr[i].charAt(l - 1)
&& arr[i].charAt(0) == 'W')
a++;
// If pile begins and ends with Black
if (arr[i].charAt(0) == arr[i].charAt(l - 1)
&& arr[i].charAt(0) == 'B')
b++;
}
if (a <= b)
System.out.print("Player1");
else
System.out.print("Player2");
}
public static void main(String[] args) {
int n = 3;
String[] arr = {"WWWW", "WBWB", "WBBW"};
findWinner(n, arr);
}
}
Python
# Python program to find the winner
# of game of N piles of boxes
# Function to find the winner of the game
def findWinner(n, arr):
a = 0
b = 0
for i in range(n):
l = len(arr[i])
# If pile begins and ends with White box
if arr[i][0] == arr[i][l - 1] and arr[i][0] == 'W':
a += 1
# If pile begins and ends with Black
if arr[i][0] == arr[i][l - 1] and arr[i][0] == 'B':
b += 1
if a <= b:
print("Player1")
else:
print("Player2")
if __name__ == "__main__":
n = 3
arr = ["WWWW", "WBWB", "WBBW"]
findWinner(n, arr)
C#
// C# program to find the winner
using System;
class GfG {
// Function to find the winner of the game
static void FindWinner(int n, string[] arr) {
int a = 0, b = 0;
for (int i = 0; i < n; ++i) {
int l = arr[i].Length;
// If pile begins and ends with White box
if (arr[i][0] == arr[i][l - 1] && arr[i][0] == 'W')
a++;
// If pile begins and ends with Black
if (arr[i][0] == arr[i][l - 1] && arr[i][0] == 'B')
b++;
}
if (a <= b)
Console.Write("Player1");
else
Console.Write("Player2");
}
public static void Main(string[] args) {
int n = 3;
string[] arr = {"WWWW", "WBWB", "WBBW"};
FindWinner(n, arr);
}
}
JavaScript
// JavaScript program to find the winner
// of game of N piles of boxes
// Function to find the winner of the game
function findWinner(n, arr) {
let a = 0, b = 0;
for (let i = 0; i < n; ++i) {
let l = arr[i].length;
// If pile begins and ends with White box
if (arr[i][0] === arr[i][l - 1]
&& arr[i][0] === 'W')
a++;
// If pile begins and ends with Black
if (arr[i][0] === arr[i][l - 1]
&& arr[i][0] === 'B')
b++;
}
if (a <= b)
console.log("Player1");
else
console.log("Player2");
}
const n = 3;
const arr = ["WWWW", "WBWB", "WBBW"];
findWinner(n, arr);
Time Complexity: O(n)
Auxiliary Space : O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem