Scala version
Scala\n\n\nobject Solution {\n  def addNegabinary(a: Array[Int], b: Array[Int]): Array[Int] = {\n    var large: Array[Int] = a\n    var small: Array[Int] = b\n\
# Scala\n\n```\nobject Solution {\n  def addNegabinary(a: Array[Int], b: Array[Int]): Array[Int] = {\n    var large: Array[Int] = a\n    var small: Array[Int] = b\n\n    if (a.length < b.length) {\n      large = b\n      small = a\n    }\n\n    val result = Array.ofDim[Int](large.length + 2)\n    var carry0 = 0\n    var carry1 = 0\n    var i = 0\n    var lastWrittenIndex = -1\n\n    while (i < large.length) {\n      val largeElem = large(large.length - 1 - i)\n      val smallElem = if (small.length - 1 - i < 0) 0 else small(small.length - 1 - i)\n\n      val res = smallElem + largeElem + carry0\n\n      if ((res % 2) > 0) {\n        lastWrittenIndex = result.length - 1 - i\n        result(result.length - 1 - i) = 1\n      }\n\n      if (res / 2 == 0) {\n        carry0 = carry1\n        carry1 = 0\n      } else {\n        if (carry1 == 1) {\n          carry0 = 0\n          carry1 = 0\n        } else {\n          carry0 = 1\n          carry1 = 1\n        }\n      }\n\n      i += 1\n    }\n\n    if (carry0 == 1) {\n      result(1) = 1\n      lastWrittenIndex = 1\n    }\n\n    if (carry1 == 1) {\n      result(0) = 1\n      lastWrittenIndex = 0\n    }\n\n    if (lastWrittenIndex < 0) {\n      Array(0)\n    } else {\n      if (lastWrittenIndex == 0) {\n        result\n      } else {\n        java.util.Arrays.copyOfRange(result, lastWrittenIndex, result.length)\n      }\n    }\n  }\n}\n\n```
['Scala']