Help me in solving INVEQ problem

My issue

this code is working for any test case i have given but not working for test case -2,please reply for any logical errors

My code

#include <stdio.h>

int main(void) {
	// your code goes here
    int n,m,i=0,j=0,l,flag=0,k;
    scanf("%d",&n);
    while(n--)
    {
        i=0;j=0;
        flag=0;
        int a[199]={0},b[199]={0},sum1=0,sum0=0;
        scanf("%d",&k);
        l=k;
        scanf("%d",&m);
        while(k--)
        {
            if(m%10!=0)
            {a[i]++;
            if(flag==0)
            {j++;
            flag=1;}}
            else{
            b[j]++;
            if(flag==1)
            {i++;
            flag=0;}}
            m=m/10;
        }
        for(i=0;i<l;i++){
        if(a[i]==0&&b[i]==0)
        break;
        if(a[i]!=0)
        sum1++;
        if(b[i]!=0)
        sum0++;
        }
        printf("%d\n",(sum1+sum0)/2);
        }
}


Problem Link: Invert And Equalize Practice Coding Problem

@shahed_786
plz refer my c++ code for better understanding of the logic

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

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
	    cin>>n;
	    string s;
	    cin>>s;
	    int z=0,o=0;
	    for(int i=0;i<n;i++)
	    {
	        if(s[i]=='0')
	        {
	           z++;
	        while(i<n&&s[i]=='0')
	        {
	            i++;
	        }
	        i--;
	        }
	        else
	        {
	            o++;
	         while(i<n&&s[i]=='1')
	        {
	            i++;
	        }   
	        i--;
	        }
	    }
	    cout<<min(z,o)<<endl;
	}
	return 0;
}