Question:
Sherlock Holmes is getting paranoid about Professor Moriarty, his arch-enemy. All his efforts to subdue Moriarty have been in vain. These days Sherlock is working on a problem with Dr. Watson. Watson mentioned that the CIA has been facing weird problems with their supercomputer, 'The Beast', recently.
This afternoon, Sherlock received a note from Moriarty, saying that he has infected 'The Beast' with a virus. Moreover, the note had the number N printed on it. After doing some calculations, Sherlock figured out that the key to remove the virus is the largest Decent Number having N digits.
A Decent Number has the following properties:
- 3, 5, or both as its digits. No other digit is allowed.
- Number of times 3 appears is divisible by 5.
- Number of times 5 appears is divisible by 3.
Meanwhile, the counter to the destruction of 'The Beast' is running very fast. Can you save 'The Beast', and find the key before Sherlock?
I am trying to solve "Sherlock and the beast" problem (above) in hackerrank and here is what I ended with -
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
for(int i=0;i<n;i++) {
int subject = s.nextInt();
if(isPerfectlyDivisible(subject, 3)) {
System.out.println(stringBuilder("5", subject));
continue;
}
if (isPerfectlyDivisible(subject, 5)) {
System.out.println(stringBuilder("3", subject));
continue;
}
if(!isPerfectlyDivisible(subject, 5) && !(isPerfectlyDivisible(subject, 3))) {
List<Integer> multipliers = dividedMultipliers(subject);
if (multipliers.isEmpty()) {
System.out.println("-1");
} else {
System.out.println(stringBuilder("5", multipliers.get(0))+stringBuilder("3", multipliers.get(1)));
}
}
}
}
private static boolean isPerfectlyDivisible(int n, int divider) {
if (n < divider) return false;
if (n%divider == 0) return true;
return false;
}
private static List<Integer> dividedMultipliers(int n) {
List<Integer> multipliers = new ArrayList<Integer>();
for(int i=n;i>0;i--) {
if( (i%3 == 0) && ((n-i)%5 == 0)) {
multipliers.add(i);
multipliers.add(n-i);
break;
}
}
return multipliers;
}
private static String stringBuilder(String s, int times) {
StringBuffer buffer = new StringBuffer();
for(int i=0;i<times;i++) {
buffer.append(s);
}
return buffer.toString();
}
}
There were test cases failing for this. I tried looking at one of the failing test cases whose input is
5
2194
12002
21965
55140
57634
Surprisingly for 21965
the output is all 5's whereas my code above returns all 3's..which seems to be the expected output..Not sure where the issue? Any insights would be helpful
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…