<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 sz1 = arr1.size();
        int sz2 = arr2.size();
        int ans_sz = max(sz1,sz2);
        vector<int> ans(ans_sz,0);
        reverse(arr1.begin(),arr1.end());
        reverse(arr2.begin(),arr2.end());
        int carry = 0;
        for(int i=0;i<ans_sz;i++){
            int v1 = 0;
            int v2 = 0;
            if(i<arr1.size()){
                v1= arr1[i];
            }
            if(i<arr2.size()){
                v2 = arr2[i];
            }
            int sum = v1+v2+carry;
            if(sum==3){
                //3 *(-2)^i = 2*(-2)^i + (-2)^i 
                sum =1;
                carry = -1;
            }
            else if(sum==2){
                // -1* (-2)^i * (-2) = -1*(-2)^(i+1)
                carry = -1;
            }else if(sum==-1){
                //sum==-1, v1=0,v2=0
                // -1 * (-2)^i = 1* (-2)^i + (-2)*(-2)^(i)  
                sum = 1;
                carry = 1;
            }else{
                carry =0;
            }
            ans[i] = (sum%2);
        }
    //if carry there 
    if(carry==1){
        ans.push_back(1);
    }else if(carry==-1){
        ans.push_back(1);
        ans.push_back(1);
    }
    reverse(ans.begin(),ans.end());
    //skipp leading zero 
    vector<int> ret;
    bool zero_chk = false;
    for(int i=0;i<ans.size();i++){
        if(ans[i]!=0){
            zero_chk = true;
        }
        if(zero_chk){
            ret.push_back(ans[i]);
        }
    }
    if(zero_chk==false){
        ret.push_back(0);
    }
    return ret;
    }
};
22700