BANSTR ( ENIGMA - PLINTH'20 LNMIIT )

Question-Banana String (BANSTR)
What’s wrong with this code? Please help!

#include <stdio.h>

int main(void) {
int t;
scanf("%d",&t);
while(t–){
int n,pt,i,j,k,flag =0;
scanf("%d%d",&n,&pt);
char s[n];
scanf("%s",s);
int aa=0,bb=0,arr[n];
for(i=0;i<n;i++){
arr[i]=0;
if(s[i]==‘a’)
{aa++;arr[i]=1;}
else
{bb++;arr[i]=2;}
}
int bab=0;
for(i=aa;i<n;i++){
if(s[i]==‘a’){
bab++;
arr[i]=1;}
}
if(bab<=pt){
pt=pt-bab;
aa+=pt/2;
for(i=0;i<n;i++){
if(i<aa)
s[i]=‘a’;
else
s[i]=‘b’;
} }
else{
bab=pt;
for(i=0;i<aa;i++){
if(pt>0){
if(s[i]==‘b’){
s[i]=‘a’;
pt–;
}
}
else break;
}
for(j=n-1;j>=aa;j–){
if(bab>0){
if(s[j]==‘a’){
s[j]=‘b’;
bab–;
}
}
else break;
}
}
printf("%s\n",s);
}
return 0;
}

Also share the logic along with the code.Maybe there’s something wrong.

Can you elaborate your logic . So that it’ll be easy to understand and also i think that the implenentation is wrong because the variable pt has garbage value

I count number of ‘a’ and ‘b’ in string. aa and bb repectively.
then I count number of ‘a’ after the aa elements of string as bab
if(number of ‘a’ after aa elements <=pt (points given))
then first i make a string in which first aa elements are ‘a’ and next bb elments are ‘b’
and pt =pt-bab
then convert pt/2 ‘b’ int ‘a’ from left side of string
else(means pt < bab)
then I convert pt ‘b’ into ‘a’ from left side
and pt ‘a’ into ‘b’ from right side

for more you can check my submission from this link https://www.codechef.com/viewsolution/28867963