Time spent here:

Tuesday, 15 March 2016

SUM OF TWO NUMBER REPRESENTED AS ARRAY

Given two numbers represented by two arrays, write a function that returns sum array. The sum array is an array representation of addition of two input arrays. It is not allowed to modify the arrays.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case contains two integers M and N separated by a space. M is the size of arr1 and N is the size of arr2.
The second line of each test case contains M integers which is the input for arr1.
The third line of each test case contains N integers which is the input for arr2.

Output:
Print the sum list.

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ M ≤ 1000
0 ≤ arr1[i],arr2[i]≤ 9

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

2 2 7 5 3 3 7 3 3 6 8 3 4 8 4 4
CODE:
#include <stdio.h>
int main() {
int t,i,j,count=0,n,m;
scanf("%d",&t);
while(t--)
{
    scanf("%d %d",&m,&n);
    int a[m],b[n];
    for(i=0;i<m;i++)
       scanf("%d",&a[i]);
    for(i=0;i<m;i++)
       scanf("%d",&b[i]);
    j=n-1;
    for(i=m-1;i>=m-n;i--)
    {
        int k=a[i]+b[j]+count;
       // printf("k=%d\n",k);
        if(k>=10)
        {
            a[i]=k%10;
            count=k/10;
        }
       else
       {
           a[i]=k;
           count=0;
       }
    
        j--;
    }
 if(count!=0)
  printf("%d ",count);
 for(i=0;i<m;i++)
       printf("%d ",a[i]);
printf("\n");    
}
return 0;
}

No comments:

Post a Comment