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
Your output - 1
6 4
H H T T T H
Ans-2
Your Output-0
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).