BINSUMMLSC -Editorial

PROBLEM LINK:

Practice

Author: Kunal Demla
Editorialist: Kunal Demla

DIFFICULTY:

Easy

PREREQUISITES:

Maths?

SOLUTIONS:

Setters' Solution
#include<bits/stdc++.h>
using namespace std;
#define ll long long int

string addBinary(string a, string b) {
         int i,j;
        i=a.length();
        j=b.length();
        vector<char> v(max(i,j)+1,'0');
        int k=max(i,j)+1;
        int cary=0;
        while(i&&j){
            i--;
            j--;
            k--;
            if(a[i]=='1'&&b[j]=='1'){
                if(cary)
                   v[k]='1';
                else
                    cary=1;
            }
            else if(a[i]=='1'||b[j]=='1'){
                if(!cary)
                    v[k]='1';
            }
            else{
                if(cary){
                    v[k]='1';
                    cary=0;
                }
            }
        }
        while(i){
            i--;
            k--;
            if(a[i]=='1'&&!cary)
                v[k]='1';
            else if(a[i]=='1'&&cary)
                continue;
            else if (cary){
                v[k]='1';
                cary=0;
            }
        }
        while(j){
            j--;
            k--;
            if(b[j]=='1'&&!cary)
                v[k]='1';
            else if(b[j]=='1'&&cary)
                continue;
            else if (cary){
                v[k]='1';
                cary=0;
            }
        }
        k--;
        if(cary&&k>=0)
            v[k]='1';
        if(v[0]=='0')
            return string (v.begin()+1,v.end());
        return string (v.begin(),v.end());
    }


void solve()
{
    ll n,m,x,y,i,j,k;
    string s,s2;
    cin>>s>>s2;
    cout<<addBinary(s,s2);
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("error.txt", "w", stderr);
freopen("output.txt", "w", stdout);
#endif

int t=1;
// cout<<t<<endl;
// ${2:is Single Test case?}cin>>t;
cin>>t;
int n=t;
while(t--)
{
    //cout<<"Case #"<<n-t<<": ";
    solve();
    cout<<"\n";
}

cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
return 0;
}