Add elements of given arrays with given constraints
Given two integer arrays, add their elements into third array by satisfying following constraints –
- Addition should be done starting from 0th index of both arrays.
- Split the sum if it is a not a single digit number and store the digits in adjacent locations in output array.
- Output array should accommodate any remaining digits of larger input array.
Examples:
Input:
a = [9, 2, 3, 7, 9, 6]
b = [3, 1, 4, 7, 8, 7, 6, 9]
Output:
[1, 2, 3, 7, 1, 4, 1, 7, 1, 3, 6, 9]
Input:
a = [9343, 2, 3, 7, 9, 6]
b = [34, 11, 4, 7, 8, 7, 6, 99]
Output:
[9, 3, 7, 7, 1, 3, 7, 1, 4, 1, 7, 1, 3, 6, 9, 9]
Input:
a = []
b = [11, 2, 3 ]
Output:
[1, 1, 2, 3 ]
Input:
a = [9, 8, 7, 6, 5, 4, 3, 2, 1]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Output:
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
Difficulty Level: Rookie
The idea is very simple. We maintain an output array and run a loop from the 0th index of both arrays. For each iteration of loop, we consider next elements in both arrays and add them. If the sum is greater than 9, we push the individual digits of the sum to output array else we push the sum itself. Finally we push the remaining elements of larger input array to output array.
Below is the implementation of above idea:
C++
#include<bits/stdc++.h>
using namespace std;
void split( int num, vector< int > &out)
{
vector< int > arr;
while (num)
{
arr.push_back(num%10);
num = num/10;
}
out.insert(out.end(), arr.rbegin(), arr.rend());
}
void addArrays( int arr1[], int arr2[], int m, int n)
{
vector< int > out;
int i = 0;
while (i < m && i < n)
{
int sum = arr1[i] + arr2[i];
if (sum < 10)
out.push_back(sum);
else
{
split(sum, out);
}
i++;
}
while (i < m)
split(arr1[i++], out);
while (i < n)
split(arr2[i++], out);
for ( int x : out)
cout << x << " " ;
}
int main()
{
int arr1[] = {9343, 2, 3, 7, 9, 6};
int arr2[] = {34, 11, 4, 7, 8, 7, 6, 99};
int m = sizeof (arr1) / sizeof (arr1[0]);
int n = sizeof (arr2) / sizeof (arr2[0]);
addArrays(arr1, arr2, m, n);
return 0;
}
|
Java
import java.util.Vector;
class GFG
{
static void split( int num, Vector<Integer> out)
{
Vector<Integer> arr = new Vector<>();
while (num > 0 )
{
arr.add(num % 10 );
num /= 10 ;
}
for ( int i = arr.size() - 1 ; i >= 0 ; i--)
out.add(arr.elementAt(i));
}
static void addArrays( int [] arr1, int [] arr2,
int m, int n)
{
Vector<Integer> out = new Vector<>();
int i = 0 ;
while (i < m && i < n)
{
int sum = arr1[i] + arr2[i];
if (sum < 10 )
out.add(sum);
else
split(sum, out);
i++;
}
while (i < m)
split(arr1[i++], out);
while (i < n)
split(arr2[i++], out);
for ( int x : out)
System.out.print(x + " " );
}
public static void main(String[] args)
{
int [] arr1 = { 9343 , 2 , 3 , 7 , 9 , 6 };
int [] arr2 = { 34 , 11 , 4 , 7 , 8 , 7 , 6 , 99 };
int m = arr1.length;
int n = arr2.length;
addArrays(arr1, arr2, m, n);
}
}
|
Python3
def split(num, out):
arr = []
while num:
arr.append(num % 10 )
num = num / / 10
for i in range ( len (arr) - 1 , - 1 , - 1 ):
out.append(arr[i])
def add_arrays(arr1, arr2, m, n):
out = []
i = 0
while i < m and i < n:
sum = arr1[i] + arr2[i]
if sum < 10 :
out.append( sum )
else :
split( sum , out)
i + = 1
while i < m:
split(arr1[i], out)
i + = 1
while i < n:
split(arr2[i], out)
i + = 1
for x in out:
print (x, end = " " )
arr1 = [ 9343 , 2 , 3 , 7 , 9 , 6 ]
arr2 = [ 34 , 11 , 4 , 7 , 8 , 7 , 6 , 99 ]
m = len (arr1)
n = len (arr2)
add_arrays(arr1, arr2, m, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void split( int num, List< int > outs)
{
List< int > arr = new List< int >();
while (num > 0)
{
arr.Add(num % 10);
num /= 10;
}
for ( int i = arr.Count - 1; i >= 0; i--)
outs.Add(arr[i]);
}
static void addArrays( int [] arr1, int [] arr2,
int m, int n)
{
List< int > outs = new List< int >();
int i = 0;
while (i < m && i < n)
{
int sum = arr1[i] + arr2[i];
if (sum < 10)
outs.Add(sum);
else
split(sum, outs);
i++;
}
while (i < m)
split(arr1[i++], outs);
while (i < n)
split(arr2[i++], outs);
foreach ( int x in outs)
Console.Write(x + " " );
}
public static void Main(String[] args)
{
int [] arr1 = { 9343, 2, 3, 7, 9, 6 };
int [] arr2 = { 34, 11, 4, 7, 8, 7, 6, 99 };
int m = arr1.Length;
int n = arr2.Length;
addArrays(arr1, arr2, m, n);
}
}
|
Javascript
<script>
function split(num, out)
{
let arr = [];
while (num)
{
arr.push(num % 10);
num = Math.floor(num / 10);
}
for (let i = arr.length - 1; i >= 0; i--)
out.push(arr[i]);
}
function addArrays(arr1, arr2, m, n)
{
let out = [];
let i = 0;
while (i < m && i < n)
{
let sum = arr1[i] + arr2[i];
if (sum < 10)
out.push(sum);
else
{
split(sum, out);
}
i++;
}
while (i < m)
split(arr1[i++], out);
while (i < n)
split(arr2[i++], out);
for (let x of out)
document.write(x + " " );
}
let arr1 = [ 9343, 2, 3, 7, 9, 6 ];
let arr2 = [ 34, 11, 4, 7, 8, 7, 6, 99 ];
let m = arr1.length;
let n = arr2.length;
addArrays(arr1, arr2, m, n);
</script>
|
Output
9 3 7 7 1 3 7 1 4 1 7 1 3 6 9 9
Time complexity of above solution is O(m + n) as we traverses both arrays exactly once.
Method 2(using String and STL):
in this approach we will take one string named “ans”. as we add the element of both arrays the resultant sum will be converted to string and this string will be appended with main string “ans”.
After doing this for all elements we will take one vector and transfer the whole string to that vector and finally will print that vector.
Below is implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void creat_new( int arr1[], int arr2[], int n, int m)
{
string ans;
int i = 0;
while (i < min(n, m)) {
int sum = arr1[i] + arr2[i];
string s = to_string(sum);
ans += s;
i++;
}
for ( int j = i; j < n; j++) {
string s = to_string(arr1[j]);
ans += s;
}
for ( int j = i; j < m; j++) {
string s = to_string(arr2[j]);
ans += s;
}
vector< int > k;
for ( int i = 0; i < ans.length(); i++) {
k.push_back(ans[i] - '0' );
}
for ( int i = 0; i < k.size(); i++) {
cout << k[i] << " " ;
}
}
int main()
{
int arr1[] = { 9, 2, 3, 7, 9, 6 };
int arr2[] = { 3, 1, 4, 7, 8, 7, 6, 9 };
int n = sizeof (arr1) / sizeof (arr1[0]);
int m = sizeof (arr2) / sizeof (arr2[0]);
creat_new(arr1, arr2, n, m);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void creat_new( int arr1[], int arr2[], int n,
int m)
{
String ans = "" ;
int i = 0 ;
while (i < Math.min(n, m))
{
int sum = arr1[i] + arr2[i];
String s = sum + "" ;
ans += s;
i++;
}
for ( int j = i; j < n; j++) {
String s = arr1[j] + "" ;
ans += s;
}
for ( int j = i; j < m; j++) {
String s = arr2[j] + "" ;
ans += s;
}
ArrayList<Integer> k = new ArrayList<>();
for ( int j = 0 ; j < ans.length(); j++) {
k.add(ans.charAt(j) - '0' );
}
for ( int j = 0 ; j < k.size(); j++) {
System.out.print(k.get(j) + " " );
}
}
public static void main(String[] args)
{
int arr1[] = { 9 , 2 , 3 , 7 , 9 , 6 };
int arr2[] = { 3 , 1 , 4 , 7 , 8 , 7 , 6 , 9 };
int n = arr1.length;
int m = arr2.length;
creat_new(arr1, arr2, n, m);
}
}
|
Python3
import math
class GFG :
@staticmethod
def creat_new( arr1, arr2, n, m) :
ans = ""
i = 0
while (i < min (n,m)) :
sum = arr1[i] + arr2[i]
s = str ( sum ) + ""
ans + = s
i + = 1
j = i
while (j < n) :
s = str (arr1[j]) + ""
ans + = s
j + = 1
j = i
while (j < m) :
s = str (arr2[j]) + ""
ans + = s
j + = 1
k = []
j = 0
while (j < len (ans)) :
k.append( ord (ans[j]) - ord ( '0' ))
j + = 1
j = 0
while (j < len (k)) :
print (k[j],end = ' ' )
j + = 1
@staticmethod
def main( args) :
arr1 = [ 9 , 2 , 3 , 7 , 9 , 6 ]
arr2 = [ 3 , 1 , 4 , 7 , 8 , 7 , 6 , 9 ]
n = len (arr1)
m = len (arr2)
GFG.creat_new(arr1, arr2, n, m)
if __name__ = = "__main__" :
GFG.main([])
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static void creat_new( int [] arr1, int [] arr2, int n, int m)
{
var ans = "" ;
var i = 0;
while (i < Math.Min(n,m))
{
var sum = arr1[i] + arr2[i];
var s = sum.ToString() + "" ;
ans += s;
i++;
}
for ( int j = i; j < n; j++)
{
var s = arr1[j].ToString() + "" ;
ans += s;
}
for ( int j = i; j < m; j++)
{
var s = arr2[j].ToString() + "" ;
ans += s;
}
var k = new List< int >();
for ( int j = 0; j < ans.Length; j++)
{
k.Add(( int )(ans[j]) - ( int )( '0' ));
}
for ( int j = 0; j < k.Count; j++)
{
Console.Write(k[j] + " " );
}
}
public static void Main(String[] args)
{
int [] arr1 = {9, 2, 3, 7, 9, 6};
int [] arr2 = {3, 1, 4, 7, 8, 7, 6, 9};
var n = arr1.Length;
var m = arr2.Length;
GFG.creat_new(arr1, arr2, n, m);
}
}
|
Javascript
const creat_new = (arr1, arr2, n, m) => {
let ans = "" ;
let i = 0;
while (i < Math.min(n, m))
{
let sum = arr1[i] + arr2[i];
let s = sum.toString();
ans += s;
i++;
}
for (let j = i; j < n; j++) {
let s = arr1[j].toString();
ans += s;
}
for (let j = i; j < m; j++) {
let s = arr2[j].toString();
ans += s;
}
let k = [];
for (let i = 0; i < ans.length; i++) {
k.push(parseInt(ans[i]));
}
console.log(k);
}
let arr1 = [9, 2, 3, 7, 9, 6];
let arr2 = [3, 1, 4, 7, 8, 7, 6, 9];
let n = arr1.length;
let m = arr2.length;
creat_new(arr1, arr2, n, m);
|
Output
1 2 3 7 1 4 1 7 1 3 6 9
Time Complexity: O(max(m,n))
Auxiliary Space:O(m+n), since vector k(of size m+n in worst case) is being created.
Last Updated :
20 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...