Medium
A step(x) operation works like this: it changes a number x into x - s(x), where s(x) is the sum of x's digits. You like applying functions to numbers, so given the number n, you decide to build a decreasing sequence of numbers: n, step(n), step(step(n)), etc., with 0 as the last element.
Building a single sequence isn't enough for you, so you replace all elements of the sequence with the sums of their digits (s(x)). Now you're curious as to which number appears in the new sequence most often. If there are several answers, return the maximal one.
Example
-
For n = 88, the output should be
mostFrequentDigitSum(n) = 9.- Here is the first sequence you built: 88, 72, 63, 54, 45, 36, 27, 18, 9, 0;
- And here is s(x) for each of its elements: 16, 9, 9, 9, 9, 9, 9, 9, 9, 0.
As you can see, the most frequent number in the second sequence is 9.
-
For n = 8, the output should be
mostFrequentDigitSum(n) = 8.- At first you built the following sequence: 8, 0
- s(x) for each of its elements is: 8, 0
As you can see, the answer is 8 (it appears as often as 0, but is greater than it).
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] integer n
Guaranteed constraints:
1 ≤ n ≤ 10^5. -
[output] integer
- The most frequent number in the sequence s(n), s(step(n)), s(step(step(n))), etc.
[C] Syntax Tips
// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
char * answer = malloc(strlen(name) + 8);
printf("This prints to the console when you Run Tests");
strcpy(answer, "Hello, ");
strcat(answer, name);
return answer;
}
Solution
int s(int x)
{
int sum=0;
while(x>0)
{
sum+=x%10;
x/=10;
}
return sum;
}
int mostFrequentDigitSum(int n)
{
int list[46]={0, }, max=0;
while(n>0)
{
list[s(n)]++;
n-=s(n);
}
for(int i=0;i<46;i++)
max=list[i]>max?list[i]:max;
for(int i=45;i>=0;i--)
if(list[i]==max)
return i;
}
'Codesignal' 카테고리의 다른 글
<Codesignal> Numbers of Clans (0) | 2020.05.03 |
---|---|
<Codesignal> Numbers Grouping (0) | 2020.05.02 |
<Codesignal> Create Anagram (0) | 2020.04.19 |
<Codesignal> Is Substitution Cipher? (0) | 2020.04.19 |
<Codesignal> Strings Construction (0) | 2020.04.19 |