when two integer arrays input 1[ ] and input 2[ ] . subtract the numbers which are present only in any one of the array (uncommon numbers ). calculate the sum of those numbers . let's call it Sum 1.and calculate single digit sum of sum 1.i.e. keep adding the digits of sum 1 until you arrive at a single point. return that single digit as output
NOTE - 1.array size ranges from 1 to 10
2.all the arrays are positive numbers
3. atleast one uncommon number will be present in arrays
Answers
Input: n = 5
Output: Sum of digits in numbers from 1 to 5 = 15
Input: n = 12
Output: Sum of digits in numbers from 1 to 12 = 51
Input: n = 328
Output: Sum of digits in numbers from 1 to 328 = 3241
Naive Solution:
A naive solution is to go through every number x from 1 to n, and compute sum in x by traversing all digits of x. Below is the implementation of this idea.
// A Simple C++ program to compute sum of digits in numbers from 1 to n
#include<bits/stdc++.h>
using namespace std;
int sumOfDigits(int );
// Returns sum of all digits in numbers from 1 to n
int sumOfDigitsFrom1ToN(int n)
{
int result = 0; // initialize result
// One by one compute sum of digits in every number from
// 1 to n
for (int x = 1; x <= n; x++)
result += sumOfDigits(x);
return result;
}
// A utility function to compute sum of digits in a
// given number x
int sumOfDigits(int x)
{
int sum = 0;
while (x != 0)
{
sum += x %10;
x = x /10;
}
return sum;
}
// Driver Program
int main()
{
int n = 328;
cout << "Sum of digits in numbers from 1 to " << n << " is "
<< sumOfDigitsFrom1ToN(n);
return 0;
}
Answer:
dkdkndndnf
Explanation:
nxhkdkakandbfjfkdkd