-
Notifications
You must be signed in to change notification settings - Fork 1
/
SherlockTheBeast.txt
122 lines (103 loc) · 2.38 KB
/
SherlockTheBeast.txt
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Challenge Link : https://www.hackerrank.com/challenges/sherlock-and-the-beast
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
private static boolean flag=false;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
int n = in.nextInt();
int[] a = new int[n];
// Intialize everything with 5
for(int x=0;x<n;x++){
a[x]=5;
}
flag = Verifycount(a);
if(flag){
print(a);
continue;
}
/* Finding the largest number starting with 5 */
int[] b = a.clone();
// Repeating last sequence with 3
for(int i = n-1;i>0;i--){
a[i] = 3;
flag = Verifycount(a);
if(flag){
break;
}
}
if(flag){
print(a);
continue;
}
/* Finding the largest number starting with 3 */
// Intialize everything Starting with 3
b[0] = 3;
for(int x=1;x<n;x++){
b[x]=5;
}
flag = Verifycount(b);
if(flag){
print(b);
continue;
}
// Repeating last sequence with 3
for(int i = n-1;i>0;i--){
b[i] = 3;
flag = Verifycount(b);
if(flag){
break;
}
}
if(flag){
print(b);
continue;
}
if(flag==false){
System.out.println("-1");
}
}
}
public static boolean Verifycount(int[] number){
int fcount = 0;
int tcount = 0;
for(int i = 0;i<number.length;i++){
if(number[i] == 5) {
fcount++;
}
else {
if(number[i] == 3){
tcount++;
}
}
}
if( tcount>0 && fcount > 0) {
if((tcount%5 == 0) && (fcount%3 == 0))
flag = true;
}
else{
if(tcount==0){
if(fcount%3 == 0) {
flag = true;
}
}
if(fcount==0){
if(tcount%5 == 0) {
flag = true;
}
}
}
return flag;
}
public static void print(int[] number){
for(int i =0;i<number.length;i++){
System.out.print(number[i]);
}
System.out.println("");flag=false;
}
}