Digits of element wise sum of two arrays into a new array
Last Updated :
12 Sep, 2022
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++
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector< int >& result)
{
for ( int i : result)
cout << i << " " ;
}
void split_number( int num, vector< int >& result)
{
if (num > 0) {
split_number(num / 10, result);
result.push_back(num % 10);
}
}
void add(vector< int > a, vector< int > b)
{
vector< int > result;
int m = a.size(), n = b.size();
int i = 0;
while (i < m && i < n) {
int sum = a[i] + b[i];
split_number(sum, result);
i++;
}
while (i < m) {
split_number(a[i++], result);
}
while (i < n) {
split_number(b[i++], result);
}
printVector(result);
}
int main()
{
vector< int > a = { 23, 5, 2, 7, 87 };
vector< int > b = { 4, 67, 2, 8 };
add(a, b);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void printVector(Vector<Integer> result)
{
for ( int i : result)
{
System.out.print(i + " " );
}
}
static void split_number( int num, Vector<Integer> result)
{
if (num > 0 )
{
split_number(num / 10 , result);
result.add(num % 10 );
}
}
static void add(Vector<Integer> a, Vector<Integer> b)
{
Vector<Integer> result = new Vector<Integer>();
int m = a.size(), n = b.size();
int i = 0 ;
while (i < m && i < n)
{
int sum = a.get(i) + b.get(i);
split_number(sum, result);
i++;
}
while (i < m)
{
split_number(a.get(i++), result);
}
while (i < n)
{
split_number(b.get(i++), result);
}
printVector(result);
}
public static void main(String[] args)
{
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);
}
}
|
Python3
def printVector(result):
for i in result:
print (i, end = " " )
def split_number(num, result):
if num > 0 :
split_number(num / / 10 , result)
result.append(num % 10 )
def add(a, b):
result = []
m, n = len (a), len (b)
i = 0
while i < m and i < n:
sum = a[i] + b[i]
split_number( sum , result)
i + = 1
while i < m:
split_number(a[i], result)
i + = 1
while i < n:
split_number(b[i], result)
i + = 1
printVector(result)
if __name__ = = "__main__" :
a = [ 23 , 5 , 2 , 7 , 87 ]
b = [ 4 , 67 , 2 , 8 ]
add(a, b)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void printVector(List< int > result)
{
foreach ( int i in result)
{
Console.Write(i + " " );
}
}
static void split_number( int num, List< int > result)
{
if (num > 0)
{
split_number(num / 10, result);
result.Add(num % 10);
}
}
static void add(List< int > a, List< int > b)
{
List< int > result = new List< int >();
int m = a.Count, n = b.Count;
int i = 0;
while (i < m && i < n)
{
int sum = a[i] + b[i];
split_number(sum, result);
i++;
}
while (i < m)
{
split_number(a[i++], result);
}
while (i < n)
{
split_number(b[i++], result);
}
printVector(result);
}
public static void Main(String[] args)
{
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);
}
}
|
Javascript
<script>
function printVector(result)
{
for (let i = 0; i < result.length; i++)
document.write(result[i] + " " );
}
function split_number(num, result)
{
if (num > 0) {
split_number(parseInt(num / 10), result);
result.push(num % 10);
}
}
function add(a, b)
{
let result = [];
let m = a.length, n = b.length;
let i = 0;
while (i < m && i < n) {
let sum = a[i] + b[i];
split_number(sum, result);
i++;
}
while (i < m) {
split_number(a[i++], result);
}
while (i < n) {
split_number(b[i++], result);
}
printVector(result);
}
let a = [ 23, 5, 2, 7, 87 ];
let b = [ 4, 67, 2, 8 ];
add(a, b);
</script>
|
Time Complexity : O(max(m,n))
Share your thoughts in the comments
Please Login to comment...