Algorithm
Problem Name: Mathematics -
https://www.hackerrank.com/challenges/best-divisor/problem?isFullScreen=true
In this HackerRank in Mathematics -
Kristen loves playing with and comparing numbers. She thinks that if she takes two different positive numbers, the one whose digits sum to a larger number is better than the other. If the sum of digits is equal for both numbers, then she thinks the smaller number is better. For example, Kristen thinks that 13 is better than 31 and that 12 is better than 11.
Given an integer, n, can you find the divisor of n that Kristin will consider to be the best?
Input Format
A single integer denoting n .
Constraints
- 0 <= n <= 105
Output Format
Print an integer denoting the best divisor of n .
Sample Input 0
12
Sample Output 0
6
Explanation 0
The set of divisors of 12 can be expressed as {1,2,3,4,6,12}. The divisor whose digits sum to the largest number is 6 (which, having only one digit, sums to itself). Thus, we print 6 as our answer.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int digit_sum(int n){
int sum = 0;
while(n>0){
sum += n%10;
n=n/10;
}
return sum;
}
int main()
{
int n;cin>>n;
int best=1;
for(int i=1;i < =n;i++){
if(n%i==0 && digit_sum(i)>digit_sum(best)){
best=i;
}
}
cout << best;
}
Copy The Code &
Try With Live Editor
#2 Code Example with Java Programming
Code -
Java Programming
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class Solution {
public static List < Integer> divisor(int n)
{
List list = new ArrayList<>();
int i=1;
while((i*i) < =n)
{
if(n%i==0){
list.add(i);
list.add(n/i);}
i++;
}
Collections.sort(list);
return list ;
}
public static int sumOFDigits(int n)
{
int s=0;
while(n>0)
{
s+=n%10;
n/=10;
}
return s;
}
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List < Integer> list =divisor(n); int maxKey =-1;
Map < Integer , List();
if(list.size()==2)
System.out.println(n);
else {
for(int i=0;i<list.size();i++)
{
int key = sumOFDigits(list.get(i));
maxKey = Math.max(maxKey, key>;
mp.putIfAbsent(key, new ArrayList < Integer>());
mp.get(key).add(list.get(i));
}
List < Integer> list1 = new ArrayList<>();
list1 = mp.get(maxKey);
int ans = list1.get(0);
System.out.println(ans);
}
bufferedReader.close();
}
}
Copy The Code &
Try With Live Editor
#3 Code Example with Python Programming
Code -
Python Programming
#!/bin/python3
import math
import os
import random
import re
import sys
def divisor(n):
div = [i for i in range(1,n+1) if n%i==0]
return max(div,key=lambda x: sum(int(i) for i in str(x)))
if __name__ == '__main__':
n = int(input().strip())
print(divisor(n))
Copy The Code &
Try With Live Editor