- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrankJava0028.java
More file actions
Latest commit
58 lines (48 loc) · 1.98 KB
/
Copy pathHackerrankJava0028.java
File metadata and controls
58 lines (48 loc) · 1.98 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
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.stream.Collectors;
importjava.util.stream.IntStream;
importjava.util.stream.Stream;
publicclassHackerrankJava0028 {
publicstaticvoidmain(String[] args) throwsIOException {
// Java 2D Array
// https://www.hackerrank.com/challenges/java-2d-array/problem?isFullScreen=true
BufferedReaderbufferedReader = newBufferedReader(newInputStreamReader(System.in));
List<List<Integer>> arr = newArrayList<>();
IntStream.range(0, 6).forEach(i -> {
try {
arr.add(
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(Collectors.toList()));
} catch (IOExceptionex) {
thrownewRuntimeException(ex);
}
});
// -------------------------------------------------------------------------------------------------
// --- You need to add this lines for solution hackerrank provides all other lines for challange ---
// -------------------------------------------------------------------------------------------------
printResult(arr);
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
bufferedReader.close();
}
publicstaticvoidprintResult(List<List<Integer>> arr) {
intmaxSum = Integer.MIN_VALUE;
for (inti = 0; i < arr.size() - 2; i++) {
for (intj = 0; j < arr.get(i).size() - 2; j++) {
intsum = arr.get(i).get(j) + arr.get(i).get(j + 1) + arr.get(i).get(j + 2) +
arr.get(i + 1).get(j + 1) +
arr.get(i + 2).get(j) + arr.get(i + 2).get(j + 1) + arr.get(i + 2).get(j + 2);
if (maxSum < sum) {
maxSum = sum;
}
}
}
System.out.println(maxSum);
}
}