Given two arrays of positive integers, add their elements into a new array. The solution should add both arrays, one by one starting from the 0th index, and split the sum into individual digits if it is a 2–digit number.

For example,

Input:
 
a = { 23, 5, 2, 7, 87 }
b = { 4, 67, 2, 8 }
 
Output: { 2, 7, 7, 2, 4, 1, 5, 8, 7 }
 
 
Input:
 
a = {}
b = { 4, 67, 2, 8 }
 
Output: { 4, 6, 7, 2, 8 }

Practice this problem

The idea is to run a loop that considers every pair of elements present at the same index in both arrays and adds them. If the sum is a 2–digit number, add its digits to the result array; otherwise, add the single-digit sum to the result array. Finally, add the remaining elements of the larger array to the result array.

Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

2 7 7 2 4 1 5 8 7

Java


Download  Run Code

Output:

[2, 7, 7, 2, 4, 1, 5, 8, 7]

Python


Download  Run Code

Output:

[2, 7, 7, 2, 4, 1, 5, 8, 7]

 
Here’s another solution that constructs a string containing the result by appending the sum of each pair of elements to it and finally add each character to a list of integers.

C++


Download  Run Code

Output:

4 6 7 2 8

Java


Download  Run Code

Output:

[4, 6, 7, 2, 8]

Python


Download  Run Code

Output:

[4, 6, 7, 2, 8]

The time complexity of both above-discussed methods is O(m + n) and runs in constant space. Here, m and n are the size of the first and second array, respectively.

 
Author: Aditya Goel