Adding Two Negabinary Numbers || JAVASCRIPT || Solution by Bharadwaj
Approach\nSimulating Negabinary Addition\n\n# Complexity\n- Time complexity:\nO(max(n, m))\n\n- Space complexity:\nO(max(n, m))\n\n# Code\n\nvar addNegabinary =
# Approach\nSimulating Negabinary Addition\n\n# Complexity\n- Time complexity:\nO(max(n, m))\n\n- Space complexity:\nO(max(n, m))\n\n# Code\n```\nvar addNegabinary = function(arr1, arr2) {\n    // Initialize variables for carry, result, and array indices\n    let carry = 0;\n    let ans = [];\n    let x = arr1.length - 1;\n    let y = arr2.length - 1;\n\n    // Iterate through arrays and handle addition\n    while (x >= 0 || y >= 0 || carry) {\n        // Add current elements and carry\n        if (x >= 0) carry += arr1[x];\n        if (y >= 0) carry += arr2[y];\n\n        // Push the least significant bit of the sum to the result\n        ans.push(carry & 1);\n\n        // Update carry for the next bit\n        carry = -(carry >> 1);\n\n        // Move to the next elements in the arrays\n        x--;\n        y--;\n    }\n\n    // Remove leading zeros in the result\n    while (ans.length > 1 && ans[ans.length - 1] === 0) {\n        ans.pop();\n    }\n\n    // Reverse the result array to get the final answer\n    return ans.reverse();\n};\n```
['JavaScript']