Whats wrong in Beautiful Garland

https://www.codechef.com/viewsolution/21673749

i have applied the concept that if length of garland is 1 then garland is beautiful else i hae counted number of R and G and if they are equal and their sum is even and then i have counted the mismatched pair and printed the result accordingly.

Can anyone please help me to figure out whats wrong…??

When searching for pairs, you didn’t check for a pair at the start-to-end join. The garland is circular.

You could fix it with:

       prev = s[len-1];
       for(int i=0;i < len;i++){
            if(s[i]==prev){
                ct++;
                if(s[i]=='R') rr=1;
                else gg=1;
            }
            prev = s[i];
        }

(or something like that to include first==last… not checked)

Aside: The second part of your test

(r==g && (r+g)%2==0)

is redundant; if r==g then of course r+g is even.

1 Like

its still showing WA

i have read the editorial and have done exactly same but still getting WA

@joffan is absolutely right. @rk_221b you didn’t check for the start and end joint. Even if you did it later, it isn’t correct. Here is the small modification to your code which gives AC.

Thanks @joffan and @yadnesh_viper for pointing out my mistake.

//