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

×

Can anyone find the mistake in this code?

So i have tried the Chef and rainbow array question. I do get correct answers for the given test cases on codeblocks, but when submitting codechef says wrong answer. Is something wrong with my logic? Thanks
Link to question: https://www.codechef.com/problems/RAINBOWA

My code:

 #include<stdio.h>

 int main(){

 int i,j,t,n,count7,rem,status,midstat;  
 int count1,count2,count3,count4,count5,count6;  
 int a[100];

 scanf("%d",&t);

 for(i=0;i<t;i++){
    scanf("%d",&n);
    count7=0;
    midstat=1;
    count6=0;
    count5=0;
    count4=0;
    count3=0;
    count2=0;
    count1=0;
    for(j=0;j<n;j++){

        scanf("%d",&a[j]);
        if(a[j]==7){
            count7++;
        }
    }
    rem=n-count7;
    if(rem%2!=0||count7==0){
            status=0;
    }

    else{
       for(j=0;j<(rem/2);j++){
        if(a[j]==a[n-1-j]){
            midstat*=1;
        }
        else{
            midstat*=0;
        }

       }

       if(midstat==1){
            for(j=0;j<rem/2;j++){
                switch(a[j]){
                    case 1:count1++;
                    break;
                    case 2:count2++;
                    break;
                    case 3:count3++;
                    break;
                    case 4:count4++;
                    break;
                    case 5:count5++;
                    break;
                    case 6:count6++;
                    break;
                }
            }
            if(count1>0&&count2>0&&count3>0&&count4>0&&count5>0&&count6>0){
                status=1;
            }
            else{
                status=0;
            }
       }


    }
  if(status==1){
    printf("yes\n");
  }
  else{
    printf("no\n");
  }

}


}

asked 19 Aug '17, 22:48

tmsreekanth98's gravatar image

3★tmsreekanth98
11
accept rate: 0%

edited 19 Aug '17, 22:49

vijju123's gravatar image

5★vijju123 ♦
13.6k11036


Two things-

1.Where are you checking if array has elements only in range [1,7]??
2. Another condition is, it must be palindromic. Or, as given in problem statement, if there are a2 number of 2 before 7, then there must be a2 number of 2 after it. Eg- {1,2,2,3,4,5,6,7,6,5,4,3,2,1] is nto rainbow as there are two 2's before 7 but only 1 after it in sequence.
link

answered 19 Aug '17, 22:53

vijju123's gravatar image

5★vijju123 ♦
13.6k11036
accept rate: 19%

He actually checks if the array is palindromic or not. But first one is indeed a problem.

(19 Aug '17, 22:55) afaxnraner6★

Sorry, didnt notice it. The code is too long so kinda skimmed through it :p EDIT: Yes, found it. There is a for loop dedicated to that. Sorry, missed it. :)

(19 Aug '17, 22:56) vijju123 ♦5★
1

thanks, i found my mistake

(19 Aug '17, 23:24) tmsreekanth983★

Here is one test case. Your program prints wrongly yes.

1
13
2 1 3 4 5 6 7 6 5 4 3 1 2
link

answered 19 Aug '17, 22:54

afaxnraner's gravatar image

6★afaxnraner
5665
accept rate: 25%

thanks for that. now i figured whats the problem. thanks

(19 Aug '17, 23:24) tmsreekanth983★

so i messed with the code, made it such that it checks that first a1 chars is 1 and so on using a function. Still some problem with the code. Sorry i am beginner so i am unable to find out the mistake and also my code is too long as well, it would have been great if any professional helps. Thanks

include<stdio.h>

int statuscheck(int a,int b,int arr[],int ch){
int c;
for(c=a;c<b;c++){
if(arr[c]==ch){
return 1;
}
else{
return 0;
}
}

}

int main(){

int i,j,t,n,count7,rem,status,midstat; int count1,count2,count3,count4,count5,count6; int a[100];

scanf("%d",&t);

for(i=0;i<t;i++){

scanf("%d",&n);
count7=0;
midstat=1;
count6=0;
count5=0;
count4=0;
count3=0;
count2=0;
count1=0;
for(j=0;j<n;j++){

    scanf("%d",&a[j]);
    if(a[j]==7){
        count7++;
    }
}
rem=n-count7;
if(rem%2!=0||count7==0){
        status=0;
}

else{
   for(j=0;j<(rem/2);j++){
    if(a[j]==a[n-1-j]){
        midstat*=1;
    }
    else{
        midstat*=0;
    }

   }

   if(midstat==1){
        for(j=0;j<rem/2;j++){
            switch(a[j]){
                case 1:count1++;  
                break;
                case 2:count2++;  
                break;
                case 3:count3++;
                break;
                case 4:count4++;  
                break;
                case 5:count5++;  
                break;
                case 6:count6++;  
                break;
                default:status=0;  
                break;
            }
        }
       /* if(count1>0&&count2>0&&count3>0&&count4>0&&count5>0&&count6>0){
            status=1;
        }
        else{
            status=0;
        }*/
        if(statuscheck(0,count1,a,1)==1&&statuscheck(count1,count2+count1,a,2)==1&&statuscheck(count2+count1,count3+count1+count2,a,3)==1&&statuscheck(count3+count1+count2,count4+count3+count2+count1,a,4)==1&&statuscheck(count4+count3+count2+count1,count5+count4+count3+count2+count1,a,5)==1&&statuscheck(count5+count4+count3+count2+count1,count6+count5+count4+count3+count2+count1,a,6)==1){
            status=1;
        }
        else{
            status=0;
        }
   }


}

if(status==1){
printf("yes\n");
}
else{
printf("no\n");
}

}

}

link

answered 19 Aug '17, 23:55

tmsreekanth98's gravatar image

3★tmsreekanth98
11
accept rate: 0%

edited 19 Aug '17, 23:56

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,359
×552
×220
×148
×25
×5

question asked: 19 Aug '17, 22:48

question was seen: 211 times

last updated: 19 Aug '17, 23:56