Sum of Two Arrays Element by Element

 

Two random integer arrays/lists have been given as ARR1 and ARR2 of size N and M respectively. Both the arrays/lists contain numbers from 0 to 9(i.e. single digit integer is present at every index). The idea here is to represent each array/list as an integer in itself of digits N and M.

You need to find the sum of both the input arrays/list treating them as two integers and put the result in another array/list i.e. output array/list will also contain only single digit at every index.


Sample Input 1:
3
6 2 4
3
7 5 6
Sample Output 1:
1 3 8 0

import java.util.Scanner;

public class SumTwoArrays {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n=s.nextInt();
		int a[]=new int[n];
		for(int i=0;i<n;i++)
		{
			a[i]=s.nextInt();
		}
		int m=s.nextInt();
		int b[]=new int[m];
	
		for(int i=0;i<m;i++)
		{
			b[i]=s.nextInt();
		}
		
	int[] c = new int[1 + Math.max(a.length, b.length)];
		sumOfTwoArrays(a, b, c);
		print(c);

	}
	
	public static void print(int a[])
	{
		System.out.println("Sum of Arrays -> ");
		for(int i=0;i<a.length;i++)
		{
			System.out.print(a[i]+" ");
		}
    }
	
	public static void sumOfTwoArrays(int arr1[], int arr2[], int output[]) {
		
		int sum=0;
		int carry=0;
		int i=arr1.length-1;
		int j=arr2.length-1;
		int k=output.length-1;
		
		while(i>=0 && j>=0)
		{
			sum=arr1[i--]+arr2[j--]+carry;
			output[k--]=sum%10;
			carry=sum/10;
		}
		while(i>=0)
		{
			sum=arr1[i--]+carry;
			output[k--]=sum%10;
			carry=sum/10;
		}
		while(j>=0)
		{
			sum=arr2[j--]+carry;
			output[k--]=sum%10;
			carry=sum/10;
		}
		output[k]=carry;
}
}

Comments

Popular posts from this blog

Minimum Length Word

Check Number Sequence

Star Pattern