K-Foldable String Problem Code: KFOLD Codechef Solution

 #include <bits/stdc++.h>
using namespace std;
int main() {  
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  int t;
  cin>>t;
  while(t--){
      int n,k;
      cin>>n>>k;
      string s;
      cin>>ws;
      cin>>s;
      int zero=0,one=0;
      for(char c:s)
          if(c=='0')zero++;
          else if(c=='1')one++;
      int rounds=n/(k);
      int ii=zero/(rounds),jj=one/rounds;
      if(rounds*ii!=zero||rounds*jj!=one||rounds*k!=n){cout<<"IMPOSSIBLE";}
      else{
          for(int x=0;x<rounds;x++){
              if(x&1){
                  cout<<string(jj,'1')<<string(ii,'0');
              }
              else{
                  cout<<string(ii,'0')<<string(jj,'1');
              }
          }
      }
      if(t)cout<<'\n';
 }
}

Comments