Here, I have two implementations of the problem Art, code- MAKEART in JAVA. The first one is giving me AC, while the seond one is giving wrong answer. Please help.
It is a problem from SNACKDOWN 2016, pre-elimination round A. Thanks.
link-https://www.codechef.com/problems/MAKEART
1st implementation-
import java.io.*;
class MAKEART
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader
(new InputStreamReader(System.in));
int i,T,N,j;
System.out.println();
T=Integer.parseInt(br.readLine());
String s[]=new String[100011];
int a[]=new int[100011];
for(i=0;i<T;i++)
{
N=Integer.parseInt(br.readLine());
s=br.readLine().trim().split(" ");
for(j=0;j<N;j++)
a[j]=Integer.parseInt(s[j]);
int flag=0;
for(j=2;j<N;j++)
{
if(a[j]==a[j-1]&&a[j-1]==a[j-2])
{
flag=1;
break;
}
}
if(flag==1)
System.out.println("Yes");
else
System.out.println("No");
}
}
}
Second implementation-
import java.io.*;
class MAKEART2 //incorrect
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader
(new InputStreamReader(System.in));
int i,T,N,j;
System.out.println();
T=Integer.parseInt(br.readLine());
String s[]=new String[100011];
for(i=0;i<T;i++)
{
N=Integer.parseInt(br.readLine());
s=br.readLine().trim().split(" ");
int flag=0,c=1;
char ch=s[0].charAt(0),ch2;
for(j=1;j<N;j++)
{
ch2=s[j].charAt(0);
if(ch2==ch)
c++;
else
{
ch=ch2;
c=1;
}
if(c==3)
{
flag=1;
break;
}
}
if(flag==1)
System.out.println("Yes");
else
System.out.println("No");
}
}
}