Leetcode Problem 1073. Adding Two Negabinary Numbers

1073. Adding Two Negabinary Numbers

Leetcode Solutions

Adding Two Negabinary Numbers

  1. Initialize an empty list result to store the sum bits.
  2. Initialize carry to 0.
  3. Use two pointers i and j to traverse arr1 and arr2 from LSB to MSB.
  4. In each iteration, calculate the sum of the current bits and carry.
  5. Based on the sum, determine the bit to append to result and update carry.
  6. If the sum is 2 or 3, append 0 or 1 to result respectively, and set carry to -1.
  7. If the sum is -1, append 1 to result and set carry to 1.
  8. Continue the process until both arrays are traversed and no carry remains.
  9. Reverse result to get the correct bit order.
  10. Remove any leading zeros from result.
  11. Return result.
UML Thumbnail

Adding Two Negabinary Numbers Using Conversion to Decimal

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...