Adding Two Negabinary Numbers - Problem
Negabinary Addition Challenge
Welcome to the fascinating world of negabinary (base -2) arithmetic! Unlike regular binary where each position represents powers of 2, in negabinary each position represents powers of -2.
You're given two numbers
Example:
1×(-2)³ + 1×(-2)² + 0×(-2)¹ + 1×(-2)⁰ = -8 + 4 + 0 + 1 = -3
Your task: Add these two negabinary numbers and return the result in the same array format, ensuring no leading zeros (unless the result is exactly [0]).
Key Challenge: Unlike regular binary addition, carries in negabinary can be tricky because we're working with negative bases!
Welcome to the fascinating world of negabinary (base -2) arithmetic! Unlike regular binary where each position represents powers of 2, in negabinary each position represents powers of -2.
You're given two numbers
arr1 and arr2 represented as arrays in base -2 format. Each array contains only 0s and 1s, arranged from most significant bit to least significant bit.Example:
[1,1,0,1] represents:1×(-2)³ + 1×(-2)² + 0×(-2)¹ + 1×(-2)⁰ = -8 + 4 + 0 + 1 = -3
Your task: Add these two negabinary numbers and return the result in the same array format, ensuring no leading zeros (unless the result is exactly [0]).
Key Challenge: Unlike regular binary addition, carries in negabinary can be tricky because we're working with negative bases!
Input & Output
example_1.py — Basic Addition
$
Input:
arr1 = [1,1,1,1,1], arr2 = [1,0,1]
›
Output:
[1,0,0,1,0]
💡 Note:
arr1 represents -3 (1×(-2)³ + 1×(-2)² + 0×(-2)¹ + 1×(-2)⁰ = -8+4+0+1 = -3). arr2 represents -1 (1×(-2)² + 0×(-2)¹ + 1×(-2)⁰ = 4+0+1 = 5). Wait, let me recalculate: arr1 = [1,1,1,1,1] = 1×(-2)⁴ + 1×(-2)³ + 1×(-2)² + 1×(-2)¹ + 1×(-2)⁰ = 16-8+4-2+1 = 11, arr2 = [1,0,1] = 1×(-2)² + 0×(-2)¹ + 1×(-2)⁰ = 4+0+1 = 5. Sum = 16, which in negabinary is [1,0,0,1,0].
example_2.py — Zero Result
$
Input:
arr1 = [1], arr2 = [1,1]
›
Output:
[0]
💡 Note:
arr1 = [1] represents 1×(-2)⁰ = 1. arr2 = [1,1] represents 1×(-2)¹ + 1×(-2)⁰ = -2+1 = -1. Sum = 1 + (-1) = 0, represented as [0].
example_3.py — Negative Result
$
Input:
arr1 = [0], arr2 = [1,0]
›
Output:
[1,0]
💡 Note:
arr1 = [0] represents 0. arr2 = [1,0] represents 1×(-2)¹ + 0×(-2)⁰ = -2+0 = -2. Sum = 0 + (-2) = -2, which remains [1,0] in negabinary.
Visualization
Tap to expand
Understanding the Visualization
1
Align Numbers
Position both numbers with rightmost digits aligned
2
Add Column by Column
Start from rightmost column, add digits plus any carry
3
Handle Carries
When sum ≥ 2, carry = sum // 2, digit = sum % 2
4
Continue Left
Process all columns and remaining carries
5
Remove Leading Zeros
Clean up result by removing unnecessary leading zeros
Key Takeaway
🎯 Key Insight: Negabinary addition follows the same digit-by-digit process as regular binary addition, but the alternating positive/negative place values create the unique negabinary representation. The carry mechanism (carry = sum // 2, digit = sum % 2) works the same way!
Time & Space Complexity
Time Complexity
O(max(m,n))
Single pass through both arrays where m and n are lengths of input arrays
✓ Linear Growth
Space Complexity
O(max(m,n))
Result array length is at most max(m,n) + 2 due to potential carries
⚡ Linearithmic Space
Constraints
- 1 ≤ arr1.length, arr2.length ≤ 1000
- arr1[i] and arr2[i] are 0 or 1
- arr1 and arr2 have no leading zeros except when the number is 0
- Each array represents a valid negabinary number
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code