Given a number N, you need to find a divisor of N such that the Digital Root of that divisor is the greatest among all other divisors of N. If more than one divisors give the same greatest Digital Root then output the maximum divisor.
The Digital Root of a non-negative number can be obtained by repeatedly summing the digits of the number until we reach to a single digit. Example: DigitalRoot(98)=9+8=>17=>1+7=>8(single digit!). The task is to print the greatest divisor having the greatest Digital Root followed by a space and the Digital Root of that divisor.
Input: N = 10
Output: 5 5
The divisors of 10 are: 1, 2, 5, 10. The Digital Roots of these divisors are as follows:
1=>1, 2=>2, 5=>5, 10=>1. The greatest Digital Root is 5 which is produced by divisor 5, so answer is 5 5
Input: N = 18
Output: 18 9
The divisors of 18 are: 1, 2, 3, 6, 9, 18. The Digital Roots of these divisors are as follows:
1=>1, 2=>2, 3=>3, 6=>6, 9=>9, 18=>9. As we can see that 9 and 18 both have the greatest Digital Root of 9. So we select the maximum of those divisors, that is Max(9, 18)=18. So answer is 18 9
A naive approach will be to iterate till N and find all the factors and their digit sums. Store the largest among them and print them.
Time Complexity: O(N)
An efficient approach is to loop till sqrt(N), then the factors will be i and n/i. Check for the largest digit sum among them, in case of similar digit sums, store the larger factor. Once the iteration is completed, print them.
Below is the implementation of the above approach.
Time Complexity: O(sqrt(N))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Digital Root (repeated digital sum) of the given large integer
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Print a number containing K digits with digital root D
- Find Nth positive number whose digital root is X
- Numbers in a Range with given Digital Root
- Digital Root of a given large integer using Recursion
- Greatest divisor which divides all natural number in range [L, R]
- Sum of greatest odd divisor of numbers in given range
- Greatest Integer Function
- Greatest number less than equal to B that can be formed from the digits of A
- Greatest number that can be formed from a pair in a given Array
- Greatest odd factor of an even number
- Program to find Greatest Common Divisor (GCD) of N strings
- Queries to find Kth greatest character in a range [L, R] from a string with updates
- Babylonian method for square root
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
- Square root of an integer
- Euler's criterion (Check if square root under modulo p exists)
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.