Please someone tell why am I getting WA for following code ?
Edge test cases with answers are appreciated.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
string str;
cin>>str;
if(k==0)
{
int sums = 0;
for(char c : str) if(c=='1') sums++;
cout<<sums<<"\n";
continue;
}
int groups1 = 0;
int groups2 = 0;
int i = 0;
int temp;
while(i<n)
{
temp = i;
if(str[i]=='1')
{
if(groups1==0) groups1++;
if(i==n-1) break;
int j=i+1;
while(j<i+k+1)
{
if(str[j]=='1') i=j;
j++;
}
if(temp==i && j>=n) break;
if(temp==i && j<n)
{
if(str[j]=='1')
{
swap(str[j],str[j-1]);
//cout<<"Swapping done\n";
i=j-1;
}
else
{
//cout<<i<<" "<<j<<"\n";
i=j+1;
groups1++;
}
}
}
else i++;
}
i = n-1;
while(i>=0)
{
temp = i;
if(str[i]=='1')
{
if(groups2==0) groups2++;
if(i==0) break;
int j=i-1;
while(j>i-k-1)
{
if(str[j]=='1') i=j;
j--;
}
if(temp==i && j<0) break;
if(temp==i && j>=0)
{
if(str[j]=='1')
{
swap(str[j],str[j+1]);
//cout<<"Swapping done\n";
i=j+1;
}
else
{
//cout<<i<<" "<<j<<"\n";
i=j-1;
groups2++;
}
}
}
else i--;
}
cout<<min(groups1,groups2)<<"\n";
}
return 0;
//11010001001101
//111001001010101 -> 111001001010101
}