# https://www.codechef.com/LTIME81B/problems/POPGATES

#include <bits/stdc++.h>
using namespace std;

int main() {
long long int t;
cin>>t;
while(t–)
{
long long int n=0,k=0,l=0,ta=0,h=0,i=0;
cin>>n>>k;
char c[n];
for(i=1;i<=n;i++)
cin>>c[i];
int p=n-k;
for(i=1;i<=p;i++)
{
if(c[i]==‘H’)
h++;
else
ta++;
}
i=n;
while(k!=0)
{
if(c[i]==‘H’)
l++;
i–;
k–;
}
//cout<<l<<“hi”;
if(l%2==0)
{
cout<<h<<endl;
}
else
cout<<ta<<endl;
}
return 0;
}

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

//isme wrong answer aa rha hai
// can anyone give me the testcase where it fails

Why can’t you just simply implement what’s given, loop and use pop_back() on the vector while checking H and flipping.
In the end count all Hs and done. I also thought of counting rightmost H’s in the list the checking even/odd but discarded the idea later for a better and reliable implementation

Here’s the failing test btw:

10 7
T H H T T H H T H T
Ans-2

6 4
H H T T T H
Ans-2
I don’t think you’re taking into account that if you flip any rightmost H and it changes the adjacent T into H you again need to flip the full the row.

Hey @qaseem_hasan can you please have a look at this:
Can you please what is wrong with this code???
my testcases all passed but the task from codechef failed.

``````def count_H_T(arr):
return arr.count('T'), arr.count('H')
if  __name__ == '__main__':
testcases = int(input())
for i in range(testcases):
n, k = map(int, input().split())
arr = input().split()

T_count_1st_half, H_count_1st_half = count_H_T(arr[:(n-k)])
T_count_2nd_half, H_count_2nd_half = count_H_T(arr[(n-k):])

if H_count_2nd_half%2==0 or H_count_2nd_half==0:
print(H_count_1st_half)
else:
print(T_count_1st_half)``````

Editorial is up
You’re also making the same mistake as the code above, not keeping into account the change on adjacent T on flipping any rightmost H. Just a simple implementation would do for n<=1000 (maybe more).