Count the no. of Head(H) And Tail(T) in first N-K inputs, then store the (N-k+1)th value and store next value if it is diffent from current stored value. Now if the no. of stored value is even then check last stored value if it is Head then ans will be H if it is Tail then ans will be T and if the no. of stored value is odd then the ans will reverse.
You are using the same variable i in both the outer for loop and the inner for loop.Change the loop
variable name from i to j either in outer or inner for loop.This would clear segmentation default
error
Yesterday I was solving a problem called At The Gates in code chef. I wrote some code in C with no errors in syntax, as the code got compiled with zero warnings and errors.
The code runs fine for the first test case, but gives an segmentation fault error for the second test.
when I debug it, it shows the segmentation fault is due to the code line
if(a[N-1]==‘H’);
Debugger output:
Program received signal SIGSEGV, Segmentation fault. 0x00000000004006d4 in main ( ) at main.c: 15
Since we flip all coins in one operation, each coin is either always the same as coin n-k or always different from coin n-k. So, the answer depends on the final state of coin n-k. From the editorial, coin n-k will always end up tails no matter what the last k-1 coins are.
Guys can you please tell me what is wrong with this code???
my testcases(the testcases mentioned in the question) 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)
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'
print(c[:n-k].count(tmp))
I doubt that I have improper English and grammar (at least, not to the point that makes my explanations incomprehensible). I did make a typo, though. In the second line of explanation 2, I have changed “it heads” into “it is heads”. If you think that any other parts are incorrect then please point them out instead of just saying that I have improper English and grammar.
By the way, you have some obvious grammar mistakes:
The “g” in “grammar” should not be capitalized as “grammar” is a common noun and not a proper noun.
There is no “the” before “Explanation”.
There should be a space between “Explanation” and “2” as “Explanation2” does not make sense as a single own word.
I think what he was trying to say is that it doesn’t depend on the parity of switches until the n-kth coin. Because if the number of switches are even, it would stay as is. so when its a head we output n(tails) since parity becomes odd, and n(heads) if its tails, since parity remains even. If the number of switches are odd, then it would be the opposite of what it is. So if it was initially a tail, it becomes a head and flips again, making the parity even, and if it was initially a head, then now its a tail, and the parity remains odd. It is obvious that the answer is only affected by parity which is only dependent on the n-kth coin, since the parity is odd when the n-kth coin is a head, and even when the n-kth coin is a tail.