Hackerrank Contest - Finding the Largest Special Factor Sub number
Given a number innum greater than 100 print outnum as per the below logic:
- Form a collection of 2-digit numbers considering the consecutive digits starting from the leftmost digit in innum.
- For each of the 2-digit numbers in the collection, find the number of special factors
- Special factor is a factor which is also present in innum.
- Print outnum, the 2-digit number in the collection which has the maximum number of special factors
- If more than one 2-digit numbers are having maximum number of special factors, consider the largest 2-digit number
Input Format
Read innum from the standard input stream
Constraints
1<=n<=10^8
Output Format
Print outnum to the standard output stream
Sample Input 0
2340567
Sample Output 0
56
Explanation 0
For the given innum, the collection of 2-digit numbers is 23, 34, 40, 05, 56 and 67. The number of special factors for each of the 2-digit numbers are
- For 23, the factors are 1 and 23. Only one factor (23) is present (2340567) in innum. Hence the number of special factors is one
- For 34, the factors are 1, 2, 17 and 34. Two factors (2 34) are present in innum. Hence the number of special factors is two
- For 40, the factors are 1, 2, 4, 5, 8, 10, 20, 40. Four factors (2, 4, 5, 40) are present in Innum. Hence the number of special factors is four
- For 05, the factors are 1 and 5 Only one factor is present in innum Hence the number of special factors is one
- For 56, factors are 1,56,2,28 4,14,8,7 Four factors (2,4,7,56) are present in innum Hence the number of special factors is four
- For 67, factors are 1,67. Only one factor (67) is present in innum. Hence the number special factors is one
- Here 56 and 40 have the maximum number of factors present in innum. Since there are more than one 2-digit numbers having maximum number of special factors, consider the largest one is 56 and hence the Output
Solution
import java.io.*;
import java.util.*;
class Result {
public static ArrayList<Integer> getFactors(int n){
ArrayList<Integer> factorList = new ArrayList<Integer>();
for (int i = 1; i <= n; ++i) {
if (n % i == 0) {
factorList.add(i);
}
}
return factorList;
}
public static int factor(int n) {
String innumStr = n+"";
// Generating Outnums
ArrayList<Integer> outnumList = new ArrayList<Integer>();
for (int i = 0; i < innumStr.length()-1; i++) {
outnumList.add(Integer.parseInt(innumStr.substring(i,i+2)));
}
ArrayList<Integer> specialFactorCountList = new ArrayList<Integer>();
for (int i = 0; i < outnumList.size(); i++) {
int specialFactorCount = 0;
// getting factors for each outnum
ArrayList<Integer> temp = getFactors(outnumList.get(i));
for (int j = 0; j < temp.size(); j++) {
if (innumStr.contains(temp.get(j)+"")){
specialFactorCount++;
}
}
specialFactorCountList.add(specialFactorCount);
}
int max = Collections.max(specialFactorCountList);
ArrayList<Integer> resultList = new ArrayList<Integer>();
for (int i = 0; i < specialFactorCountList.size(); i++) {
if (specialFactorCountList.get(i) == max){
resultList.add(outnumList.get(i));
}
}
return Collections.max(resultList);
}
}
class Solution {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int res = Result.factor(n);
System.out.println(res);
}
}
Comments
Post a Comment