ZEBRA -Help in finding failing test case

Can someone tell which test cases my code is failing ???
https://www.codechef.com/problems/ZEBRA

#include <iostream>
using namespace std;
#include<bits/stdc++.h>
typedef long long ll;

int main() 
{
ios_base::sync_with_stdio(false);
    cin.tie(NULL);

ll t;
cin>>t;
while(t--)
{
ll n,k;
cin>>n>>k;
string s;
cin>>s;
char prev = s[0];
ll cnt=0;
for(ll i=1;i<s.size();i++)
{
    if(prev != s[i])
    {
        cnt++;
        prev = s[i];
    }
   
}
if(cnt<k)
{
        cout<<-1;
}
else
{

ll fone=-1;
ll fzero=-1;
for(int i=n-1;i>=0;i--)
{
    if(s[i]=='1' && fone==-1)
    {
        fone =i+1;
    }
   else if(s[i]=='0' && fzero==-1)
    {
        fzero =i+1;
    }
   else if(fone!=-1 && fzero!=-1)
   {
       break;
   }
   
}

if(k%2==0)  // k even hai
{
   char x = s[0];
   if(x=='1')  
   {
       cout<<fone;
   }
   else
   {
        cout<<fzero;
   }
    
}
else
{
    ll x = s[0];
   if(x=='1')
   {
         cout<<fzero;
   }
   else
   {
        cout<<fone;
   }
}
cout<<"\n";
}
}
}
2
6 1
000000
6 1
000000

Another person who might benefit from “Further consider” advice here :slight_smile: e.g. (untested):

#include <iostream>
using namespace std;
#include<bits/stdc++.h>
typedef long long ll;

ll solve()
{
    ll n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    char prev = s[0];
    ll cnt=0;
    for(ll i=1;i<s.size();i++)
    {
        if(prev != s[i])
        {
            cnt++;
            prev = s[i];
        }
        
    }
    if(cnt<k)
    {
        return -1;
    }
    
    ll fone=-1;
    ll fzero=-1;
    for(int i=n-1;i>=0;i--)
    {
        if(s[i]=='1' && fone==-1)
        {
            fone =i+1;
        }
        else if(s[i]=='0' && fzero==-1)
        {
            fzero =i+1;
        }
        else if(fone!=-1 && fzero!=-1)
        {
            break;
        }
        
    }
    
    if(k%2==0)  // k even hai
    {
        char x = s[0];
        if(x=='1')  
        {
            return fone;
        }
        else
        {
            return fzero;
        }
        
    }
    else
    {
        ll x = s[0];
        if(x=='1')
        {
            return fzero;
        }
        else
        {
            return fone;
        }
    }
}

int main() 
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;
    cin>>t;
    while(t--)
    {
        cout << solve() << endl;
    }
}

1 Like