-2
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

001
  • 13,291
  • 5
  • 35
  • 66
  • 1
    Note the troubleshooting information provided by judging systems tends to suck. But if you set up the same (or similar) compiler on your computer and take advantage of debuggers, sanitizers, profilers, and the other groovy stuff you CAN get to work with your development environment, you'll be able to find and fix problems a lot quicker. – user4581301 Feb 02 '22 at 18:53

1 Answers1

1

Insertion may invalidate iterators, the reason being that a new backing array may be allocated. You cannot just assume the old iterator remains valid the way you do e.g. here:

for(int k=0;k<(len1-len2);k++)
{
    b.insert(front_b,'0');
}

the code snippet should be changed to

for(int k=0;k<(len1-len2);k++)
{
    b.insert(b.begin(), '0');
}

or better

// casting unnecessary, if you use size_t or auto when declaring len1 and len2
b.insert(0, static_cast<size_t>(len1-len2), '0');

Note: there are 3 more places in your where you made this mistake.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Tangent: [More information on iterator invalidation](https://stackoverflow.com/questions/6438086/iterator-invalidation-rules-for-c-containers). – user4581301 Feb 02 '22 at 18:54