Whats wrong with my post | At the Gates | CodeChef

Most of coder did this program at O(n3) but I did it at O(n2), even sample test case is right but it is say WA…Please help!

#include
using namespace std;

int main() {

int i,K,c,d,n,T;
cin>>T;
char a[100];

for(i=1; i<=T; i++) {
    cin>>n>>K;
    c=0;
    d=0;
    for(int j=0;j<100;j++) a[j]=NULL;
    for(int j=0;j<n;j++) {
        cin>>a[j];
        if(a[j] == 'H' && j>=n-K) 
            c++;
        if(a[j] == 'H')
            d++;
    }
    
    if(c%2==0) {
        cout<<(d-c)<<endl; 
    }
    else cout<<(n-K-d+c)<<endl; 
    
    
    
    
}

return 0;

}

You have to flip every remaining coin,everytime when you get HEAD. In your Program you are flipping coins which only will remain at last.
Try this testcase:
1
5 2
T H H T H
Your Ans: 1
Correct Ans: 2

Thanks man!!

1 Like