Sudo Placement[1.7] | Greatest Digital Root
Last Updated :
16 Dec, 2022
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.
Examples:
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.
C++
#include <bits/stdc++.h>
using namespace std;
int summ( int n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
void printDigitalRoot( int n)
{
int maxi = 1;
int dig = 1;
for ( int i = 1; i <= sqrt (n); i++) {
if (n % i == 0) {
int d1 = summ(n / i);
int d2 = summ(i);
if (d1 > maxi) {
dig = n / i;
maxi = d1;
}
if (d2 > maxi) {
dig = i;
maxi = d2;
}
if (d1 == maxi) {
if (dig < (n / i)) {
dig = n / i;
maxi = d1;
}
}
if (d2 == maxi) {
if (dig < i) {
dig = i;
maxi = d2;
}
}
}
}
cout << dig << " " << maxi << endl;
}
int main()
{
int n = 10;
printDigitalRoot(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int summ( int n)
{
if (n == 0 )
return 0 ;
return (n % 9 == 0 ) ? 9 : (n % 9 );
}
static void printDigitalRoot( int n)
{
int maxi = 1 ;
int dig = 1 ;
for ( int i = 1 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
int d1 = summ(n / i);
int d2 = summ(i);
if (d1 > maxi)
{
dig = n / i;
maxi = d1;
}
if (d2 > maxi)
{
dig = i;
maxi = d2;
}
if (d1 == maxi)
{
if (dig < (n / i))
{
dig = n / i;
maxi = d1;
}
}
if (d2 == maxi)
{
if (dig < i)
{
dig = i;
maxi = d2;
}
}
}
}
System.out.println(dig + " " + maxi);
}
public static void main(String[] args)
{
int n = 10 ;
printDigitalRoot(n);
}
}
|
Python3
def summ(n):
if (n = = 0 ):
return 0 ;
if (n % 9 = = 0 ):
return 9 ;
else :
return (n % 9 );
def printDigitalRoot(n):
maxi = 1 ;
dig = 1 ;
for i in range ( 1 , int ( pow (n, 1 / 2 ) + 1 )):
if (n % i = = 0 ):
d1 = summ(n / i);
d2 = summ(i);
if (d1 > maxi):
dig = n / i;
maxi = d1;
if (d2 > maxi):
dig = i;
maxi = d2;
if (d1 = = maxi):
if (dig < (n / i)):
dig = n / i;
maxi = d1;
if (d2 = = maxi):
if (dig < i):
dig = i;
maxi = d2;
print ( int (dig), " " , int (maxi));
if __name__ = = '__main__' :
n = 10 ;
printDigitalRoot(n);
|
C#
using System;
class GFG
{
static int summ( int n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
static void printDigitalRoot( int n)
{
int maxi = 1;
int dig = 1;
for ( int i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
int d1 = summ(n / i);
int d2 = summ(i);
if (d1 > maxi)
{
dig = n / i;
maxi = d1;
}
if (d2 > maxi)
{
dig = i;
maxi = d2;
}
if (d1 == maxi)
{
if (dig < (n / i))
{
dig = n / i;
maxi = d1;
}
}
if (d2 == maxi)
{
if (dig < i)
{
dig = i;
maxi = d2;
}
}
}
}
Console.WriteLine(dig + " " + maxi);
}
public static void Main()
{
int n = 10;
printDigitalRoot(n);
}
}
|
Javascript
<script>
function summ(n) {
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
function printDigitalRoot(n) {
var maxi = 1;
var dig = 1;
for (i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
var d1 = summ(n / i);
var d2 = summ(i);
if (d1 > maxi) {
dig = n / i;
maxi = d1;
}
if (d2 > maxi) {
dig = i;
maxi = d2;
}
if (d1 == maxi) {
if (dig < (n / i)) {
dig = n / i;
maxi = d1;
}
}
if (d2 == maxi) {
if (dig < i) {
dig = i;
maxi = d2;
}
}
}
}
document.write(dig + " " + maxi);
}
var n = 10;
printDigitalRoot(n);
</script>
|
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...