Header Ads Widget

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,,, 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,,.

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

  • 140,000
  • 23105
  • 01 for each valid 
  • 1==1
  • the sum of  over all test cases does not exceed 106

Subtasks

Subtask #1 (50 points): =0 for each  (21)

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() {
  // your code goes here
  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
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t=Integer.parseInt(br.readLine());
        long c,m,p;
        while(t-->0)
        {
         br.readLine();
         String s[]=br.readLine().split(" ");
         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:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-



 

Post a Comment

0 Comments