Pizza or Broccoli - November Lunchtime 2019 Division 2 (HELP)

Can anyone please help me with this problem, what am i missing here…??

Problem Link : CodeChef: Practical coding for everyone

Solution Link: CodeChef: Practical coding for everyone

#include<bits/stdc++.h>
#include
#define ll long long int

using namespace std;

int main(){

ll t; cin>>t;

while(t–){

ll n,k;  cin>>n>>k;
string s;  cin>>s;
int ans = INT_MIN, tmp = 0, kk = 0;

if(n <= k){
    ans = n;
    cout<<ans<<endl;
}
else{
//   ans = k;
for(int i=0;i<n-k;i++){
    int j = i+k;
    int l = i-1;
    tmp = k;
    for(;j<n;j++){
        if(s[j] == '1'){
            tmp++;
        }
        else{
            break;
        }
    }

    for(;l>=0;l--){
        if(s[l] == '1'){
            tmp++;
        }
        else{
            break;
        }
    }

ans = max(ans,tmp);
}
cout<<ans<<endl;
}

}

return 0;
}

For each element i have skipped k (including it) and then updated my answer to k then run 2 loops to get consecutive 1’s after and before k elements and updated my answer according to that.

Can anyone tell me whats wrong in my approach …??

Consider the testcase:

1
7 6
1010110

Edit:

Longest consecutive run of 1's = 7 obtained by changing the following marked range (of size 6) to all 1's:
 
1010110
 ^    ^
Giving: 
1111111
2 Likes

@ssjgz Thanks a lot i got my mistake it was just due to “=”. I should have to used “=<” but i used “<”.
My bad.

1 Like