Given an arr[] of binary digits. Consider two players X and Y are playing a game where in one move a player can remove any number of zeros from either the left or right of an element 1. A player loses the game if he/she can't make any move. The task is to find the winner of the game if both players play optimally and Y starts first.
Examples:
Input: arr[] = {1}
Output: X
Explanation: There are no zeros initially in the arr[], Therefore, player Y can't make any move. Hence, Player X wins the game.Input: arr[] = {0, 1, 0, 0, 1, 1, 0 }
Output: Y
Explanation:
Y choose 1 at index 5 along which the game will be played as marked as bold in all below operations, Then:
Player Y removes 2 zeros from the left side of element 1. Then arr[] = {0, 1, 1, 1, 0 }
player X removes 1 zero from the left side of element 1. Then arr[] = {1, 1, 1, 0 }
Player Y removes 1 zero from the right side of element 1. Then arr[] = {1, 1, 1}
Now, It's impossible to make any move for player X, because no zeros are present there. Hence player Y wins.
Approach: Implement the idea below to solve the problem:
There's always a winning state present for Y, if zeros at right or left of those 1 are not equal. So this problem can be solved by Counting a number of zeros present left and right of each element 1 present in arr[], Y will choose that 1 optimally at which a number of zeros right or left to that 1 are not equal. If the number of zeros on the left and right side are equal, Then X wins else Y wins.
Follow the below steps to solve the problem:
- Initialize two variables counter1 = 0 and counter2= 0 to store the count of zeroes initially in arr[] and for counting zeroes at left and right of 1s in the array.
- Store the number of zeros in counter1 variable by traversing arr[] from the start.
- Iterate through the whole array again from the right:
- If the current element is zero, Then increment counter2.
- If the current element is one, Then check if counter2 is equal to (counter1-counter2).
- If yes return true. Otherwise, return false.
Below is the implementation of the above approach.
// C++ implementation
#include <bits/stdc++.h>
using namespace std;
// Function for finding winner
string findWinner(int arr[], int n)
{
// int n= arr.size();
// Variable to store number of
// zeros present initially in arr[]
int counter1 = 0;
// Variable to store number of
// zeros while traversing
// arr[] again
int counter2 = 0;
// Loop for traversing on input arr[]
for (int i = 0; i < n; i++) {
// Incrementing counter1 if
// 0 is found
counter1 = arr[i] == 0 ? counter1 + 1 : counter1;
}
// Loop for traversing again on arr[]
for (int i = 0; i < n; i++) {
// If zero is found then increment
// counter2
counter2 = arr[i] == 0 ? counter2 + 1 : counter2;
// If one is found then check
// zero at left or right side
// are equal?
if (arr[i] == 1) {
// Checking that zero at left
// or right side of current
// element 1 are equal?
if (counter2 == (counter1 - counter2)) {
// If equal then
// returning X
return "X";
}
}
}
// If zeros are not equal then
// returning Y
return "Y";
}
int main()
{
int n = 8;
int arr[] = { 0, 1, 0, 0, 1, 1, 0, 0 };
// Function call
cout << (findWinner(arr, n));
return 0;
}
// this code is contributed by ksam24000
// Java code to implement the approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args)
{
int[] arr = { 0, 1, 0, 0, 1, 1, 0, 0 };
// Function call
System.out.println(findWinner(arr));
}
// Function for finding winner
static String findWinner(int[] arr)
{
// Variable to store number of
// zeros present initially in arr[]
int counter1 = 0;
// Variable to store number of
// zeros while traversing
// arr[] again
int counter2 = 0;
// Loop for traversing on input arr[]
for (int i = 0; i < arr.length; i++) {
// Incrementing counter1 if
// 0 is found
counter1
= arr[i] == 0 ? counter1 + 1 : counter1;
}
// Loop for traversing again on arr[]
for (int i = 0; i < arr.length; i++) {
// If zero is found then increment
// counter2
counter2
= arr[i] == 0 ? counter2 + 1 : counter2;
// If one is found then check
// zero at left or right side
// are equal?
if (arr[i] == 1) {
// Checking that zero at left
// or right side of current
// element 1 are equal?
if (counter2 == (counter1 - counter2)) {
// If equal then
// returning X
return "X";
}
}
}
// If zeros are not equal then
// returning Y
return "Y";
}
}
# Python code to implement the approach
# Function for finding winner
def findWinner(arr,n):
# n=len(arr)
# Variable to store number of
# zeros present initially in arr[]
counter1=0
# Variable to store number of
# zeros while traversing
# arr[] again
counter2=0
# Loop for traversing on input arr[]
for i in range(n):
# Incrementing counter1 if
# 0 is found
counter1 = (counter1+1) if arr[i]==0 else counter1
# Loop for traversing again on arr[]
for i in range(n):
# If zero is found then increment
# counter2
counter2 = (counter2+1) if arr[i]==0 else counter2
# If one is found then check
# zero at left or right side
# are equal?
if(arr[i]==1):
# Checking that zero at left
# or right side of current
# element 1 are equal?
if(counter2==(counter1-counter2)):
# If equal then
# returning X
return "X"
# If zeros are not equal then
# returning Y
return "Y"
# Driver code
n=8
arr=[0, 1, 0, 0, 1, 1, 0, 0]
# Function call
print(findWinner(arr,n))
# This code is contributed by Pushpesh Raj.
// C# code to implement the approach
using System;
class GFG {
// Driver code
public static void Main()
{
int[] arr = { 0, 1, 0, 0, 1, 1, 0, 0 };
// Function call
Console.WriteLine(findWinner(arr));
}
// Function for finding winner
static string findWinner(int[] arr)
{
// Variable to store number of
// zeros present initially in arr[]
int counter1 = 0;
// Variable to store number of
// zeros while traversing
// arr[] again
int counter2 = 0;
// Loop for traversing on input arr[]
for (int i = 0; i < arr.Length; i++) {
// Incrementing counter1 if
// 0 is found
counter1
= arr[i] == 0 ? counter1 + 1 : counter1;
}
// Loop for traversing again on arr[]
for (int i = 0; i < arr.Length; i++) {
// If zero is found then increment
// counter2
counter2
= arr[i] == 0 ? counter2 + 1 : counter2;
// If one is found then check
// zero at left or right side
// are equal?
if (arr[i] == 1) {
// Checking that zero at left
// or right side of current
// element 1 are equal?
if (counter2 == (counter1 - counter2)) {
// If equal then
// returning X
return "X";
}
}
}
// If zeros are not equal then
// returning Y
return "Y";
}
}
// This code is contributed by Samim Hossain Mondal.
// JS implementation
// Function for finding winner
function findWinner(arr,n)
{
// int n= arr.size();
// Variable to store number of
// zeros present initially in arr[]
let counter1 = 0;
// Variable to store number of
// zeros while traversing
// arr[] again
let counter2 = 0;
// Loop for traversing on input arr[]
for (let i = 0; i < n; i++) {
// Incrementing counter1 if
// 0 is found
counter1 = arr[i] == 0 ? counter1 + 1 : counter1;
}
// Loop for traversing again on arr[]
for (let i = 0; i < n; i++) {
// If zero is found then increment
// counter2
counter2 = arr[i] == 0 ? counter2 + 1 : counter2;
// If one is found then check
// zero at left or right side
// are equal?
if (arr[i] == 1) {
// Checking that zero at left
// or right side of current
// element 1 are equal?
if (counter2 == (counter1 - counter2)) {
// If equal then
// returning X
return "X";
}
}
}
// If zeros are not equal then
// returning Y
return "Y";
}
let n = 8;
let arr = [ 0, 1, 0, 0, 1, 1, 0, 0 ];
// Function call
console.log(findWinner(arr, n));
// this code is contributed by ksam24000
Output
Y
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles: