Amazon Interview Question
Quality Assurance EngineersTeam: amazon.in
Country: India
Interview Type: In-Person
public class AddArray
{
public static void main(String[] args) {
int arr1[] = {1,5,8,3};
int arr2[] = {4,4,7,9};
int arr3[] = new int[11];
int k = 0;
for (int i = 0; i < arr1.length; i++) {
int sum = arr1[i] +arr2[i];
int a =sum;
if(a>9)
{
String s = Integer.toString(a);
System.out.println(s);
for (int j = 0; j < s.length(); j++) {
arr3[k] = Integer.parseInt(s.substring(j, j+1));
System.out.println(arr3[k]);
k++;
}
}
else
{
arr3[k] = sum;
System.out.println("runing else");
System.out.println(arr3[k]);
k++;
}
}
System.out.println("final output");
for (int i = 0; i <k; i++) {
System.out.print("{" +arr3[i]+ "}");
}
}
}
var a = [1,2,3,4,5,6];
var b = [2,3,4,5,6,7,8];
print_sum(a,b);
function print_sum(a,b){
var c = [];
var i=0;
var j=0;
while (i<a.length && j<b.length){
var sum = a[i] + b[j];
var su = sum.toString();
if(su < 10){
c.push(sum);
}
else{
var str = su.split('');
c.push(parseInt(str[0]));
c.push(parseInt(str[1]));
}
i++;
j++;
}
while(i < a.length){
c.push(a[i]);
i++;
}
while(j < b.length){
c.push(b[j]);
j++;
}
console.log(c);
}
#include<iostream>
using namespace std;
int main()
{
int a[6]={1,2,3,4,5,6}, b[7]={2,3,4,5,6,7,8}, c[10], i=0, j=0, x,y;
int asize = sizeof(a)/sizeof(int);
int bsize = sizeof(b)/sizeof(int);
while(i < asize)
{
x = a[i] + b[i];
if(x<10) c[j]=x;
else
{
y = x % 10;
x = x / 10;
c[j] = x;
j++;
c[j] = y;
}
i++;
j++;
if(i >= asize)
{
for(int n=i; n<bsize; n++)
{
c[j] = b[i]; j++;
}
}
}
cout<<"array c: ";
for (i=0; i<j; i++)
{
cout<<" "<<c[i];
}
cout<<"\n";
return 0;
}
int main()
{
int a[6]={1,2,3,4,5,6}, b[7]={2,3,4,5,6,7,8}, c[10], i=0, j=0, x,y;
int asize = sizeof(a)/sizeof(int);
int bsize = sizeof(b)/sizeof(int);
while(i < asize)
{
x = a[i] + b[i];
if(x<10) c[j]=x;
else
{
y = x % 10;
x = x / 10;
c[j] = x;
j++;
c[j] = y;
}
i++;
j++;
if(i >= asize)
{
for(int n=i; n<bsize; n++)
{
c[j] = b[i]; j++;
}
}
}
cout<<"array c: ";
for (i=0; i<j; i++)
{
cout<<" "<<c[i];
}
cout<<"\n";
return 0;
}
public static int[] findSum(int []a, int []b){
int n = findLength(a, b);
int [] c = new int[n];
int l1 = a.length;
int l2 = b.length;
int l = l1 > l2 ? l2:l1;
int j =0;
for(int i =0 ; i < l; i++ ){
int x = a[i] + b[i];
if(x/10 > 0){
c[j++] = x/10;
c[j++] =x % 10;
}
else{
c[j++] = x;
}
}
if(l1 > l2){
for(int i=l; i< l1; i++){
c[j++] = a[i];
}
}
else{
for(int i=l; i< l2; i++){
c[j++] = b[i];
}
}
return c;
}
public static int findLength(int[] a, int[] b){
int count =0;
int l1 = a.length;
int l2 = b.length;
int l = l1 > l2 ? l2:l1;
int d = l1 > l2 ? l1 - l2: l2 - l1;
for(int i=0; i< l; i++){
int x =a[i]+b[i];
count++;
if(x/10 > 0){
count++;
}
}
return count+d;
}
#include<iostream>
using namespace std;
int main()
{
int a[6]={1,2,3,4,5,6}, b[7]={2,3,4,5,6,7,8}, c[10], i=0, j=0, x,y;
int asize = sizeof(a)/sizeof(int);
int bsize = sizeof(b)/sizeof(int);
while(i < asize)
{
x = a[i] + b[i];
if(x<10) c[j]=x;
else
{
y = x % 10;
x = x / 10;
c[j] = x;
j++;
c[j] = y;
}
i++;
j++;
if(i >= asize)
{
for(int n=i; n<bsize; n++)
{
c[j] = b[i]; j++;
}
}
}
cout<<"array c: ";
for (i=0; i<j; i++)
{
cout<<" "<<c[i];
}
cout<<"\n";
return 0;
}
public class AddAllDigitstoGetSingleDigit {
public int addDigits(int num) {
int sum = 0;
while (num > 0) {
int rem = num % 10; // the remainder (or last digit of the number)
// will be added to sum
sum = sum + rem;
num = num / 10; // the quotient will be the next number;
if(num==0 && sum > 9){
num = sum;
sum=0;
}
}
return sum;
}
public class AddAllDigitstoGetSingleDigit {
public int addDigits(int num) {
int sum = 0;
while (num > 0) {
int rem = num % 10; // the remainder (or last digit of the number)
// will be added to sum
sum = sum + rem;
num = num / 10; // the quotient will be the next number;
if(num==0 && sum > 9){
num = sum;
sum=0;
}
}
return sum;
}
vector<int> add(int a[], int m, int b[], int n)
{
vector<int> ans;
int k = min(m, n);
for (int i = 0; i < k; i++) {
int s = a[i] + b[i];
if (s > 9) {
ans.push_back(1);
ans.push_back(s - 10);
} else {
ans.push_back(s);
}
}
// copy the rest to answer
if (m > n) copy_n(a+n, m-n, ans.begin() + n);
else copy_n(b+m, n-m, ans.begin() + m);
return ans;
}
int a[] = {2,3,4,5,6,7,8};
int b[] = {1,2,3,4,5,6};
int Na=sizeof(a)/sizeof(int);
int Nb=sizeof(b)/sizeof(int);
int k=0;
int *c=new int[2*Na];
for(int i=0; i<Na; i++)
{
if(i>=Nb)
{
c[i+k]=a[i];
break;
}
if((a[i]+b[i])<10){
c[i] = a[i]+b[i];
}
else
{
c[i+k]=((a[i]+b[i])-(a[i]+b[i])%10)/10;
c[i+1+k]=(a[i]+b[i])%10;
k++;
}
}
for(int i=0; i<Na+k; i++)
{
cout<<c[i]<<' '<<endl;
}
function addArray(nums1,nums2) {
var nums3 =[];
var i =0,j=0;
while(true){
var op1 = 0,op2 =0 ;
var arr1Done = false,arr2Done = false;
if (i<nums1.length ) {
op1 = nums1[i++];
}else{
arr1Done = true;
}
if (j<nums2.length) {
op2 = nums2[j++];
}else{
arr2Done = true;
}
if(arr1Done == true && arr2Done == true){
break;
}else{
var res = op1+op2;
if (res>10) {
res = res+'';
nums3.push(parseInt(res.split('')[0]));
nums3.push(parseInt(res.split('')[1]));
}else{
nums3.push(res);
}
}
}
return nums3;
}
import java.util.ArrayList;
public class AddIntArrays {
int[] a1 = {1,2,3,4,5,6};
int[] a2 = {2,3,4,5,6,7,8};
static ArrayList<Integer> addArrays(int[] a1, int[] a2) {
ArrayList <Integer> a3 = new ArrayList<Integer>();
int i = 0, j = 0, temp;
while (i < a1.length && j < a2.length) {
temp = a1[i] + a2[i];
if (temp < 10){
a3.add(temp);
System.out.println("temp:" + temp);
} else {
String s = Integer.toString(temp);
System.out.println("String:" + s);
for (int l = 0; l < s.length(); l++) {
a3.add(Integer.parseInt(s.substring(l, l+1)));
}
}
i++;
j++;
}
while (i < a1.length){
a3.add(a1[i]);
i++;
}
while (j < a2.length) {
a3.add(a2[j]);
j++;
}
return a3;
}
public static void main(String[] args) {
int[] a1 = {1,2,3,5};
int[] a2 = {2,3,4,5,6,7,8};
ArrayList<Integer> a3 = addArrays(a1, a2);
System.out.println(a3);
}
}
import java.util.ArrayList;
public class AddIntArrays {
int[] a1 = {1,2,3,4,5,6};
int[] a2 = {2,3,4,5,6,7,8};
static ArrayList<Integer> addArrays(int[] a1, int[] a2) {
ArrayList <Integer> a3 = new ArrayList<Integer>();
int i = 0, j = 0, temp;
while (i < a1.length && j < a2.length) {
temp = a1[i] + a2[i];
if (temp < 10){
a3.add(temp);
System.out.println("temp:" + temp);
} else {
String s = Integer.toString(temp);
System.out.println("String:" + s);
for (int l = 0; l < s.length(); l++) {
a3.add(Integer.parseInt(s.substring(l, l+1)));
}
}
i++;
j++;
}
while (i < a1.length){
a3.add(a1[i]);
i++;
}
while (j < a2.length) {
a3.add(a2[j]);
j++;
}
return a3;
}
public static void main(String[] args) {
int[] a1 = {1,2,3,5};
int[] a2 = {2,3,4,5,6,7,8};
ArrayList<Integer> a3 = addArrays(a1, a2);
System.out.println(a3);
}
}
package practice.DataStructures;
import java.util.ArrayList;
public class af {
public static void main(String[] args) {
int[] A = { 1, 2, 3, 4, 5, 6 };
int[] B = { 2, 3, 4, 5, 6, 7, 8 };
ArrayList<Integer> C = new ArrayList<Integer>();
int sum = 0;
int first, second;
for (int i = 0; i < A.length || i < B.length; i++) {
try {
while (i >= A.length) {
C.add(B[i]);
i++;
}
while (i >= B.length) {
C.add(A[i]);
i++;
}
sum = A[i] + B[i];
if (sum < 10)
C.add(sum);
else {
first = sum / 10;
C.add(first);
second = sum % 10;
C.add(second);
C.remove(sum);
}
} catch (Exception e) {
e.getMessage();
}
}
System.out.println(C);
}
}
package practice.DataStructures;
import java.util.ArrayList;
public class af {
public static void main(String[] args) {
int[] A = { 1, 2, 3, 4, 5, 6 };
int[] B = { 2, 3, 4, 5, 6, 7, 8 };
ArrayList<Integer> C = new ArrayList<Integer>();
int sum = 0;
int first, second;
for (int i = 0; i < A.length || i < B.length; i++) {
try {
while (i >= A.length) {
C.add(B[i]);
i++;
}
while (i >= B.length) {
C.add(A[i]);
i++;
}
sum = A[i] + B[i];
if (sum < 10)
C.add(sum);
else {
first = sum / 10;
C.add(first);
second = sum % 10;
C.add(second);
C.remove(sum);
}
} catch (Exception e) {
e.getMessage();
}
}
System.out.println(C);
}
}
package practice.DataStructures;
import java.util.ArrayList;
public class af {
public static void main(String[] args) {
int[] A = { 1, 2, 3, 4, 5, 6 };
int[] B = { 2, 3, 4, 5, 6, 7, 8 };
ArrayList<Integer> C = new ArrayList<Integer>();
int sum = 0;
int first, second;
for (int i = 0; i < A.length || i < B.length; i++) {
try {
while (i >= A.length) {
C.add(B[i]);
i++;
}
while (i >= B.length) {
C.add(A[i]);
i++;
}
sum = A[i] + B[i];
if (sum < 10)
C.add(sum);
else {
first = sum / 10;
C.add(first);
second = sum % 10;
C.add(second);
C.remove(sum);
}
} catch (Exception e) {
e.getMessage();
}
}
System.out.println(C);
}
}
package practice.DataStructures;
import java.util.ArrayList;
public class af {
public static void main(String[] args) {
int[] A = { 1, 2, 3, 4, 5, 6 };
int[] B = { 2, 3, 4, 5, 6, 7, 8 };
ArrayList<Integer> C = new ArrayList<Integer>();
int sum = 0;
int first, second;
for (int i = 0; i < A.length || i < B.length; i++) {
try {
while (i >= A.length) {
C.add(B[i]);
i++;
}
while (i >= B.length) {
C.add(A[i]);
i++;
}
sum = A[i] + B[i];
if (sum < 10)
C.add(sum);
else {
first = sum / 10;
C.add(first);
second = sum % 10;
C.add(second);
C.remove(sum);
}
} catch (Exception e) {
e.getMessage();
}
}
System.out.println(C);
}
}
and
package practice.DataStructures;
import java.util.ArrayList;
public class af {
public static void main(String[] args) {
int[] A = { 1, 2, 3, 4, 5, 6 };
int[] B = { 2, 3, 4, 5, 6, 7, 8};
ArrayList<Integer> C = new ArrayList<Integer>();
int sum = 0;
int first, second;
for (int i = 0; i < A.length || i < B.length; i++) {
try {
while (i >= A.length) {
C.add(B[i]);
i++;
}
while (i >= B.length) {
C.add(A[i]);
i++;
}
sum = A[i] + B[i];
if (sum < 10)
C.add(sum);
else {
first = sum / 10;
C.add(first);
second = sum % 10;
C.add(second);
C.remove(sum);
}
} catch (Exception e) {
e.getMessage();
}
}
System.out.println(C);
}
}
package practice.DataStructures;
import java.util.ArrayList;
public class af {
public static void main(String[] args) {
int[] A = { 1, 2, 3, 4, 5, 6 };
int[] B = { 2, 3, 4, 5, 6, 7, 8};
ArrayList<Integer> C = new ArrayList<Integer>();
int sum = 0;
int first, second;
for (int i = 0; i < A.length || i < B.length; i++) {
try {
while (i >= A.length) {
C.add(B[i]);
i++;
}
while (i >= B.length) {
C.add(A[i]);
i++;
}
sum = A[i] + B[i];
if (sum < 10)
C.add(sum);
else {
first = sum / 10;
C.add(first);
second = sum % 10;
C.add(second);
C.remove(sum);
}
} catch (Exception e) {
e.getMessage();
}
}
System.out.println(C);
}
}
package practice.DataStructures;
import java.util.ArrayList;
public class af {
public static void main(String[] args) {
int[] A = { 1, 2, 3, 4, 5, 6 };
int[] B = { 2, 3, 4, 5, 6, 7, 8};
ArrayList<Integer> C = new ArrayList<Integer>();
int sum = 0;
int first, second;
for (int i = 0; i < A.length || i < B.length; i++) {
try {
while (i >= A.length) {
C.add(B[i]);
i++;
}
while (i >= B.length) {
C.add(A[i]);
i++;
}
sum = A[i] + B[i];
if (sum < 10)
C.add(sum);
else {
first = sum / 10;
C.add(first);
second = sum % 10;
C.add(second);
C.remove(sum);
}
} catch (Exception e) {
e.getMessage();
}
}
System.out.println(C);
}
}
<?php
function addArray($arr1 , $arr2) {
$arr1_length = count($arr1); // length of array 1
$arr2_length = count($arr2); // length of array 2
$arrr_sum = array();
if($arr2_length >= $arr1_length){
$bigArr = $arr2;
$smallArr = $arr1;
$bigArrLength= $arr2_length;
$smallArrLen = $arr1_length;
} else {
$bigArr = $arr1;
$smallArr = $arr2;
$bigArrLength= $arr1_length;
$smallArrLen = $arr2_length;
}
for($i=0; $i < $bigArrLength ; $i++) {
if($i > $smallArrLen){
$sum = $bigArr[$i];
} else {
$sum = $smallArr[$i]+$bigArr[$i];
}
if($sum > 9){
$sumArr = str_split($sum);
foreach ($sumArr as $value) {
array_push($arrr_sum, $value);
}
}else {
array_push($arrr_sum, $sum);
}
}
return $arrr_sum;
}
$temp1 = array(1,2,3,4,5,6);//any integer array 1
$temp2 = array(2,3,4,5,6,7,8,9,6,5);//any integer array 2
$resultSum = addArray($temp2,$temp1); // calling the addArray function
print_r($resultSum); //printing the output Sum array
?>
public class AddArray {
public static void main(String[] args) {
int arry1[] = {1, 5, 3, 8};
int arry2[] = {3, 2, 4, 7};
List arrayList=new ArrayList<>();
int a = Math.min(arry1.length, arry2.length);
for (int i = 0; i <= a - 1; i++) {
int sum = arry1[i] + arry2[i];
if (sum > 10) {
int b = 1;
arrayList.add(b);
sum = sum - 10;
arrayList.add(sum);
} else if (sum == 10) {
arrayList.add(1);
arrayList.add(0);
} else {
arrayList.add(sum);
}
}
if (arry2.length > arry1.length) {
for (int j = arry1.length + 1; j < arry1.length - arry2.length; j++) {
int b = arry2[j];
arrayList.add(b);
}
} else if (arry1.length > arry2.length) {
for (int j = arry2.length + 1; j < arry2.length - arry1.length; j++) {
int b= arry2[j];
arrayList.add(b);
}
}
System.out.print(arrayList);
}
}
public class AddArray {
public static void main(String[] args) {
int arry1[] = {1, 5, 3, 8};
int arry2[] = {3, 2, 4, 7};
List arrayList=new ArrayList<>();
int a = Math.min(arry1.length, arry2.length);
for (int i = 0; i <= a - 1; i++) {
int sum = arry1[i] + arry2[i];
if (sum > 10) {
int b = 1;
arrayList.add(b);
sum = sum - 10;
arrayList.add(sum);
} else if (sum == 10) {
arrayList.add(1);
arrayList.add(0);
} else {
arrayList.add(sum);
}
}
if (arry2.length > arry1.length) {
for (int j = arry1.length + 1; j < arry1.length - arry2.length; j++) {
int b = arry2[j];
arrayList.add(b);
}
} else if (arry1.length > arry2.length) {
for (int j = arry2.length + 1; j < arry2.length - arry1.length; j++) {
int b= arry2[j];
arrayList.add(b);
}
}
System.out.print(arrayList);
}
}
static int[] a = {1,2,3,4,5,6} ;
static int[] b = {2,3,4,5,6,7,8} ;
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] mergedArray;
if (a.length > b.length) {
mergedArray = merge(a, b);
} else {
mergedArray = merge(b, a);
}
System.out.println(Arrays.toString(mergedArray));
}
private static int[] merge(int[] a, int[] b) {
// TODO Auto-generated method stub
//a is bigger/equal to b
int arraySize = b.length, tempSum = 0;
for (int i = 0; i < b.length; i++) {
if (a[i] + b[i] >= 10) {
arraySize++;
}
}
arraySize += a.length - b.length;
int[] aReturnArray = new int[arraySize];
// this loop will run at max for the biggest array's length
for (int i = 0, k = 0; i < a.length && k <arraySize; i++) {
if (i > b.length - 1) {
aReturnArray[k] = a[i];
} else {
tempSum = a[i] + b[i];
if (tempSum >= 10) {
aReturnArray[k] = tempSum/10;
aReturnArray[k+1] = tempSum%10;
k = k+ 2;
} else {
aReturnArray[k] = tempSum;
k++;
}
}
}
return aReturnArray;
}
static int[] a = {1,2,3,4,5,6} ;
static int[] b = {2,3,4,5,6,7,8} ;
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] mergedArray;
if (a.length > b.length) {
mergedArray = merge(a, b);
} else {
mergedArray = merge(b, a);
}
System.out.println(Arrays.toString(mergedArray));
}
private static int[] merge(int[] a, int[] b) {
// TODO Auto-generated method stub
//a is bigger/equal to b
int arraySize = b.length, tempSum = 0;
for (int i = 0; i < b.length; i++) {
if (a[i] + b[i] >= 10) {
arraySize++;
}
}
arraySize += a.length - b.length;
int[] aReturnArray = new int[arraySize];
// this loop will run at max for the biggest array's length
for (int i = 0, k = 0; i < a.length && k <arraySize; i++) {
if (i > b.length - 1) {
aReturnArray[k] = a[i];
} else {
tempSum = a[i] + b[i];
if (tempSum >= 10) {
aReturnArray[k] = tempSum/10;
aReturnArray[k+1] = tempSum%10;
k = k+ 2;
} else {
aReturnArray[k] = tempSum;
k++;
}
}
}
return aReturnArray;
}
public static void main(String[] arg)
{
int[] a={1,2,3,4,5,6};
int[] b={2,3,4,5,6,7,8};
int alength=a.length;
int blength=b.length;
int i=alength>=blength?blength:alength;
ArrayList<Integer> al=new ArrayList<Integer>();
for(int x=0;x<i;x++)
{ int c=a[x]+b[x];
if(c>9)
{
al.add(c/10);
al.add(c%10);
}
else
{
al.add(c);
}
}
for(int x=i;x<(alength+blength-i);x++)
{ if(alength>blength)
al.add(a[x]);
else
al.add(b[x]);
}
System.out.println(al);
}
//this code is for n no. of array size & input from user
#include <iostream>
using namespace std;
int main()
{
int a[10];
int b[10],c[10];
int i,n,n1,j;
cout<<"enter size of array a";
cin>>n;
cout<<"enter array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter size of array b";
cin>>n1;
cout<<"enter array";
for(i=0;i<n1;i++)
{
cin>>b[i];
}
if(n1>n)
{
for(i=0,j=0;i<n;i++,j++)
{
int x=a[i]+b[i];
if(x>9)
{ int t=x/10;
int m=x%10;
c[j]=t;
j++;
c[j]=m;
}
else
{
c[j]=x;
}
}
}
for(i=n;i<n1;i++)
{
c[j]=b[i];
j++;
}
cout<<"output array is";
for(i=0;i<j;i++)
{
cout<<c[i]<<"\n";
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a[10];
int b[10],c[10];
int i,n,n1,j;
cout<<"enter size of array a";
cin>>n;
cout<<"enter array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter size of array b";
cin>>n1;
cout<<"enter array";
for(i=0;i<n1;i++)
{
cin>>b[i];
}
if(n1>n)
{
for(i=0,j=0;i<n;i++,j++)
{
int x=a[i]+b[i];
if(x>9)
{ int t=x/10;
int m=x%10;
c[j]=t;
j++;
c[j]=m;
}
else
{
c[j]=x;
}
}
}
for(i=n;i<n1;i++)
{
c[j]=b[i];
j++;
}
cout<<"output array is";
for(i=0;i<j;i++)
{
cout<<c[i]<<"\n";
}
return 0;
}
{
{
{
#include <iostream>
using namespace std;
int main()
{
int a[10];
int b[10],c[10];
int i,n,n1,j;
cout<<"enter size of array a";
cin>>n;
cout<<"enter array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter size of array b";
cin>>n1;
cout<<"enter array";
for(i=0;i<n1;i++)
{
cin>>b[i];
}
if(n1>n)
{
for(i=0,j=0;i<n;i++,j++)
{
int x=a[i]+b[i];
if(x>9)
{ int t=x/10;
int m=x%10;
c[j]=t;
j++;
c[j]=m;
}
else
{
c[j]=x;
}
}
}
for(i=n;i<n1;i++)
{
c[j]=b[i];
j++;
}
cout<<"output array is";
for(i=0;i<j;i++)
{
cout<<c[i]<<"\n";
}
return 0;
}
}
}
{
{
{
#include <iostream>
using namespace std;
int main()
{
int a[10];
int b[10],c[10];
int i,n,n1,j;
cout<<"enter size of array a";
cin>>n;
cout<<"enter array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter size of array b";
cin>>n1;
cout<<"enter array";
for(i=0;i<n1;i++)
{
cin>>b[i];
}
if(n1>n)
{
for(i=0,j=0;i<n;i++,j++)
{
int x=a[i]+b[i];
if(x>9)
{ int t=x/10;
int m=x%10;
c[j]=t;
j++;
c[j]=m;
}
else
{
c[j]=x;
}
}
}
for(i=n;i<n1;i++)
{
c[j]=b[i];
j++;
}
cout<<"output array is";
for(i=0;i<j;i++)
{
cout<<c[i]<<"\n";
}
return 0;
}
}
}
}
{
{
#include <iostream>
using namespace std;
int main()
{
int a[10];
int b[10],c[10];
int i,n,n1,j;
cout<<"enter size of array a";
cin>>n;
cout<<"enter array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter size of array b";
cin>>n1;
cout<<"enter array";
for(i=0;i<n1;i++)
{
cin>>b[i];
}
if(n1>n)
{
for(i=0,j=0;i<n;i++,j++)
{
int x=a[i]+b[i];
if(x>9)
{ int t=x/10;
int m=x%10;
c[j]=t;
j++;
c[j]=m;
}
else
{
c[j]=x;
}
}
}
for(i=n;i<n1;i++)
{
c[j]=b[i];
j++;
}
cout<<"output array is";
for(i=0;i<j;i++)
{
cout<<c[i]<<"\n";
}
return 0;
}
}
#include <iostream>
#include <stack>
using namespace std;
std::stack <int> myStack;
int main()
{
int a[] = {1,2,3,4,5,6};
int b[] = {2,3,4,5,6,7,8};
int aSize = sizeof (a)/sizeof (a[0]);
int bSize = sizeof (b)/sizeof (b[0]);
int i =0, j=0,k=0;
int c[20];
while(i <= aSize-1 && j <= bSize-1)
{
int num =0;
num = a[i] + b[j];
int temp = num;
while(temp)
{
auto remain = temp%10;
temp = temp /10;
myStack.push(remain);
}
while(!myStack.empty())
{
c[k] = myStack.top();
myStack.pop();
k++;
}
i++;
j++;
}
while(j <= bSize -1)
{
c[k] = b[j];
j++;
k++;
}
for(int i = 0;i<k; i++)
{
cout << c[i] << " ";
}
}
#include <iostream>
#include <stack>
using namespace std;
std::stack <int> myStack;
int main()
{
int a[] = {1,2,3,4,5,6};
int b[] = {2,3,4,5,6,7,8};
int aSize = sizeof (a)/sizeof (a[0]);
int bSize = sizeof (b)/sizeof (b[0]);
int i =0, j=0,k=0;
int c[20];
while(i <= aSize-1 && j <= bSize-1)
{
int num =0;
num = a[i] + b[j];
int temp = num;
while(temp)
{
auto remain = temp%10;
temp = temp /10;
myStack.push(remain);
}
while(!myStack.empty())
{
c[k] = myStack.top();
myStack.pop();
k++;
}
i++;
j++;
}
while(j <= bSize -1)
{
c[k] = b[j];
j++;
k++;
}
for(int i = 0;i<k; i++)
{
cout << c[i] << " ";
}
}
#include <iostream>
#include <stack>
using namespace std;
std::stack <int> myStack;
int main()
{
int a[] = {1,2,3,4,5,6};
int b[] = {2,3,4,5,6,7,8};
int aSize = sizeof (a)/sizeof (a[0]);
int bSize = sizeof (b)/sizeof (b[0]);
int i =0, j=0,k=0;
int c[20];
while(i <= aSize-1 && j <= bSize-1)
{
int num =0;
num = a[i] + b[j];
int temp = num;
while(temp)
{
auto remain = temp%10;
temp = temp /10;
myStack.push(remain);
}
while(!myStack.empty())
{
c[k] = myStack.top();
myStack.pop();
k++;
}
i++;
j++;
}
while(j <= bSize -1)
{
c[k] = b[j];
j++;
k++;
}
for(int i = 0;i<k; i++)
{
cout << c[i] << " ";
}
}
#include <iostream>
#include <stack>
using namespace std;
std::stack <int> myStack;
int main()
{
int a[] = {1,2,3,4,5,6};
int b[] = {2,3,4,5,6,7,8};
int aSize = sizeof (a)/sizeof (a[0]);
int bSize = sizeof (b)/sizeof (b[0]);
int i =0, j=0,k=0;
int c[20];
while(i <= aSize-1 && j <= bSize-1)
{
int num =0;
num = a[i] + b[j];
int temp = num;
while(temp)
{
auto remain = temp%10;
temp = temp /10;
myStack.push(remain);
}
while(!myStack.empty())
{
c[k] = myStack.top();
myStack.pop();
k++;
}
i++;
j++;
}
while(j <= bSize -1)
{
c[k] = b[j];
j++;
k++;
}
for(int i = 0;i<k; i++)
{
cout << c[i] << " ";
}
}
public class SumOfTwoArrays {
public static void main(String[] args)
{
int[] a = {1,2,3,4,5,6} ;
int[] b = {2,3,4,5,6,7,8} ;
int lena = a.length;
int lenb = b.length;
List<Integer> finalList = new ArrayList<Integer>();
int iterator = lena>lenb?lenb:lena;
for(int i=0; i<iterator; i++)
{
int sum = a[i]+ b[i];
String num = String.valueOf(sum);
for (int j=0; j<num.length();j++)
{
finalList.add(num.charAt(j)-'0');
}
/*while(sum/10 >0)
{
finalList.add(sum%10);
sum = sum/10;
}
finalList.add(sum);*/
}
if(iterator>lena)
{
addRemaing(a,iterator,finalList);
}
else
{
addRemaing(b,iterator,finalList);
}
Integer [] finalArray = new Integer [finalList.size()];
finalList.toArray(finalArray);
for(int i = 0; i<finalArray.length; i++)
{
System.out.print(" "+finalArray[i]);
}
}
public static void addRemaing(int maxSizeArray [], int length, List<Integer> finalList)
{
for(int i=length; i<maxSizeArray.length; i++)
{
finalList.add(maxSizeArray[i]);
}
}
}
public class SumOfTwoArrays {
public static void main(String[] args)
{
int[] a = {1,2,3,4,5,6} ;
int[] b = {2,3,4,5,6,7,8} ;
int lena = a.length;
int lenb = b.length;
List<Integer> finalList = new ArrayList<Integer>();
int iterator = lena>lenb?lenb:lena;
for(int i=0; i<iterator; i++)
{
int sum = a[i]+ b[i];
String num = String.valueOf(sum);
for (int j=0; j<num.length();j++)
{
finalList.add(num.charAt(j)-'0');
}
/*while(sum/10 >0)
{
finalList.add(sum%10);
sum = sum/10;
}
finalList.add(sum);*/
}
if(iterator>lena)
{
addRemaing(a,iterator,finalList);
}
else
{
addRemaing(b,iterator,finalList);
}
Integer [] finalArray = new Integer [finalList.size()];
finalList.toArray(finalArray);
for(int i = 0; i<finalArray.length; i++)
{
System.out.print(" "+finalArray[i]);
}
}
public static void addRemaing(int maxSizeArray [], int length, List<Integer> finalList)
{
for(int i=length; i<maxSizeArray.length; i++)
{
finalList.add(maxSizeArray[i]);
}
}
}
public class SumOfTwoArrays {
public static void main(String[] args)
{
int[] a = {1,2,3,4,5,6} ;
int[] b = {2,3,4,5,6,7,8} ;
int lena = a.length;
int lenb = b.length;
List<Integer> finalList = new ArrayList<Integer>();
int iterator = lena>lenb?lenb:lena;
for(int i=0; i<iterator; i++)
{
int sum = a[i]+ b[i];
String num = String.valueOf(sum);
for (int j=0; j<num.length();j++)
{
finalList.add(num.charAt(j)-'0');
}
/*while(sum/10 >0)
{
finalList.add(sum%10);
sum = sum/10;
}
finalList.add(sum);*/
}
if(iterator>lena)
{
addRemaing(a,iterator,finalList);
}
else
{
addRemaing(b,iterator,finalList);
}
Integer [] finalArray = new Integer [finalList.size()];
finalList.toArray(finalArray);
for(int i = 0; i<finalArray.length; i++)
{
System.out.print(" "+finalArray[i]);
}
}
public static void addRemaing(int maxSizeArray [], int length, List<Integer> finalList)
{
for(int i=length; i<maxSizeArray.length; i++)
{
finalList.add(maxSizeArray[i]);
}
}
}
public int[ ] solve(int a[ ], int b[ ]){
//Biggest result size will be a.length + b.length if all sums are bigger than 9
// and if the inputs have only single digits
int result[] = new int[(a.length + b.length)];
int count = 0;
int resultCount = 0;
while(count < a.length && count < b.length) {
int sum = a[count] + b[count];
if (sum < 10) {
result[resultCount++] = sum;
} else {
result[resultCount++] = sum / 10; //decimal
result[resultCount++] = sum % 10; //unit
}
count++;
}
//Complete array if a is bigger than b
while(count < a.length) {
result[resultCount++] = a[count++];
}
//Complete array if b is bigger than a
while(count < b.length) {
result[resultCount++] = b[count++];
}
return Arrays.copyOfRange(result, 0, resultCount);
}
import string
a= [1,2,3,4,5,6]
b= [2,3,4,5,6,7,8]
if len(a) >= len(b):
i= len(b)
else:
i = len(a)
af =[]
for x in range(0,i):
sum1 = a[x] + b[x]
if sum1 > 9:
str = sum1.__str__()
for z in str\
:
af.append(z)
else:
af.append(sum1)
if len(a) > len(b):
for y in range(x+1, len(a)):
af.append(a[y])
else:
for y in range(x+1, len(b)):
af.append(b[y])
print(af)
public class ArraySum2 {
public static void main(String[] args) {
int arr1[] = { 1, 5, 8, 3, 3, 5, 6 };
int arr2[] = { 4, 4, 7, 9, 5,15,3,6 };
int arr1Length = arr1.length;
int arr2Length = arr2.length;
int actuallength = 0;
if (arr1Length < arr2Length) {
actuallength = arr2Length;
} else {
actuallength = arr1Length;
}
int arr3[] = new int[actuallength];
int additonval = 0;
int additonval1 = 0;
int corma=0;
char firstLetterChar;
String numberString;
for (int i = 0; i < actuallength; i++) {
if (i < arr1Length) {
additonval = arr1[i];
} else {
additonval = 0;
}
if (i < arr2Length) {
additonval1 = arr2[i];
} else {
additonval1 = 0;
}
if ((additonval + additonval1) >= 10) {
corma = additonval + additonval1;
numberString = Integer.toString(corma);
firstLetterChar = numberString.charAt(0);
arr3[i] = Character.getNumericValue(firstLetterChar);
} else {
arr3[i] = additonval + additonval1;
}
System.out.println(arr3[i]);
}
}
}
public static void addTwoArrays(){
int[] a = new int[]{1,2,3,4,5,6};
int[] b = new int[]{2,3,4,5,6,7,8};
// 
int[] b = new int[]{2,3,4,5,6,7,8};
int min = a.length<b.length?a.length:b.length;
int newLen = (min)*2 + (a.length-b.length<0?(a.length-b.length)*-1:a.length-b.length);
System.out.println(newLen);
ArrayList<Integer> c = new ArrayList<Integer>();
int k =0;
for (int i = 0; i <min ; i++) {
int sum = (a[i]+b[i]);
if ((a[i]+b[i])>9){
c.add(1);
c.add(sum-10);
}else{
c.add(sum);
}
}
if (a.length>b.length){
for (int i = b.length; i <=a.length; i++) {
c.add(a[i]);
}
}else{
for (int i = a.length; i <=b.length; i++) {
c.add(b[i]);
}
}
for (Integer f:c){
System.out.println(f);
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class ArraySum {
public static int[] arraysSum(int arr1[], int arr2[]) {
int empty[] = {};
if (Objects.isNull(arr1) && Objects.isNull(arr2)) {
return empty;
} else if (Objects.isNull(arr1) || arr1.length == 0) {
return arr2;
} else if (Objects.isNull(arr2) || arr2.length == 0) {
return arr1;
}
if (arr1.length > arr2.length)
return getSum(arr1, arr2);
return getSum(arr2, arr1);
}
private static int[] getSum(int arr1[], int arr2[]) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < arr2.length; i++) {
int sum = arr1[i] + arr2[i];
if (sum > 9) {
String temp = new Integer(sum).toString();
for (int j = 0; j < temp.length(); j++) {
result.add(new Integer("" + temp.charAt(j)));
}
continue;
}
result.add(sum);
}
for (int j = arr2.length; j < arr1.length; j++) {
result.add(arr1[j]);
}
return getArray(result);
}
private static int[] getArray(List<Integer> arr) {
int result[] = new int[arr.size()];
int i = 0;
for (Integer data : arr) {
result[i] = data;
i++;
}
return result;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class ArraySum {
public static int[] arraysSum(int arr1[], int arr2[]) {
int empty[] = {};
if (Objects.isNull(arr1) && Objects.isNull(arr2)) {
return empty;
} else if (Objects.isNull(arr1) || arr1.length == 0) {
return arr2;
} else if (Objects.isNull(arr2) || arr2.length == 0) {
return arr1;
}
if (arr1.length > arr2.length)
return getSum(arr1, arr2);
return getSum(arr2, arr1);
}
private static int[] getSum(int arr1[], int arr2[]) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < arr2.length; i++) {
int sum = arr1[i] + arr2[i];
if (sum > 9) {
String temp = new Integer(sum).toString();
for (int j = 0; j < temp.length(); j++) {
result.add(new Integer("" + temp.charAt(j)));
}
continue;
}
result.add(sum);
}
for (int j = arr2.length; j < arr1.length; j++) {
result.add(arr1[j]);
}
return getArray(result);
}
private static int[] getArray(List<Integer> arr) {
int result[] = new int[arr.size()];
int i = 0;
for (Integer data : arr) {
result[i] = data;
i++;
}
return result;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class ArraySum {
public static int[] arraysSum(int arr1[], int arr2[]) {
int empty[] = {};
if (Objects.isNull(arr1) && Objects.isNull(arr2)) {
return empty;
} else if (Objects.isNull(arr1) || arr1.length == 0) {
return arr2;
} else if (Objects.isNull(arr2) || arr2.length == 0) {
return arr1;
}
if (arr1.length > arr2.length)
return getSum(arr1, arr2);
return getSum(arr2, arr1);
}
private static int[] getSum(int arr1[], int arr2[]) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < arr2.length; i++) {
int sum = arr1[i] + arr2[i];
if (sum > 9) {
String temp = new Integer(sum).toString();
for (int j = 0; j < temp.length(); j++) {
result.add(new Integer("" + temp.charAt(j)));
}
continue;
}
result.add(sum);
}
for (int j = arr2.length; j < arr1.length; j++) {
result.add(arr1[j]);
}
return getArray(result);
}
private static int[] getArray(List<Integer> arr) {
int result[] = new int[arr.size()];
int i = 0;
for (Integer data : arr) {
result[i] = data;
i++;
}
return result;
}
}
Python 3:
def sum_zip_split(list_a, list_b):
summed = [item_a + item_b for item_a, item_b in zip(list_a, list_b)]
if len(summed) != len(list_a) or len(summed) != len(list_b):
if len(summed) < len(list_a):
summed.extend(list_a[len(summed):])
else:
summed.extend(list_b[len(summed):])
result = []
for item in summed:
if len(str(item)) < 2:
result.append(item)
else:
temp = [int(value) for value in str(item)]
result += temp
return result
print(sum_zip_split([1,2,3,4,5], [1,2,8,4,5,6,7])) #[2, 4, 1, 1, 8, 1, 0, 6, 7]
package com.Array;
public class ArraySumWithCondi {
public static void main(String[] args) {
int a[]={1,2,3,4,5,6,9};
int b[]={2,3,4,5,6,7,9,3};
int i=0, j=0,k=0;
int c[]=new int[20];
while(i<a.length&&j<b.length){
int sum=a[i]+b[j];
if(sum<10){
c[k++]=sum;
}else{
int rem=sum%10;
int div=sum/10;
c[k++]=div;
c[k++]=rem;
}
i++;j++;
}
if(a.length<b.length){
while(j<=b.length-1){
c[k++]=b[j++];
}
}else{
while(i<=a.length-1){
c[k++]=a[i++];
}
}
for(int m=0;m<c.length;m++){
System.out.print(c[m]+" ");
}
System.out.println();
}
}
{
public class ArraySum {
public static void main(String[] args) {
int a[]={1,2,3,4,5,6,9};
int b[]={2,3,4,5,6,7,9,3};
int i=0, j=0,k=0;
int c[]=new int[20];
while(i<a.length&&j<b.length){
int sum=a[i]+b[j];
if(sum<10){
c[k++]=sum;
}else{
int rem=sum%10;
int div=sum/10;
c[k++]=div;
c[k++]=rem;
}
i++;j++;
}
if(a.length<b.length){
while(j<=b.length-1){
c[k++]=b[j++];
}
}else{
while(i<=a.length-1){
c[k++]=a[i++];
}
}
for(int m=0;m<c.length;m++){
System.out.print(c[m]+" ");
}
System.out.println();
}
}
}
public class ArraySum {
public static void main(String[] args) {
int a[]={1,2,3,4,5,6,9};
int b[]={2,3,4,5,6,7,9,3};
int i=0, j=0,k=0;
int c[]=new int[20];
while(i<a.length&&j<b.length){
int sum=a[i]+b[j];
if(sum<10){
c[k++]=sum;
}else{
int rem=sum%10;
int div=sum/10;
c[k++]=div;
c[k++]=rem;
}
i++;j++;
}
if(a.length<b.length){
while(j<=b.length-1){
c[k++]=b[j++];
}
}else{
while(i<=a.length-1){
c[k++]=a[i++];
}
}
for(int m=0;m<c.length;m++){
System.out.print(c[m]+" ");
}
System.out.println();
}
}
public class ArraySum {
public static void main(String[] args) {
int a[]={1,2,3,4,5,6,9};
int b[]={2,3,4,5,6,7,9,3};
int i=0, j=0,k=0;
int c[]=new int[20];
while(i<a.length&&j<b.length){
int sum=a[i]+b[j];
if(sum<10){
c[k++]=sum;
}else{
int rem=sum%10;
int div=sum/10;
c[k++]=div;
c[k++]=rem;
}
i++;j++;
}
if(a.length<b.length){
while(j<=b.length-1){
c[k++]=b[j++];
}
}else{
while(i<=a.length-1){
c[k++]=a[i++];
}
}
for(int m=0;m<c.length;m++){
System.out.print(c[m]+" ");
}
System.out.println();
}
}
public static void main(String[] args){
int [] a = {1, 2, 3, 4, 5, 6};
int [] b = {2, 3, 4, 5, 6, 7, 8};
ArrayList<Integer> array = new ArrayList<Integer>();
if(a.length==b.length){
for(int i=0;i<a.length;i++){
int c=a[i]+b[i];
if(c>10){
array.add(c/10);
array.add(c%10);
}
else
array.add(c);
}
}
else if(a.length<b.length){
for(int i=0;i<a.length;i++){
int c=a[i]+b[i];
if(c>10){
array.add(c/10);
array.add(c%10);
}
else
array.add(c);
}
for(int j=a.length;j<b.length;j++){
array.add(b[j]);
}
}
else if(a.length>b.length){
for(int i=0;i<b.length;i++){
int c=a[i]+b[i];
if(c>10){
array.add(c/10);
array.add(c%10);
}
else
array.add(c);
}
for(int j=b.length;j<a.length;j++){
array.add(a[j]);
}
}
System.out.println(array);
}
public static void main(String[] args){
int [] a = {1, 2, 3, 4, 5, 6};
int [] b = {2, 3, 4, 5, 6, 7, 8};
ArrayList<Integer> array = new ArrayList<Integer>();
if(a.length==b.length){
for(int i=0;i<a.length;i++){
int c=a[i]+b[i];
if(c>10){
array.add(c/10);
array.add(c%10);
}
else
array.add(c);
}
}
else if(a.length<b.length){
for(int i=0;i<a.length;i++){
int c=a[i]+b[i];
if(c>10){
array.add(c/10);
array.add(c%10);
}
else
array.add(c);
}
for(int j=a.length;j<b.length;j++){
array.add(b[j]);
}
}
else if(a.length>b.length){
for(int i=0;i<b.length;i++){
int c=a[i]+b[i];
if(c>10){
array.add(c/10);
array.add(c%10);
}
else
array.add(c);
}
for(int j=b.length;j<a.length;j++){
array.add(a[j]);
}
}
System.out.println(array);
}
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6};
int[] b = {2,3,4,5,6,7,8};
if(a.length>b.length) {
int[] temp = a;
a = b;
b = temp;
}
boolean arraysEqual = false;
if(a.length == b.length) {
arraysEqual = true;
}
ArrayList<Integer> al = new ArrayList<>();
int tempSum = 0;
for(int i=0; i<a.length; i++) {
tempSum = a[i] + b[i];
if(tempSum > 9) {
al.add(tempSum/10);
al.add(tempSum%10);
} else {
al.add(tempSum);
}
}
if(!arraysEqual) {
for(int j = a.length;j<b.length; j++) {
al.add(b[j]);
}
}
System.out.println(al);
//If they want the values in ARRAY only, then uncomment below and print
/*Integer[] arr = new Integer[al.size()];
arr = al.toArray(arr);*/
}
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6};
int[] b = {2,3,4,5,6,7,8};
if(a.length>b.length) {
int[] temp = a;
a = b;
b = temp;
}
boolean arraysEqual = false;
if(a.length == b.length) {
arraysEqual = true;
}
ArrayList<Integer> al = new ArrayList<>();
int tempSum = 0;
for(int i=0; i<a.length; i++) {
tempSum = a[i] + b[i];
if(tempSum > 9) {
al.add(tempSum/10);
al.add(tempSum%10);
} else {
al.add(tempSum);
}
}
if(!arraysEqual) {
for(int j = a.length;j<b.length; j++) {
al.add(b[j]);
}
}
System.out.println(al);
//If they want the values in ARRAY only, then uncomment below and print
/*Integer[] arr = new Integer[al.size()];
arr = al.toArray(arr);*/
}
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6};
int[] b = {2,3,4,5,6,7,8};
if(a.length>b.length) {
int[] temp = a;
a = b;
b = temp;
}
boolean arraysEqual = false;
if(a.length == b.length) {
arraysEqual = true;
}
ArrayList<Integer> al = new ArrayList<>();
int tempSum = 0;
for(int i=0; i<a.length; i++) {
tempSum = a[i] + b[i];
if(tempSum > 9) {
al.add(tempSum/10);
al.add(tempSum%10);
} else {
al.add(tempSum);
}
}
if(!arraysEqual) {
for(int j = a.length;j<b.length; j++) {
al.add(b[j]);
}
}
System.out.println(al);
//If they want the values in ARRAY only, then uncomment below and print
/*Integer[] arr = new Integer[al.size()];
arr = al.toArray(arr);*/
}
public static ArrayList<Integer> addTwoArrays(int[] inputFirstArrVal, int[] inputSecondArrVal) {
ArrayList<Integer> sumOfArrays = new ArrayList<Integer>();
int[] tempArray;
int iteratorArrLength = inputFirstArrVal.length - 1;
if (inputFirstArrVal.length > inputSecondArrVal.length) {
tempArray = Arrays.copyOf(inputFirstArrVal, inputFirstArrVal.length);
iteratorArrLength = inputSecondArrVal.length - 1;
} else {
tempArray = Arrays.copyOf(inputSecondArrVal, inputSecondArrVal.length);
iteratorArrLength = inputFirstArrVal.length - 1;
}
for (int i = 0; i <= iteratorArrLength; i++) {
if (inputFirstArrVal[i] + inputSecondArrVal[i] < 10) {
sumOfArrays.add(inputFirstArrVal[i] + inputSecondArrVal[i]);
} else {
sumOfArrays.add((inputFirstArrVal[i] + inputSecondArrVal[i]) / 10);
sumOfArrays.add((inputFirstArrVal[i] + inputSecondArrVal[i]) % 10);
}
// Add remaining elements in Array
if (i == iteratorArrLength)
for (int j = i + 1; j <= tempArray.length-1; j++) {
sumOfArrays.add(tempArray[j]);
}
}
return sumOfArrays;
}
}
- codeforfun April 05, 2016