# Game on a Strip | codechef solution

Tzuyu gave Nayeon a strip of $�$ cells (numbered $1$ through $�$) for her birthday. This strip is described by a sequence ${�}_{1},{�}_{2},\dots ,{�}_{�}$, where for each valid $�$, the $�$-th cell is blocked if ${�}_{�}=1$ or free if ${�}_{�}=0$. Tzuyu and Nayeon are going to use it to play a game with the following rules:

• The players alternate turns; Nayeon plays first.
• Initially, both players are outside of the strip. However, note that afterwards during the game, their positions are always different.
• In each turn, the current player should choose a free cell and move there. Afterwards, this cell becomes blocked and the players cannot move to it again.
• If it is the current player's first turn, she may move to any free cell.
• Otherwise, she may only move to one of the left and right adjacent cells, i.e. from a cell $�$, the current player may only move to the cell $�-1$ or $�+1$ (if it is free).
• If a player is unable to move to a free cell during her turn, this player loses the game.

Nayeon and Tzuyu are very smart, so they both play optimally. Since it is Nayeon's birthday, she wants to know if she can beat Tzuyu. Find out who wins.

### Input

• The first line of the input contains a single integer $�$ denoting the number of test cases. The description of $�$ test cases follows.
• The first line of each test case contains a single integer $�$.
• The second line contains $�$ space-separated integers ${�}_{1},{�}_{2},\dots ,{�}_{�}$.

### Output

For each test case, print a single line containing the string "Yes" if Nayeon wins the game or "No" if Tzuyu wins (without quotes).

### Constraints

• $1\le �\le 40,000$
• $2\le �\le 3\cdot 1{0}^{5}$
• $0\le {�}_{�}\le 1$ for each valid $�$
• ${�}_{1}={�}_{�}=1$
• the sum of $�$ over all test cases does not exceed $1{0}^{6}$

Subtask #1 (50 points): ${�}_{�}=0$ for each $�$ ($2\le �\le �-1$)

Subtask #2 (50 points): original constraints

### Sample 1:

Input
Output
4
7
1 1 0 0 0 1 1
8
1 0 1 1 1 0 0 1
4
1 1 0 1
4
1 1 1 1
Yes
No
Yes
No

### Explanation:

Example case 1: Since both Nayeon and Tzuyu play optimally, Nayeon can start e.g. by moving to cell $4$, which then becomes blocked. Tzuyu has to pick either the cell $3$ or the cell $5$, which also becomes blocked. Nayeon is then left with only one empty cell next to cell $4$ (the one Tzuyu did not pick); after she moves there, Tzuyu is unable to move, so she loses the game.

Example case 2: Regardless of what cell Nayeon moves to at the start, Tzuyu will always be able to beat her.

Code(C++):-

#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int count=0;
int a[n];
int firstmax=0,secmax=0;
for(int i =0;i<n;i++)
{
cin>>a[i];
if(a[i]==1)
{
if(count!=0)
{
if(count>firstmax)
{
secmax=firstmax;
firstmax =count;
}
else if(count>secmax)
secmax=count;
count=0;
}
}
else count=count+1;
}
if(firstmax%2!=0 && secmax<(firstmax+1)/2)
std::cout << "Yes" << std::endl;
else
std::cout << "No" << std::endl;
}
return 0;
}

Code(JAVA):-

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
long c,m,p;
while(t-->0)
{
c=0;m=0;p=0;
for(String su:s)
{
if(su.equals("0"))
c++;
else
{
if(c>m||c>p)
{
if(c>m)
{
p=m;
m=c;
}
else
p=c;
}
c=0;
}
}
if(p<m/2+1&&m%2==1)
System.out.println("Yes");
else
System.out.println("No");
}
}
}

### Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-