A retired mechanical adding machine at a currency-exchange counter stores every amount using a peculiar digit scheme instead of ordinary binary: each digit is 0 or 1, but the digit at position i counted from the right (0-indexed) contributes digit * (-2)^i to the value, rather than digit * 2^i. Because the place values alternate between positive and negative, the machine can represent both positive and negative amounts without ever needing a separate sign digit.
You are given two amounts already encoded this way, each written as a string of characters 0/1 from the most significant digit to the least significant digit, with no leading zero unless the encoded value is exactly 0 (in which case the string is exactly "0"). Compute the sum of the two encoded values and output it using the very same encoding: most significant digit first, and no leading zero unless the sum is exactly 0.
Line 1: a string A of characters 0/1.
Line 2: a string B of characters 0/1.
A single line: the string encoding of A + B (as values), most significant digit first, with no leading zero unless the result is exactly "0".
A and B is 0 or 1A nor B has a leading zero unless it is exactly the single character 0Example 1
Input
110 011
Expected
1
Explanation
A = "110" has digits (from the right) 0,1,1 with weights 1,-2,4, so its value is 0*1 + 1*(-2) + 1*4 = 2. B = "011" has digits 1,1,0 with weights 1,-2,4, so its value is 1*1 + 1*(-2) + 0*4 = -1. The sum is 2 + (-1) = 1, and 1 is encoded simply as the single digit "1".
Example 2
Input
1 1
Expected
110
Explanation
Both A and B are the single digit "1", each worth 1*(-2)^0 = 1, so the value sum is 2. Encoding 2 requires a carry chain: digit 0 is 0 with a carry that resolves over the next two positions to digits 1 and 1, giving "110" (0*1 + 1*(-2) + 1*4 = -2 + 4 = 2).
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →