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?

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!

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.

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

1 Like

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?

This test case seems familiar :stuck_out_tongue:

sorry didn’t catch your ‘sarcasm’!

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

@slugger, not sarcasm, just some light hearted humor.

https://discuss.codechef.com/questions/108341/why-my-rainbowa-solution-giving-wrong-answer

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

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

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

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

BTW, i think you meant “output for above changes to no” :stuck_out_tongue:

Here is the code:
https://www.codechef.com/viewsolution/15033974

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

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.

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