<p>Given two numbers <code>arr1</code> and <code>arr2</code> in base <strong>-2</strong>, return the result of adding them together.</p>

<p>Each number is given in <em>array format</em>:&nbsp; as an array of 0s and 1s, from most significant bit to least significant bit.&nbsp; For example, <code>arr = [1,1,0,1]</code> represents the number <code>(-2)^3&nbsp;+ (-2)^2 + (-2)^0 = -3</code>.&nbsp; A number <code>arr</code> in <em>array, format</em> is also guaranteed to have no leading zeros: either&nbsp;<code>arr == [0]</code> or <code>arr[0] == 1</code>.</p>

<p>Return the result of adding <code>arr1</code> and <code>arr2</code> in the same format: as an array of 0s and 1s with no leading zeros.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr1 = [1,1,1,1,1], arr2 = [1,0,1]
<strong>Output:</strong> [1,0,0,0,0]
<strong>Explanation: </strong>arr1 represents 11, arr2 represents 5, the output represents 16.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr1 = [0], arr2 = [0]
<strong>Output:</strong> [0]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr1 = [0], arr2 = [1]
<strong>Output:</strong> [1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr1.length,&nbsp;arr2.length &lt;= 1000</code></li>
	<li><code>arr1[i]</code>&nbsp;and <code>arr2[i]</code> are&nbsp;<code>0</code> or <code>1</code></li>
	<li><code>arr1</code> and <code>arr2</code> have no leading zeros</li>
</ul>

class Solution {
public:
    vector<int> addNegabinary(vector<int>& arr1, vector<int>& arr2) {
        int n=arr1.size();
        int m=arr2.size();
        int i=0,j=0,carry=0,temp;
        reverse(arr1.begin(),arr1.end());
        reverse(arr2.begin(),arr2.end());
        vector<int>ans;
        while(i<n&& j<m){
            temp=arr1[i]+arr2[j]+carry;
            carry=0;
            if(temp==2){
                carry=-1;
                temp=0;
            }
            ans.push_back(temp);
            i++;
            j++;
        }
        while(i<n){
            temp=arr1[i]+carry;
            carry=0;
            if(temp==2){
                carry=-1;
                temp=0;
            }
            ans.push_back(temp);
            i++;
        }
        while(j<m){
            temp=arr2[j]+carry;
            carry=0;
            if(temp==2){
                carry=-1;
                temp=0;
            }
            ans.push_back(temp);
            j++;
        }
        if(carry)ans.push_back(carry);



        int carry2=0;
        carry=0;
        vector<int>res;
        for(auto i :ans){
            cout<<i<<" ";
            temp=i+carry;
            carry=0;
            if(temp==2){
                temp=0;
                carry=-1+carry2;
                carry2=0;
            }
            else if(temp==-1){
                temp=1;
                carry=1+carry2;
                carry2=0;
            }
            else{
                carry=carry2;
                carry2=0;
            }
            res.push_back(temp);
        }
        while(carry==-1){
            res.push_back(1);
            carry=1+carry2;
            carry2=0;
        }
        if(carry)res.push_back(carry);
        if(carry2)res.push_back(carry2);
        i=res.size()-1;
        while(i>0 && res[i]==0){
            res.pop_back();
            i--;
        }

        reverse(res.begin(),res.end());
        return res;
    }
};
22800