Find The Sum of Uncommon Numbers
Given two integer arrays input1[] and input2[ ],
extract the numbers which are present only in any
Calculate the sum of those numbers. Lets call it sur
sum1,
i.e keep adding the digits of sum1 until you arrive a
Return that single digit as output.
Note:
1.Array size ranges from 1 to 10.
Answers
Answer:
#include<iostream>
using namespace std;
int ispresent(int arr[],int k,int t){
for(int i = 0;i<=k;i++){
if(arr[i] == t){
return 1;
}
}
return 0;
}
int sum(int input1[],int input2[],int n,int m){
int arr[10];
int k = -1;
for(int i = 0;i<n;i++){
int flag = 0;
for(int j = 0;j<m;j++){
if(input1[i] == input2[j]){
flag = 1;
break;
}
}
if(flag == 0){
if(k == -1 || (ispresent(arr,k,input1[i]) == false) ){
++k;
arr[k] = input1[i];
}
}
}
for(int i = 0;i<m;i++){
int flag = 0;
for(int j = 0;j<n;j++){
if(input2[i] == input1[j]){
flag = 1;
break;
}
}
if(flag == 0){
if(k == -1 || (ispresent(arr,k,input2[i]) == 0) ){
++k;
arr[k] = input2[i];
}
}
}
int sum = 0;
for(int i = 0;i<=k;i++){
sum += arr[i];
}
return (sum % 9 == 0) ? (9) : (sum % 9);
}
int main(){
int n,m;
printf("Enter size of both array:- ");
scanf("%d%d",&n,&m);
int a1[n],a2[m];
for(int i = 0;i<n;i++){
scanf("%d",&a1[i]);
}
for(int i = 0;i<m;i++){
scanf("%d",&a2[i]);
}
cout << sum(a1,a2,n,m);
}
Explanation: