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.
Example 2:
Input: arr1 = [0], arr2 = [0] Output: [0]
Example 3:
Input: arr1 = [0], arr2 = [1] Output: [1]
Constraints:
1 <= arr1.length, arr2.length <= 1000
arr1[i]
and arr2[i]
are 0
or 1
arr1
and arr2
have no leading zerosWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to add two numbers that are written in a special way using -2 instead of 2 as the base. The brute force strategy is like manually working through the addition, handling the carry-overs in this unusual system.
Here's how the algorithm would work step-by-step:
def adding_two_negabinary_numbers(first_number, second_number):
maximum_length = max(len(first_number), len(second_number))
first_number = first_number + [0] * (maximum_length - len(first_number))
second_number = second_number + [0] * (maximum_length - len(second_number))
carry = 0
result = []
for i in range(maximum_length):
digit_sum = first_number[i] + second_number[i] + carry
# Handle cases where digit sum is 2 or 3
if digit_sum >= 2:
result.append(digit_sum - 2)
carry = -1
# Handle cases where digit sum is negative.
elif digit_sum < 0:
result.append(digit_sum + 2)
carry = 1
else:
result.append(digit_sum)
carry = 0
# Handle any remaining carry.
if carry != 0:
result.append(carry)
# Remove leading zeros.
while len(result) > 1 and result[-1] == 0:
result.pop()
return result
To add two numbers in negabinary (base -2), we simulate how you add regular binary numbers, but with special handling for carries. The core idea is to add the digits at each position and deal with any resulting value that is not a valid negabinary digit (0 or 1).
Here's how the algorithm would work step-by-step:
def adding_two_negabinary_numbers(arr1, arr2):
result = []
carry = 0
i = len(arr1) - 1
j = len(arr2) - 1
while i >= 0 or j >= 0 or carry != 0:
digit1 = arr1[i] if i >= 0 else 0
digit2 = arr2[j] if j >= 0 else 0
current_sum = digit1 + digit2 + carry
if current_sum == 0:
result.append(0)
carry = 0
elif current_sum == 1:
result.append(1)
carry = 0
elif current_sum == 2:
# 2 in negabinary is '110', so we carry -1.
result.append(0)
carry = -1
elif current_sum == 3:
# 3 in negabinary is '111', so we carry -1.
result.append(1)
carry = -1
elif current_sum == -1:
# -1 in negabinary is '11', so we carry 1.
result.append(1)
carry = 1
i -= 1
j -= 1
# Reverse the result to get the correct order.
result.reverse()
# Removing leading zeros
leading_zero_index = 0
while leading_zero_index < len(result) - 1 and result[leading_zero_index] == 0:
leading_zero_index += 1
final_result = result[leading_zero_index:]
return final_result
Case | How to Handle |
---|---|
Both input arrays are empty | Return an array containing only 0, since 0 + 0 = 0 in any base. |
One input array is empty, the other is not | Return the non-empty array after removing any leading zeros. |
Input arrays containing only zeros | Return an array containing only 0 to represent the sum. |
The result has leading zeros after the addition | Remove leading zeros from the result array before returning it. |
The sum results in a long sequence of 1s requiring multiple carry operations | The carry logic must handle cascading carries correctly until no further carries are needed. |
One input is much longer than the other | The algorithm should correctly handle different array lengths by padding the shorter array implicitly with zeros. |
Result is zero after removing leading zeros, but initial calculation yielded [0,0,0] | Return [0] after stripping leading zeros in this case. |
Large inputs causing potential memory or time constraints | The solution should scale linearly with the size of the input arrays; if extremely large, consider generator-based approach. |