-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.java
70 lines (62 loc) · 2.23 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//Problem: https://www.hackerrank.com/challenges/sherlock-and-the-beast
//Java 8
/*
Initial Thoughts: We can iterate over the input substracting 5
and 3 each time, because that is what it takes
to keep the number decent. When we subtract, if
the number is divisible by 3, we add 5's and are
done. If the number is divisible by 5, we subtract
5 threes and keep iterating. If it is not divisible
by three or five, we subtract 3 and continue
Time Complexity: O(n) //We must iterate over every every input number
Space Complexity: O(n) //We store our output as we go to prevent unecessary I/O
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int T = input.nextInt();
StringBuilder output = new StringBuilder("");
tests:
for(int t = 0; t < T; t++)
{
int n = input.nextInt();
StringBuilder decentNumber = new StringBuilder("");
int fives = 0;
int threes = 0;
while(n > 0)
{
if(n % 3 == 0) //n is divisible by 3
{
while(n > 0)
{
fives++;
n -= 3;
}
}
else if(n % 5 == 0) //n is divisible by 5
{
threes++;
n -= 5;
}
else //n is not divisible by 3 or 5
{
fives++;
n -= 3;
}
}
if(n < 0) //No decent number exist
output.append("-1");
else //Build the decent number
{
for(int i = 0; i < fives; i++)
output.append("555");
for(int i = 0; i < threes; i++)
output.append("33333");
}
output.append("\n"); //Move to the next line of output
}
System.out.println(output);
}
}