You are given two integers arr1 and arr2 written in base -2 (negabinary). Each number is an array of 0s and 1s, ordered from the most significant bit to the least significant bit. The bit at position k, counting from the right starting at 0, has weight (−2)k. For example, [1,1,0,1] represents (−2)3+(−2)2+(−2)0=−8+4+1=−3.
Return the sum of arr1 and arr2 in the same format.
Function Signature
def add_negabinary(arr1: list[int], arr2: list[int]) -> list[int]:
...
Rules
-
Input and output arrays list bits from the most significant to the least significant.
-
Neither input has leading zeros: each one is either
[0]
or starts with
1
.
-
The output must have no leading zeros. A sum of zero is returned as
[0]
.
Constraints
-
1 <= len(arr1), len(arr2) <= 1000
-
Every
arr1[i]
and
arr2[i]
is
0
or
1
.
-
arr1
and
arr2
have no leading zeros.
Examples
Input: arr1 = [1,1,1,1,1], arr2 = [1,0,1]
Output: [1,0,0,0,0]
arr1 is 16−8+4−2+1=11 and arr2 is 4+1=5. Their sum 16 equals (−2)4, which is a 1 followed by four zeros.
Input: arr1 = [1], arr2 = [1]
Output: [1,1,0]
The sum is 2, and 2=4−2 sets the bits for (−2)2 and (−2)1. The result is one bit longer than the inputs plus one more, because the carry continues past the end of both arrays.
Input: arr1 = [1], arr2 = [1,1]
Output: [0]
[1,1] is −2+1=−1, so the sum is 0. Zero is written as [0], with no leading zeros.