Digits of element wise sum of two arrays into a new array

Given two arrays of positive integers A and B of sizes M and N respectively, the task is to push A[i] + B[i] into a new array for every i = 0 to min(M, N) and print the newly generated array in the end. If the sum is a two-digit number then break the digits into two elements i.e. every element of the resultant array must be a single digit number.

Examples:

Input: A = {2, 3, 4, 5}, B = {1, 12, 3}
Output: 3 1 5 7 5
2 + 1 = 3
3 + 12 = 15 = 1 5
4 + 3 = 7
5
Hence the resultant array will be {3, 1, 5, 7, 5}



Input: A = {23, 5, 2, 7, 87}, B = {4, 67, 2, 8}
Output: 2 7 7 2 4 1 5 8 7

Approach: Create a vector to store the result of every addition. If the addition is a single digit number then push the number in the vector else break the number into different digits and push the digits in the array one by one. Print the contents of the vector in the end.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
#include <vector>
using namespace std;
  
// Utility function to print the contents of the vector
void printVector(vector<int>& result)
{
    for (int i : result)
        cout << i << " ";
}
  
// Recursive function to separate the digits of a positive
// integer and add them to the given vector
void split_number(int num, vector<int>& result)
{
    if (num > 0) {
        split_number(num / 10, result);
        result.push_back(num % 10);
    }
}
  
// Function to add two arrays
void add(vector<int> a, vector<int> b)
{
    // Vector to store the output
    vector<int> result;
    int m = a.size(), n = b.size();
  
    // Loop till a or b runs out
    int i = 0;
    while (i < m && i < n) {
  
        // Get sum of next element from each array
        int sum = a[i] + b[i];
  
        // Separate the digits of sum and add them to 
        // the resultant vector
        split_number(sum, result);
        i++;
    }
  
    // Process remaining elements of first vector, if any
    while (i < m) {
        split_number(a[i++], result);
    }
  
    // Process remaining elements of second vector, if any
    while (i < n) {
        split_number(b[i++], result);
    }
  
    // Print the resultant vector
    printVector(result);
}
  
// Driver code
int main()
{
    // input vectors
    vector<int> a = { 23, 5, 2, 7, 87 };
    vector<int> b = { 4, 67, 2, 8 };
  
    add(a, b);
  
    return 0;
}

chevron_right


Python3

# Python3 implementation of the
# above approach



# Utility function to print the
# contents of the list
def printVector(result):

for i in result:
print(i, end = ” “)

# Recursive function to separate the
# digits of a positive integer and
# add them to the given list
def split_number(num, result):

if num > 0:
split_number(num // 10, result)
result.append(num % 10)

# Function to add two lists
def add(a, b):

# List to store the output
result = []
m, n = len(a), len(b)

# Loop till a or b runs out
i = 0
while i < m and i < n: # Get sum of next element from # each array sum = a[i] + b[i] # Separate the digits of sum and # add them to the resultant list split_number(sum, result) i += 1 # Process remaining elements of # first list, if any while i < m: split_number(a[i], result) i += 1 # Process remaining elements of # second list, if any while i < n: split_number(b[i], result) i += 1 # Print the resultant list printVector(result) # Driver Code if __name__ == "__main__": # input lists a = [23, 5, 2, 7, 87] b = [4, 67, 2, 8] add(a, b) # This code is contributed by rituraj_jain [tabbyending]

Output:

2 7 7 2 4 1 5 8 7


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : rituraj_jain