Wrong Answer (POPGATES) new

What is wrong with my code. Why I’m getting WA?
link of code is here
https://www.codechef.com/submit/POPGATES

Wrong link, this is the submit button link

Please link the code in “My Submission”

Well, saw your code through your submission

I think it fails for most of the guys not taking into account that flipping the rightmost H has an effect on the next adjacent T as it will be changed into H and you have to flip the whole list again. This causes a chain of many flips that your even odd method can’t account for.

Here’s some tests:
5 2
H T H T H
Your Output-1
Ans-2

10 7
T H H T T H H T H T
Ans-2
Your output - 1

Try doing a simple implementation using vector by pop_back() the rightmost and if it’s H simply flip the whole vector in using a nested loop. Then check the no. of Hs in the end.

1 Like

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)