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 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
Negabinary Addition ProcessPowers of -2:(-2)³(-2)²(-2)¹(-2)⁰-8+4-2+1arr1 [1,1,0,1]:1101= -3arr2 [1,0,1]:101= -1Result [1,0,0]:100= -4Addition StepsStep 1: 1 + 1 = 2 → digit=0, carry=1Step 2: 0 + 0 + 1(carry) = 1 → digit=1, carry=0Step 3: 1 + 1 + 0(carry) = 2 → digit=0, carry=1Step 4: 1 + 0 + 1(carry) = 2 → digit=0, carry=1Step 5: 0 + 0 + 1(carry) = 1 → digit=1, carry=0Final result: [1,0,0,0,0]After removing leading zeros: [1,0,0]Key Insight: Negabinary addition uses same carry rules as binary, but with alternating positive/negative place values
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

n
2n
Linear Growth
Space Complexity
O(max(m,n))

Result array length is at most max(m,n) + 2 due to potential carries

n
2n
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
Asked in
Google 15 Facebook 12 Microsoft 8 Amazon 6
12.8K Views
Medium Frequency
~25 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen