Wrong Addition (Codeforces 1619C)
Tags: Implement, implementation

Tanya is learning how to add numbers, but so far she is not doing it correctly. She is adding two numbers $$$a$$$ and $$$b$$$ using the following algorithm:  If one of the numbers is shorter than the other, Tanya adds leading zeros so that the numbers are the same length.  The numbers are processed from right to left (that is, from the least significant digits to the most significant).  In the first step, she adds the last digit of $$$a$$$ to the last digit of $$$b$$$ and writes their sum in the answer.  At each next step, she performs the same operation on each pair of digits in the same place and writes the result to the left side of the answer.  For example, the numbers $$$a = 17236$$$ and $$$b = 3465$$$ Tanya adds up as follows:$$$$$$ \large{ \begin{array}{r} + \begin{array}{r} 17236\\ 03465\\ \end{array} \\ \hline \begin{array}{r} 1106911 \end{array} \end{array}} $$$$$$  calculates the sum of $$$6 + 5 = 11$$$ and writes $$$11$$$ in the answer.  calculates the sum of $$$3 + 6 = 9$$$ and writes the result to the left side of the answer to get $$$911$$$.  calculates the sum of $$$2 + 4 = 6$$$ and writes the result to the left side of the answer to get $$$6911$$$.  calculates the sum of $$$7 + 3 = 10$$$, and writes the result to the left side of the answer to get $$$106911$$$.  calculates the sum of $$$1 + 0 = 1$$$ and writes the result to the left side of the answer and get $$$1106911$$$. As a result, she gets $$$1106911$$$.You are given two positive integers $$$a$$$ and $$$s$$$. Find the number $$$b$$$ such that by adding $$$a$$$ and $$$b$$$ as described above, Tanya will get $$$s$$$. Or determine that no suitable $$$b$$$ exists.

Input: The first line of input data contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.Each test case consists of a single line containing two positive integers $$$a$$$ and $$$s$$$ ($$$1 \le a \lt s \le 10^{18}$$$) separated by a space.

Output: For each test case print the answer on a separate line.If the solution exists, print a single positive integer $$$b$$$. The answer must be written without leading zeros. If multiple answers exist, print any of them.If no suitable number $$$b$$$ exists, output -1.

Note: The first test case is explained in the main part of the statement.In the third test case, we cannot choose $$$b$$$ that satisfies the problem statement.

Sample Inputs:
6
17236 1106911
1 5
108 112
12345 1023412
1 11
1 20

Sample Outputs:
3465
4
-1
90007
10
-1

Solution (Python):
def solve(a, s):
    b = ''
    for digit in a[::-1]:
        if not len(s):
            return '-1'
        if int(digit) <= int(s[-1]):
            b += str(int(s[-1]) - int(digit))
            s = s[:-1]
        else:
            if len(s) < 2:
                return '-1'
            tmp = int(s[-2:]) - int(digit)
            if tmp > 9 or tmp < 0:
                return '-1'
            b += str(tmp)
            s = s[:-2]
    if s:
        b += s[::-1]
    return b[::-1]
t = int(input())
ans = []
for _ in range(t):
    (a, s) = input().split()
    b = solve(a, s)
    ans.append(str(int(b)))
print('\n'.join(ans))

Solution (Python):
t = int(input())
ans = [0] * t
for j in range(t):
    (a, su) = map(int, input().split())
    i = 0
    b = 0
    while a != 0 or su != 0:
        t1 = a % 10
        t2 = su % 10
        t3 = su % 100
        if su == 0 and a != 0:
            b = -1
            break
        if t2 >= t1:
            di = 10 ** i * (t2 - t1)
            b = b + di
            i = i + 1
            su //= 10
            a //= 10
        elif t3 > t1:
            di = 10 ** i * (t3 - t1)
            b = b + di
            i = i + 1
            su //= 10
            a //= 10
            if su % 10 > 1:
                b = -1
                break
            su //= 10
        else:
            b = -1
            break
    ans[j] = b
for i in ans:
    print(i)

Solution (Python):
import sys
import copy
import heapq
import bisect
import functools
from math import *
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import groupby
import itertools
from itertools import accumulate
from decimal import Decimal, ROUND_HALF_UP
readline = lambda : sys.stdin.readline().rstrip()

def solve():
    (a, s) = map(int, readline().split())
    (b, t) = (0, 1)
    while s != 0:
        da = a % 10
        ds = s % 10
        a //= 10
        s //= 10
        if da <= ds:
            b += t * (ds - da)
        elif s % 10 == 1:
            s //= 10
            b += t * (10 + ds - da)
        else:
            print(-1)
            return
        t *= 10
    print(b if a == 0 else -1)

def main():
    t = 1
    t = int(readline())
    for i in range(t):
        solve()
if __name__ == '__main__':
    main()