×

# Showing Wrong answer despite of correct output

 0 Here is my code for the question: #include 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;i0;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 4★namanjn 5●3 accept rate: 0%

 1 You are 1 step away from AC now. Few things you must notice- 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/ 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). answered 20 Aug '17, 12:31 15.5k●1●20●66 accept rate: 18%
 0 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! answered 19 Aug '17, 20:05 1★slugger 54●1●4 accept rate: 25% This test case seems familiar :p (19 Aug '17, 20:11) 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★ @slugger, not sarcasm, just some light hearted humor. https://discuss.codechef.com/questions/108341/why-my-rainbowa-solution-giving-wrong-answer (19 Aug '17, 21:09) 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
 0 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. answered 19 Aug '17, 21:49 15.5k●1●20●66 accept rate: 18% @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) Here is the code: https://www.codechef.com/viewsolution/15033974 (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) 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
 0 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? answered 13 Nov '17, 01:01 37●5 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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