You are not logged in. Please login at www.codechef.com to post your questions!

×

[closed] Why isn't the second implementation working?

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

asked 27 May '17, 13:23

akashbhalotia's gravatar image

4★akashbhalotia
68112
accept rate: 14%

closed 27 May '17, 14:10

vijju123's gravatar image

5★vijju123 ♦♦
15.4k12066

The question has been closed for the following reason "The question is answered, right answer was accepted" by vijju123 27 May '17, 14:10


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

link

answered 27 May '17, 13:51

pankaj_chopra's gravatar image

3★pankaj_chopra
32929
accept rate: 21%

edited 27 May '17, 13:57

2

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

(27 May '17, 14:05) vijju123 ♦♦5★
2

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.

(27 May '17, 14:07) akashbhalotia4★
2

Thanks a lot

(27 May '17, 14:08) akashbhalotia4★
1

HAHAHAHA XDDDDDDDDDD

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

(27 May '17, 14:08) vijju123 ♦♦5★
2

You should @vijju123

(27 May '17, 14:12) akashbhalotia4★

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.

link

answered 27 May '17, 14:08

prakhariitd's gravatar image

6★prakhariitd
1.1k211
accept rate: 10%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,302

question asked: 27 May '17, 13:23

question was seen: 272 times

last updated: 27 May '17, 14:12