Why isn't the second implementation working?

java

#1

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");
      }
    }
}

#2

In the second implementation, you are using char data type for accessing colours which have been given in int data type. In this way, you are matching the first digit of the integer with the first digit of another integer, not the complete integer value.

For the below Input:

1

3

50 52 56

The output of your solution is:

Yes

Whereas the expected output should be:

No


#3

For this test case

4 2 15 15 16 also the output is yes.

You are using charAt(0) but Ci need not be a single digit number, you are checking only the first digit.


#4

Lol i messed up with the order. :confused: I misread that “first is not working” while second got AC. Better go get some sleep XD


#5

OMG! Such a stupid mistake!!! Sometimes, the obvious is just in front of you, and you still don’t get it for hours!! I feel soooo stupid.


#6

Thanks a lot


#7

HAHAHAHA XDDDDDDDDDD

Same here, i am also doing silly errors today. Better get some rest :slight_smile:


#8

You should @vijju123