-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
129 lines (99 loc) · 2.69 KB
/
Solution.java
File metadata and controls
129 lines (99 loc) · 2.69 KB
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
123
124
125
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
BufferedReader br = null;
PrintWriter pw = null;
try{
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
String oneline = br.readLine();
int lineNum = 1;
Integer totalLineNum = null;
Set<Integer> set = new HashSet<>();
while(oneline!= null ){
if(lineNum == 1){
try {
totalLineNum = Integer.parseInt(oneline);
} catch (NumberFormatException e) {
outMsgForWrongInput(pw, lineNum);
}
}else if(oneline.trim().length() == 0){
outMsgForWrongInput(pw, lineNum);
}else if(totalLineNum == null || lineNum > totalLineNum+1){
outMsgForWrongInput(pw, lineNum);
}else{
String [] splits = oneline.split(" ");
boolean allValidDigits = checkAllValidDigits(splits);
if(!allValidDigits){
outMsgForWrongInput(pw, lineNum);
}else{
set.clear();
int max = Integer.MIN_VALUE;
for(String split: splits){
Integer val = null;
try {
val = Integer.parseInt(split);
} catch (NumberFormatException e) {
outMsgForWrongInput(pw, lineNum);
}
if(val == null){
break;
}
if(val<=0 ){
outMsgForWrongInput(pw, lineNum);
break;
}else if(set.contains(val)){
outMsgForWrongInput(pw, lineNum);
break;
}else{
set.add(val);
max = Math.max(max, val);
}
}
if(set.size() > 0 && set.size() == splits.length){
if(max == splits.length){
pw.println("SUCCESS => RECEIVED: "+splits.length);
}else{
pw.println("FAILURE => RECEIVED: "+splits.length+", EXPECTED: "+max);
}
}
}
}
lineNum+=1;
oneline = br.readLine();
}
}catch(IOException e){
throw new RuntimeException(e.getMessage(),e);
}finally{
if(pw != null){
pw.flush();
pw.close();
}
if(br!= null){
try {
br.close();
} catch (IOException e) {
throw new RuntimeException(e.getMessage(),e);
}
}
}
}
private static void outMsgForWrongInput(PrintWriter pw, int lineNum) {
pw.println("FAILURE => WRONG INPUT (LINE "+lineNum+")");
}
private static boolean checkAllValidDigits(String[] splits) {
for(String split:splits){
for(int i=0;i<split.length();i+=1){
if(!Character.isDigit(split.charAt(i))){
return false;
}
}
}
return true;
}
}