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++
// 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; } |
Java
// Java implementation of the above approach import java.util.*; Â
class GFG { Â
    // Utility function to print     // the contents of the vector     static void printVector(Vector<Integer> result)     {         for ( int i : result)         {             System.out.print(i + " " );         }     } Â
    // Recursive function to separate     // the digits of a positive integer     // and add them to the given vector     static void split_number( int num, Vector<Integer> result)     {         if (num > 0 )         {             split_number(num / 10 , result);             result.add(num % 10 );         }     } Â
    // Function to add two arrays     static void add(Vector<Integer> a, Vector<Integer> b)     {         // Vector to store the output         Vector<Integer> result = new Vector<Integer>();         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.get(i) + b.get(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.get(i++), result);         } Â
        // Process remaining elements         // of second vector, if any         while (i < n)         {             split_number(b.get(i++), result);         } Â
        // Print the resultant vector         printVector(result);     } Â
    // Driver code     public static void main(String[] args)     {         // input vectors         int [] arr1 = { 23 , 5 , 2 , 7 , 87 };         Vector<Integer> a = new Vector<>();         for (Integer i:arr1)             a.add(i);                      int [] arr2 = { 4 , 67 , 2 , 8 };         Vector<Integer> b = new Vector<Integer>();         for (Integer i:arr2)             b.add(i); Â
        add(a, b);     } } Â
// This code is contributed by 29AjayKumar |
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 |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; Â
class GFG { Â
    // Utility function to print     // the contents of the vector     static void printVector(List< int > result)     {         foreach ( int i in result)         {             Console.Write(i + " " );         }     } Â
    // Recursive function to separate     // the digits of a positive integer     // and add them to the given vector     static void split_number( int num, List< int > result)     {         if (num > 0)         {             split_number(num / 10, result);             result.Add(num % 10);         }     } Â
    // Function to add two arrays     static void add(List< int > a, List< int > b)     {         // Vector to store the output         List< int > result = new List< int >();         int m = a.Count, n = b.Count; Â
        // 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     public static void Main(String[] args)     {         // input vectors         int [] arr1 = {23, 5, 2, 7, 87};         List< int > a = new List< int >();         foreach ( int i in arr1)             a.Add(i);                      int [] arr2 = {4, 67, 2, 8};         List< int > b = new List< int >();         foreach ( int i in arr2)             b.Add(i); Â
        add(a, b);     } } Â
// This code is contributed by princiraj1992 |
Javascript
<script> // Javascript implementation of the approach Â
// Utility function to print the contents of the vector function printVector(result) { Â Â Â Â for (let i = 0; i < result.length; i++) Â Â Â Â Â Â Â Â document.write(result[i] + " " ); } Â
// Recursive function to separate the digits of a positive // integer and add them to the given vector function split_number(num, result) { Â Â Â Â if (num > 0) { Â Â Â Â Â Â Â Â split_number(parseInt(num / 10), result); Â Â Â Â Â Â Â Â result.push(num % 10); Â Â Â Â } } Â
// Function to add two arrays function add(a, b) {     // Vector to store the output     let result = [];     let m = a.length, n = b.length; Â
    // Loop till a or b runs out     let i = 0;     while (i < m && i < n) { Â
        // Get sum of next element from each array         let 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 // input vectors let a = [ 23, 5, 2, 7, 87 ]; let b = [ 4, 67, 2, 8 ]; Â
add(a, b); Â
// This code is contributed by souravmahato348. </script> |
2 7 7 2 4 1 5 8 7
Time Complexity : O(max(m,n))
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!