class Solution {
public:
void rev(string &str)
{
char temp;
for(int i=0;i<(str.length()/2);i++)
{
temp=str[i];
str[i]=str[str.length()-1-i];
str[str.length()-1-i]=temp;
}
}
string addBinary(string a, string b) {
int len1=a.length(),len2=b.length(),carry=0;
string res="";
string::iterator front_a;
front_a=a.begin();
string::iterator front_b;
front_b=b.begin();
if(len1==len2)
{
}
else if(len1>len2)
{
for(int k=0;k<(len1-len2);k++)
{
b.insert(front_b,'0');
}
}
else
{
for(int k=0;k<(len2-len1);k++)
{
a.insert(front_a,'0');
}
}
len1=a.length();
len2=b.length();
cout<<a<<endl<<b<<endl;
for(int i=len1-1;i>=0;i--)
{
if(a[i]=='0'&&b[i]=='0')
{
if(carry==1)
{
res.push_back('1');
carry=0;
}
else
{
res.push_back('0');
}
}
else if((a[i]=='1'&&b[i]=='0')||(a[i]=='0'&&b[i]=='1'))
{
if(carry==1)
{
res.push_back('0');
carry=1;
}
else
{
res.push_back('1');
}
}
else
{
if(carry==1)
{
res.push_back('1');
carry=1;
}
else
{
res.push_back('0');
}
carry=1;
}
}
rev(res);
string::iterator front;
front=res.begin();
if(carry==1)
{
res.insert(front,'1');
}
return res;
}
};
"10100000101000001010000010100000101000001010000010100000" "1101010010111010100000101000001010000010100000101000001010000010100000" this is the test case which is causing an issue small inputs are leading to correct answer where as large input is causing runtime error