#include<stdio.h>

#include<string.h>

int main()

{int te;

scanf("%d",&te);

while(te–){

long int n,k,i,t,h=0,cnt=0;

scanf("%ld",&n);

scanf("%ld",&k);

char str[n];

for(i=0;i<n;i++)

{

scanf(" %c", &str[i]);

```
}
for(i=n-k;i<n;i++)
{
if(str[i]=='H')
{
cnt++;
}
}
for(i=0;i<n-k;i++)
{
if(str[i]=='H')
{
h++;
}
}
t= n-k-h;
if(cnt%2==0)
{
printf("%d\n", h);
}
else
{
printf("%d\n",t);
}}
return 0;
```

}

Consider the scenario [ … H H ] ,Here the first H is (n-k)th position. According to your logic, since 2 H’s are encountered from n-k th position the previous sequence wouldn’t be altered and you will print h heads, whereas the rightmost H would reverse the H prior to it and make it T while also reversing the entire sequence. Since now the n-k th position holds T the answer would be the no of heads in reversed sequence which is t Heads.

So you only need to check the character at n-k th position and if it is H print h else print t;

Modified your code : CodeChef: Practical coding for everyone

thank you very much i never thought about it !