Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def add_arrays(arr1, arr2):
- # make sure arr1 is the shorter array
- if len(arr1) > len(arr2):
- arr1, arr2 = arr2, arr1
- # add the arrays element-wise
- result = []
- carry = 0
- for i in range(len(arr1)):
- res = arr1[i] + arr2[i] + carry
- result.append(res % 10)
- carry = res // 10
- # add the remaining elements of arr2, if any
- for i in range(len(arr1), len(arr2)):
- res = arr2[i] + carry
- result.append(res % 10)
- carry = res // 10
- # add the carry, if any
- if carry > 0:
- result.append(carry)
- return result
- # read size of arrays
- size = input().strip().split()
- # read the first array
- arr1 = input().strip().split()
- arr1 = [int(x) for x in arr1]
- # read the second array
- arr2 = input().strip().split()
- arr2 = [int(x) for x in arr2]
- # add the arrays and print the result
- result = add_arrays(arr1, arr2)
- print(" ".join(str(x) for x in result))
Advertisement
Add Comment
Please, Sign In to add comment