Given two array A[0….N-1] and B[0….M-1] of size N and M respectively, representing two numbers such that every element of arrays represent a digit. For example, A[] = { 1, 2, 3} and B[] = { 2, 1, 4 } represent 123 and 214 respectively. The task is to find the sum of both the numbers.
Example 1:
Input : A[] = {1, 2}, B[] = {2, 1}
Output : 33
Explanation:
N=2, and A[]={1,2}
M=2, and B[]={2,1}
Number represented by first array is 12.
Number represented by second array is 21
Sum=12+21=33
 
Example 2:
Input : A[] = {9, 5, 4, 9}, B[] = {2, 1, 4} 
Output : 9763 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function calc_Sum() that takes an array (a), sizeOfArray (n), an array (b), sizeOfArray (m), and return the sum . The driver code takes care of the printing.
Expected Time Complexity: O(N + M).
Expected Auxiliary Space: O(N + M).
 
Constraints:
2 ≤ N ≤ 10^{5}
2 ≤ M ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def calc_Sum (self, arr,  n, brr, m) : 
        #Complete the function
    


Solutions:
class Solution:

	def calc_Sum(self, arr, n, brr, m):
		a = ''
		b = ''
		for i in arr:
			a += str(i)
		for j in brr:
			b += str(j)
		return int(a) + int(b)


class Solution:

	def calc_Sum(self, arr, n, brr, m):
		num1 = ''
		num2 = ''
		for i in arr:
			num1 += str(i)
		for i in brr:
			num2 += str(i)
		return int(num1) + int(num2)


class Solution:

	def calc_Sum(self, arr, n, brr, m):
		d = ''
		d1 = ''
		for i in arr:
			d += str(i)
		for i in brr:
			d1 += str(i)
		return int(d) + int(d1)


class Solution:

	def calc_Sum(self, arr, n, brr, m):
		ans = ''
		carry = 0
		n = n - 1
		m = m - 1
		while n >= 0 and m >= 0:
			a = arr[n] + brr[m] + carry
			ans += str(a % 10)
			carry = a // 10
			n -= 1
			m -= 1
		while n >= 0:
			a = arr[n] + carry
			ans += str(a % 10)
			carry = a // 10
			n -= 1
		while m >= 0:
			a = brr[m] + carry
			ans += str(a % 10)
			carry = a // 10
			m -= 1
		if carry != 0:
			ans += str(carry)
		ans = ans[::-1]
		i = 0
		while ans[i] == '0':
			i += 1
		ans = ans[i:]
		return ans