Tap here to switch tabs
Problem
Ask
Submissions

Problem: Adding Two Negabinary Numbers

med
30 min
Explore how to add two numbers represented in negabinary (base -2) format given as arrays. Learn to handle bitwise operations and return the correct sum maintaining negabinary representation without leading zeros.

Statement

You are given two numbers represented in negabinary (base 2-2). Each number is provided as an array of 00s and 11s, ordered from the most significant bit to the least significant bit. For instance, arr = [1,1,0,1] corresponds to the value (2)3+(2)2+(2)0=3(-2)^3 + (-2)^2 + (-2)^0 = -3.

Each input array is guaranteed to contain no leading zeros: either the array is [0], or the first element is 11.

Given two such arrays arr1 and arr2, compute their sum and return the result in the same negabinary array format, with no leading zeros.

Constraints:

  • 11 \leq arr1.length, arr2.length 1000\leq 1000

  • arr1[i] and arr2[i] are 00 or 11

  • arr1 and arr2 have no leading zeros

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Adding Two Negabinary Numbers

med
30 min
Explore how to add two numbers represented in negabinary (base -2) format given as arrays. Learn to handle bitwise operations and return the correct sum maintaining negabinary representation without leading zeros.

Statement

You are given two numbers represented in negabinary (base 2-2). Each number is provided as an array of 00s and 11s, ordered from the most significant bit to the least significant bit. For instance, arr = [1,1,0,1] corresponds to the value (2)3+(2)2+(2)0=3(-2)^3 + (-2)^2 + (-2)^0 = -3.

Each input array is guaranteed to contain no leading zeros: either the array is [0], or the first element is 11.

Given two such arrays arr1 and arr2, compute their sum and return the result in the same negabinary array format, with no leading zeros.

Constraints:

  • 11 \leq arr1.length, arr2.length 1000\leq 1000

  • arr1[i] and arr2[i] are 00 or 11

  • arr1 and arr2 have no leading zeros