Getting WA in At the Gates

Can someone plz tell me why my submission is getting WA. It seems to run fine on the sample case and I’m unable to come up with and edge case that will fail it.

simply,count the no of heads if no of heads is even then you can consider one more head otherwise you have to consider tail ,after k considerations if that count even print the no of head left else print the no of odd left

That’s what I did:
Counted number of heads in last k coins. If even, then count no. of heads in first (n-k) coins else count tails in first (n-k) coins

And the code seems to be going right in sample test cases as well. However, I’m unable to figure out why it’s going wrong in the actual test cases

Will you please refer this code

from sys import stdin
for _ in range(int(stdin.readline())):
    n,k = map(int,stdin.readline().split())
    c = list(map(str,stdin.readline().split()))
    tmp = 'H'
    for i in range(n-1,n-k-1,-1):
        if c[i]=='H' and tmp=='H':
            tmp = 'T'
        elif c[i]=='T' and tmp=='T':
            tmp = 'H'

Traversing the list from right to left / from n-1 to n-k-1

  1. Checking the current value and tmp value, if both are same change the tmp value.
  2. then count the number of times the tmp value occurred from the index 0 to n-k-1 in the list.

@nagesh_reddy I get your code. But, could you please tell me why my code isn’t working.

for the TC :
17 11

Answer should be 4, but your code is giving 2 .

Read the question carefully :

In one operation, you should remove the rightmost coin present on the table, and if this coin was showing heads right before it was removed, then you should also flip all the remaining coins. (If a coin was showing heads, then after it is flipped, it is showing tails, and vice versa.)

The above sentence is from problem statement and this sentence is clearly explaining if the right most coin was heads right before it was removed, then you should flip the remaining coins.

Please read carefully heads right before it was removed.

That means we have to flip the coins after removing the right most heads coin. So no need to flip the coins when the right most coin was tails right before it was removed.

Hence your approach is wrong.

Understood it now. Thanks a lot for your help.

I actually thought that when we perform a flip, only first (n-k) coins are to be flipped

1 Like