Sum of Big integers.

Easy
0/40
Average time to solve is 15m
profile
Contributed by
45 upvotes
Asked in companies
UberVisaInformatica

Problem statement

You have been given two integers ‘NUM1’ and ‘NUM2’ as a string. Your task is to print the sum of both the numbers.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. The 'T' test cases are as follows: 

The only line of each test case contains two space-separated integers ‘NUM1’ and ‘NUM2’ representing two integers whose sum you need to return. As numbers are pretty large ‘NUM1’ and ‘NUM2’ will be provided as strings.
Note:
You do not need to print anything; it has already been taken care of. Just implement the function.
Output Format:
For each test case, print the sum of two numbers.
Constraints:
1 <= T <= 10000
1 <= NUM1 <= 10^50
1 <= NUM2 <= 10^50

Where ‘T’ is the number of test cases. ‘NUM1’ and ‘NUM2’ are the numbers whose sum you have to compute.  

Time Limit: 1sec
Sample Input 1:
4
1 1
2 1
17 13
11 24
Sample Output 1:
2
3
30
35

Explanation For Sample Input 1:

In the first test case, 2 is the sum of the two numbers. Therefore the answer is 2.
In the second test case, 3 is the sum of the two numbers. Therefore the answer is 3.
In the third test case, 30 is the sum of the two numbers. Therefore the answer is 30.
In the fourth test case, 35 is the sum of the two numbers. Therefore the answer is 35.
Sample Input 2:
2
1555555555555555555500 20
4 3
Sample Output 2:
1555555555555555555520
7

Explanation For Sample Input 2:

In the first test case, 1555555555555555555520 is the sum of the two numbers. Therefore the answer is 1555555555555555555520.
In the second test case, 7 is the sum of the two numbers. Therefore the answer is 7.
Hint

Can you try to implement it in strings?

Approaches (1)
Using Strings.

 

 

Let us store ‘NUM1’ and ‘NUM2’ as strings rather than storing them as integers. We will calculate the sum as follows: 

 

  1. We will add zeroes before the smaller string among ‘NUM1’ and ‘NUM2’ and make them of equal length.
  2. We will add one more zero before both the numbers because the sum of both the numbers can contain at most one extra digit than the larger number.
  3. We will start adding from the unit’s place i.e backward.
  4. We will also define a variable ‘CARRY’ which stores carry from the previous position. Initially, it will be initialized to zero.
  5. At the ith position, we can calculate the value by (‘NUM1[i]’ - ‘0’+ ‘NUM2[i]’- ‘0’ + ‘CARRY’) modulus ‘10’.
  6. ‘CARRY’ for the next position i.e. ‘i’-1th position will be (‘NUM1[i]’ - ‘0’+ ‘NUM2[i]’- ‘0’ + ‘CARRY’)/10.
  7. Remove leading zeroes from the answer.
  8. Return the ‘SUM’.
Time Complexity

O(max(length(‘num1’), length(‘num2’))), where length(‘num1’) represents number of digits in ‘num1’ and length(‘num2’) represents number of digits in ‘num2’.

 

As we just iterating over the strings.

Space Complexity

O(max(length(‘num1’), length(‘num2’))), where length(‘num1’) represents number of digits in ‘num1’ and length(‘num2’) represents number of digits in ‘num2’.

 

As we declare string to store the answer.

Code Solution
(100% EXP penalty)
Sum of Big integers.
Full screen
Console