#1073

Adding Two Negabinary Numbers

expert · 1065 · lc medium +32 · verified · 37.6% accepted · 337 likes · top 16%

Description

You are given two numbers in base -2, represented as arrays arr1 and arr2 from most-significant to least-significant bit. For example, [1,1,0,1] represents (-2)3 + (-2)2 + (-2)0 = -3.

Return the sum of arr1 and arr2 in the same array format with no leading zeros.

Example 1:

Input: arr1 = [1,1,1,1,1], arr2 = [1,0,1]
Output: [1,0,0,0,0]
Explanation: arr1 represents 11, arr2 represents 5, the output represents 16.

Example 2:

Input: arr1 = [0], arr2 = [0]
Output: [0]

Example 3:

Input: arr1 = [0], arr2 = [1]
Output: [1]

Code

1
2
3