POPGATES - Editorial

#include <stdio.h>
//#include <string.h>

int main(void) {
int t;
scanf("%d",&t);
while(t–){
int n,k;
scanf("%d%d",&n,&k);
char C[n];
for(int i=0 ; i<n; i++)
scanf("\n%c",&C[i]);
for(int i=n-1; i>=n-k; i++)
if(C[i]==‘H’){
for(int i=0; i<n; i++)
if(C[i]==‘H’)
C[i]=‘T’;
else
C[i]=‘H’;}
int ans=0;
for(int i=0; i<n-k; i++)
if(C[i]==‘H’)
ans++;

printf("%d\n",ans);

}
return 0;
}
why i am getting Segmentation fault ?

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.

#include
using namespace std;
int main(){
int tc=0;
char c;
cin>>tc;
while(tc–){
int h=0,t=0,n,k,count=0,j=0;
cin>>n>>k;
char ar[k];
for(int i=0;i<n;i++){
cin>>c;
if(i<n-k){
if(c==‘H’||c==‘h’)
h++;
else
t++;
}
else if(i==n-k){
ar[j]=c;
j++;
}else{
if(ar[j-1]!=‘H’&&c==‘H’){
ar[j]=c;
j++;
}
if(ar[j-1]!=‘T’&&c==‘T’){
ar[j]=c;
j++;
}
}
}
if(j%2==0){
if(ar[j-1]==‘H’)
cout<<h<<’\n’;
else
cout<<t<<’\n’;
}
else{
if(ar[j-1]==‘T’)
cout<<h<<’\n’;
else
cout<<t<<’\n’;
}
}
return 0;
}

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 :slightly_smiling_face:

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

Please help me rectify this code. @tmwilliamlin

#include<stdio.h>
void main(){
int T;

scanf("%d",&T);
while(T!=0){
    int N=0,K,count=0,i;
    scanf("%d%d",&N,&K);
    char a[200];
    for(i=0;i<N;i++){
        scanf("%c",&a[i]);
    }

    while(K--){
        if(a[N-1]=='H'){
            N--;
            for(i=0;i<N;i++){
                if(a[i]=='H'){
                    a[i]=='T';
                }
                if(a[i]=='T'){
                    a[i]=='H';
                }
            }
        }
        else{
            N--;
        }

    }

    for(i=0;i<N;i++){
        if(a[i]=='H'){
            count=count+1;
        }
    }
    printf("%d\n",count);
    T=T-1;
}
}

In the question it is never mentioned that value of K is less than N .May be the second test case contains the value of K greater than N.

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.

1 Like

Thanks @tmwilliamlin

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)

I think I solved this in O(n)
Here’s my solution CodeChef: Practical coding for everyone

#include
using namespace std;
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int counter=0;
int n,k;
cin>>n>>k;
char ch[n];
for(int j=0;j<n;j++)
{
scanf("%c",&ch[j]);
}
int x=(n-1);
while(k>0)
{
if(ch[x]==‘H’)
{
for(int m=0;m<=(x-1);m++)
{
if(ch[m]==‘H’)
{
ch[m]=‘T’;
}
else
{
ch[m] = ‘H’;
}
}
x–;
k–;
}
else
{
x–;
k–;
}
}
for(int y=0;y<(x+1);y++)
{
if(ch[y]==‘H’)
{
counter++;
}
}
cout<<counter<<"\n";
}
return 0;
}

working for sample cases but not getting the submission

I have solved this in O(n) time : Submission link - CodeChef: Practical coding for everyone

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))

Did you get AC by using above method??

O(n) solution.

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

int main()
{
int t;
cin >> t;
while(t–)
{
int n,k;
cin >> n >> k;
int consLim = n - k;
int consHeads = 0,consTails = 0;
char tmp;
char coins[n];
for(int i = 0;i<consLim;i++)
{
cin >> tmp;
if(tmp == ‘H’)
consHeads++;
else
consTails++;
}
for(int i = consLim;i<n;i++)
cin >> coins[i];

    int res = 0,headCnt = 0;
    for(int i = n-1;i>=consLim;i--)
    {
        if(coins[i] == 'H')
        {
            if(!(res%2))
            {
                headCnt++;
                res++;
            }
        }
        else
        {
            if(res%2)
            {
                headCnt++;
                res++;
            }
        }
    }
    
    if(headCnt%2)
        cout << consTails << endl;
    else
        cout << consHeads << endl;
    
    
}

}

I was attempting to solve this problem: At the Gates

Issue: Getting NZEC Runtime Error.

Code: CodeChef: Practical coding for everyone

Kindly hekp me finding out why this is happening & what is the fix for the same?

Thanks in advance.

#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
 while(t--)
 {
     int n,k;
     cin>>n>>k;
     
     char arr[n];
     for(int i=0;i<n;i++)
     {
         cin>>arr[i];
         
     }
     int c=0;int e=0;
     for(int i=n-1;i>=n-k;i--)
     {
         
         if(arr[i]=='H' && c%2==0)
         {
             c++;
         }
         if(arr[i]=='T'&&c%2==1)
         {
             c++;
         }
     }
      char ch1;
     if(c%2==0)
     {
          ch1='H';
         
     }else
     {
         ch1='T';
     }
     int ans=0;
     for(int i=0;i<n-k;i++)
     {
         if(arr[i]==ch1)
         {
             ans++;
         }
     }
     
     cout<<ans<<endl;
 }
	return 0;
}

Kindly elaborate the Explanation2 in a better manner. I guess you are not able to convey the explanation due to improper English and Grammar.

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.

can someone explain why my code isn’t working. python 3.6

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

Thank you in advance