website/content.en/ChapterFour/1000~1099/1073.Adding-Two-Negabinary-Numbers.md
Given two numbers arr1 and arr2 in base -2, return the result of adding them together.
Each number is given in array format: as an array of 0s and 1s, from most significant bit to least significant bit. For example, arr = [1,1,0,1]represents the number (-2)^3 + (-2)^2 + (-2)^0 = -3. A number arr in array format is also guaranteed to have no leading zeros: either arr == [0] or arr[0] == 1.
Return the result of adding arr1 and arr2 in the same format: as an array of 0s and 1s 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.
Note:
1 <= arr1.length <= 10001 <= arr2.length <= 1000arr1 and arr2 have no leading zerosarr1[i] is 0 or 1arr2[i] is 0 or 1Given two numbers arr1 and arr2 with base -2 , return the result of adding the two numbers. The numbers are given in array form :the array consists of several 0s and 1s,arranged from the most significant bit to the least significant bit. For example,arr = [1,1,0,1] represents the number (-2)^3 + (-2)^2 + (-2)^0 = -3。Numbers in array form also similarly contain no leading zeros:taking arr as an example,this means either arr == [0],or arr[0] == 1。
Return the result of adding arr1 and arr2 in the same representation form. The representation form of the two numbers is:an array containing no leading zeros and consisting of several 0s and 1s。
Hint:
Proof:Since the carry comes from bit k - 1,bit k - 1 has 2 1s 。Now bit k has 2 0s,
So the sum is 2 * (-2)^(k - 1)。
When k is odd,2 * (-2)^(k - 1) = (-1)^(k - 1)* 2 * 2^(k - 1) = 2^k
When k is even,2 * (-2)^(k - 1) = (-1)^(k - 1)* 2 * 2^(k - 1) = -2^k
Taken together, this is (-2)^k,so there is ultimately a 1 at bit k
Proof:Since the carry comes from bit k - 1,bit k - 1 has 2 1s。Now bit k has 1 0 and 1 1,
So the sum is (-2)^k + 2 * (-2)^(k - 1)。
When k is odd,(-2)^k + 2 * (-2)^(k - 1) = -2^k + 2^k = 0
When k is even,(-2)^k + 2 * (-2)^(k - 1) = 2^k - 2^k = 0
Taken together, this is 0,so there is ultimately a 0 at bit k
Proof:Since the carry comes from bit k - 1,bit k - 1 has 2 1s 。Now bit k has 2 1s,
So the sum is 2 * (-2)^k + 2 * (-2)^(k - 1)。
When k is odd,2 * (-2)^k + 2 * (-2)^(k - 1) = -2^(k + 1) + 2^k = 2^k*(1 - 2) = -2^k
When k is even,2 * (-2)^k + 2 * (-2)^(k - 1) = 2^(k + 1) - 2^k = 2^k*(2 - 1) = 2^k
Taken together, this is (-2)^k,so there is ultimately a 1 at bit k
package leetcode
// Solution One Simulate Carry
func addNegabinary(arr1 []int, arr2 []int) []int {
carry, ans := 0, []int{}
for i, j := len(arr1)-1, len(arr2)-1; i >= 0 || j >= 0 || carry != 0; {
if i >= 0 {
carry += arr1[i]
i--
}
if j >= 0 {
carry += arr2[j]
j--
}
ans = append([]int{carry & 1}, ans...)
carry = -(carry >> 1)
}
for idx, num := range ans { // remove leading 0s
if num != 0 {
return ans[idx:]
}
}
return []int{0}
}
// Solution Two Standard simulation,but this method cannot AC,because the test data exceeds 64 bits,ordinary data types cannot store it
func addNegabinary1(arr1 []int, arr2 []int) []int {
return intToNegabinary(negabinaryToInt(arr1) + negabinaryToInt(arr2))
}
func negabinaryToInt(arr []int) int {
if len(arr) == 0 {
return 0
}
res := 0
for i := 0; i < len(arr)-1; i++ {
if res == 0 {
res += (-2) * arr[i]
} else {
res = res * (-2)
res += (-2) * arr[i]
}
}
return res + 1*arr[len(arr)-1]
}
func intToNegabinary(num int) []int {
if num == 0 {
return []int{0}
}
res := []int{}
for num != 0 {
remainder := num % (-2)
num = num / (-2)
if remainder < 0 {
remainder += 2
num++
}
res = append([]int{remainder}, res...)
}
return res
}