WA in POPGATES

THE PROBLEM IS-POPGATES Problem - CodeChef
AND MY CODE IS-
#include
using namespace std;
int main(){
int t;
cin>>t;
for(int i=0;i<t;i++){
int n,k;
int n_head=0,prev_head=0,h=0,t=0;
cin>>n>>k;
char coin[n];
for(int j=1;j<=n;j++)
cin>>coin[j];
for(int j=n;j>=n-k+1;j–){
if(coin[j]==‘H’)
{++prev_head; ++n_head;}
else if(coin[j]==‘T’ && prev_head%2==1)
{++prev_head; ++n_head;}
}
for(int j=1;j<=n-k;j++){
if(coin[j]==‘T’) t=t+1;
else h=h+1;
}
if(n_head%2==0) cout<<h;
else cout<<t;
}
}

please format your code or provide a link for your solution.

and mistake is not in your logic, the way you are printing the answer is not correct.

at last write
if (n_head%2==0) cout<<h<<endl;
else cout<<t<<endl;