Find the winner if player removes any number of 0 from one side of 1
Last Updated :
09 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
string findWinner( int arr[], int n)
{
int counter1 = 0;
int counter2 = 0;
for ( int i = 0; i < n; i++) {
counter1 = arr[i] == 0 ? counter1 + 1 : counter1;
}
for ( int i = 0; i < n; i++) {
counter2 = arr[i] == 0 ? counter2 + 1 : counter2;
if (arr[i] == 1) {
if (counter2 == (counter1 - counter2)) {
return "X" ;
}
}
}
return "Y" ;
}
int main()
{
int n = 8;
int arr[] = { 0, 1, 0, 0, 1, 1, 0, 0 };
cout << (findWinner(arr, n));
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int [] arr = { 0 , 1 , 0 , 0 , 1 , 1 , 0 , 0 };
System.out.println(findWinner(arr));
}
static String findWinner( int [] arr)
{
int counter1 = 0 ;
int counter2 = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
counter1
= arr[i] == 0 ? counter1 + 1 : counter1;
}
for ( int i = 0 ; i < arr.length; i++) {
counter2
= arr[i] == 0 ? counter2 + 1 : counter2;
if (arr[i] == 1 ) {
if (counter2 == (counter1 - counter2)) {
return "X" ;
}
}
}
return "Y" ;
}
}
|
Python3
def findWinner(arr,n):
counter1 = 0
counter2 = 0
for i in range (n):
counter1 = (counter1 + 1 ) if arr[i] = = 0 else counter1
for i in range (n):
counter2 = (counter2 + 1 ) if arr[i] = = 0 else counter2
if (arr[i] = = 1 ):
if (counter2 = = (counter1 - counter2)):
return "X"
return "Y"
n = 8
arr = [ 0 , 1 , 0 , 0 , 1 , 1 , 0 , 0 ]
print (findWinner(arr,n))
|
C#
using System;
class GFG {
public static void Main()
{
int [] arr = { 0, 1, 0, 0, 1, 1, 0, 0 };
Console.WriteLine(findWinner(arr));
}
static string findWinner( int [] arr)
{
int counter1 = 0;
int counter2 = 0;
for ( int i = 0; i < arr.Length; i++) {
counter1
= arr[i] == 0 ? counter1 + 1 : counter1;
}
for ( int i = 0; i < arr.Length; i++) {
counter2
= arr[i] == 0 ? counter2 + 1 : counter2;
if (arr[i] == 1) {
if (counter2 == (counter1 - counter2)) {
return "X" ;
}
}
}
return "Y" ;
}
}
|
Javascript
function findWinner(arr,n)
{
let counter1 = 0;
let counter2 = 0;
for (let i = 0; i < n; i++) {
counter1 = arr[i] == 0 ? counter1 + 1 : counter1;
}
for (let i = 0; i < n; i++) {
counter2 = arr[i] == 0 ? counter2 + 1 : counter2;
if (arr[i] == 1) {
if (counter2 == (counter1 - counter2)) {
return "X" ;
}
}
}
return "Y" ;
}
let n = 8;
let arr = [ 0, 1, 0, 0, 1, 1, 0, 0 ];
console.log(findWinner(arr, n));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...