Codechef 56 rated question Binary Substituion getting WA on two subtasks please guide

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

int main() {
    int t;cin>>t;
    while(t--){
       int n;cin>>n;
       string s;cin>>s;
      //  int ust=0,st=0;
      int ch=2;
      vector<int>startarr;
      vector<int>endarr;
      vector<char>elem;
      int cnt=0;
      int ust=0,st=0;
     for(int i=0;i<n;i++){
      
      if(s[i]=='0')ust++;
      else st++;
     }
     if(ust==0 or st==0){
      cout<<s.length()<<' '<<0<<endl;
      continue;
     }
      int i=0;
      while(i<n-1 and n>1){
        
        
        
          // cout<<endl<<i<<endl;
          if((s[i]=='0' and s[i+1]=='1') or (s[i]=='1' and s[i+1]=='0'))
          {
             int start=i;
             int end=i+1;
             //here im trying to find 1st 10 or 01 and after that im travering in both direction i.e Right and Left using p1 and p2
             //to find out set and unset count and wherever im getting unset==set  im updating start and end pointer so that i should be 
             //able to delete that range from start to end 
             //p1->pointer1 for backward iteration and p2->pointer2 for forward iteration 
              //1234
             // 1100
             //p1->2 p2->3
             //p1--  p2++ for traversing in both direction 
             
             int p1=i;
             int p2=i+1;
              ust=0,st=0;
             while(p1>=0 and p2<n){
                
                if(s[p1]=='0')ust++;
                if(s[p2]=='0')ust++;
                if(s[p1]=='1')st++;
                if(s[p2]=='1')st++;
                 if(ust==st){
                  start=p1;
                  end=p2;
                  // cout<<"done..";
                }
                p1--;p2++;
             }
             bool check=true;
             while(p1>=0){
               if(check)p2--,check=false;
                
                if(s[p1]=='0')ust++;
                
                if(s[p1]=='1')st++;
                if(ust==st){
                  start=p1;
                  end=p2;
                  
                }
                p1--;
             }
             while(p2<n){
             if(check) p1++,check=false;
                if(s[p2]=='0')ust++;
                
                if(s[p2]=='1')st++;
                if(ust==st){
                  start=p1;
                  end=p2;  
                }
                p2++;
             }
            
            char ch=(st>ust)?'0':'1';
            elem.push_back(ch);
            startarr.push_back(start+1);
            endarr.push_back(end+1);
    
             string newstr="";
            for(int i=0;i<start;i++){
              newstr+=s[i];
            }
            newstr+=ch;
            for(int i=end+1;i<n;i++){
              newstr+=s[i];
            }

            s=newstr;
            
            
            // i=p2;
            i=0;
            cnt++;
          }
          else i++;
          n=s.length();
        
      }
          cout<<1<<' '<<startarr.size()<<endl;
         for(int i=0;i<startarr.size();i++){
            cout<<startarr[i]<<' '<<endarr[i]<<' '<<elem[i]<<endl;
         }

     }

	
    
	return 0;
}