Add two numbers represented by two arrays | Return sum of two number represented by the arrays . Size of a [ ] is greater than b [ ] . It is made sure be the wrapper function ; array to store sum . ; Until we reach beginning of array . we are comparing only for second array because we have already compare the size of array in wrapper function . ; find sum of corresponding element of both array . ; Finding carry for next sum . ; If second array size is less the first array size . ; Add carry to first array elements . ; If there is carry on adding 0 index elements . append 1 to total sum . ; Converting array into number . ; Wrapper Function ; Making first array which have greater number of element ; Driven Code
def calSumUtil ( a , b , n , m ) : NEW_LINE INDENT sum = [ 0 ] * n NEW_LINE i = n - 1 NEW_LINE j = m - 1 NEW_LINE k = n - 1 NEW_LINE carry = 0 NEW_LINE s = 0 NEW_LINE while j >= 0 : NEW_LINE INDENT s = a [ i ] + b [ j ] + carry NEW_LINE sum [ k ] = ( s % 10 ) NEW_LINE carry = s // 10 NEW_LINE k -= 1 NEW_LINE i -= 1 NEW_LINE j -= 1 NEW_LINE DEDENT while i >= 0 : NEW_LINE INDENT s = a [ i ] + carry NEW_LINE sum [ k ] = ( s % 10 ) NEW_LINE carry = s // 10 NEW_LINE i -= 1 NEW_LINE k -= 1 NEW_LINE DEDENT ans = 0 NEW_LINE if carry : NEW_LINE INDENT ans = 10 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT ans += sum [ i ] NEW_LINE ans *= 10 NEW_LINE DEDENT return ans // 10 NEW_LINE DEDENT def calSum ( a , b , n , m ) : NEW_LINE INDENT if n >= m : NEW_LINE INDENT return calSumUtil ( a , b , n , m ) NEW_LINE DEDENT else : NEW_LINE INDENT return calSumUtil ( b , a , m , n ) NEW_LINE DEDENT DEDENT a = [ 9 , 3 , 9 ] NEW_LINE b = [ 6 , 1 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE print ( calSum ( a , b , n , m ) ) NEW_LINE