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

×

Showing Wrong answer despite of correct output

Here is my code for the question: #include<stdio.h>

int main(){

     int arr[100];
     int narr[50];
     narr[0]=0;
     int t,n,i,j=0,k;
     scanf("%d",&t);
    for(;t>0;t--) {
            int flag=0;
            scanf("%d",&n);
      for(i=0;i<n;i++){
           scanf("%d",&arr[i]);
      }
    for(i=1;i<=7;i++){
        while(arr[j]==i){
            j++;
        }
        narr[i]=j;
        for(k=i-1;k>0;k--){
            narr[i]-=narr[k];
        }

    }
    for(i=6;i>0;i--){
        while(arr[j]==i){
            j++;
        }
        narr[14-i]=j;
        for(k=13-i;k>0;k--){
            narr[14-i]-=narr[k];
        }

    }

    for(i=1;i<14;i++){
        if(narr[i]!=0&&(narr[i]==narr[14-i])&& arr[i-1]<8&&arr[i-1]>0){

        }
        else
        {
          flag = 1;
          break;
        }
    }
       if(flag==0)
        printf("yes\n");
       else
        printf("no\n");

    }


}

I have tried several inputs and i got the right output.Can anyone point out where this code is wrong?

asked 19 Aug '17, 19:42

namanjn's gravatar image

4★namanjn
53
accept rate: 0%


You are 1 step away from AC now.

Few things you must notice-

  1. After every iteration, you are not resetting the values. You must reset the arrays arr, narr and variable j. Whats happening is, after giving correct output for iteration 1, j is still having value of 14 or something depending on size of array of previous TC, and this is causing you to get immediate WA/

  2. while(arr[j]==i){

What if the rainbow array is of size 100? Your code will then try to check the '100th' (0 based indexing) index, which will either cause it to go into undefined behavior (WA) or runtime error. Either add a check for j<100 && arr[j]==1 or increase array size to 105 or something (whose default value will be 0, causing the loop to exit).

link

answered 20 Aug '17, 12:31

vijju123's gravatar image

5★vijju123 ♦♦
15.5k12066
accept rate: 18%

check for this: 14

1 2 3 4 5 6 7 7 6 5 4 3 2 1

your code outputs 'no' when it should be 'yes'!

ps- you can have multiple 7 in between.

I hope you got it!

link

answered 19 Aug '17, 20:05

slugger's gravatar image

1★slugger
5414
accept rate: 25%

edited 19 Aug '17, 20:06

This test case seems familiar :p

(19 Aug '17, 20:11) vijju123 ♦♦5★

sorry didn't catch your 'sarcasm'!

(19 Aug '17, 20:16) slugger1★

@slugger I have checked this case it is showing yes only.

(19 Aug '17, 20:50) namanjn4★

Can you tell me please what is wrong in my code?

(19 Aug '17, 21:23) namanjn4★

@vijju123, I hope we my rating doesn't fall due to 'the same first thought' plagiarism! :P

(20 Aug '17, 03:12) slugger1★
showing 5 of 6 show all

Here is your bug-

Input
1
15
1 2 3 4 5 6 7 7 6 5 4 3 2 1 8
Your output
yes
Correct Output
no

Here is the next TC-

Input
1
15
1 2 3 4 5 6 7 7 6 5 4 3 2 1 7
Your output
yes
Correct output
no

Only 1 group of 7 allowed/array must be palindromic.

link

answered 19 Aug '17, 21:49

vijju123's gravatar image

5★vijju123 ♦♦
15.5k12066
accept rate: 18%

edited 20 Aug '17, 11:10

@vijju123 I have now added the condition that array elements should be between 1 to 7(both included) and the output for above changes to yes still it showing wrong answer.

(20 Aug '17, 10:53) namanjn4★

Link to your new code. I can have a look :)

BTW, i think you meant "output for above changes to no" :p

(20 Aug '17, 11:04) vijju123 ♦♦5★
(20 Aug '17, 11:24) namanjn4★

Ok second case is also wrong. Can you tell me how to choose the input to find the bug.

(20 Aug '17, 11:26) namanjn4★

Well, for me, its a skill i developed by intense debugging of other people's code XD.

Just look at your code and think where it can fail. Your code is leaving some spaces for errors to creep in.

(20 Aug '17, 11:27) vijju123 ♦♦5★

I have changed my code it is showing correct output for the above two cases still the answer is wrong. Here is the link to code: https://www.codechef.com/viewsolution/15034290

(20 Aug '17, 11:43) namanjn4★
showing 5 of 6 show all

What happens when i optimize the code it gives wrong answer but in other case Tle, more test cases are correct than in wrong answer?

link

answered 13 Nov '17, 01:01

gautamcse27's gravatar image

2★gautamcse27
375
accept rate: 0%

toggle preview
Preview

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:

×2,474
×1,917
×862
×856
×139
×41
×22

question asked: 19 Aug '17, 19:42

question was seen: 947 times

last updated: 13 Nov '17, 01:01