We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
List<List<Integer>> arr = new ArrayList<>();
for (int i = 0; i < 6; i++) {
String[] arrRowTempItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
List<Integer> arrRowItems = new ArrayList<>();
for (int j = 0; j < 6; j++) {
int arrItem = Integer.parseInt(arrRowTempItems[j]);
arrRowItems.add(arrItem);
}
arr.add(arrRowItems);
}
bufferedReader.close();
int maxSum = Integer.MIN_VALUE;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int sum = calculateHourglassSum(arr, i, j);
if (sum > maxSum) {
maxSum = sum;
}
}
}
System.out.println(maxSum);
}
private static int calculateHourglassSum(List<List<Integer>> arr, int row, int col) {
int sum = 0;
sum += arr.get(row).get(col) + arr.get(row).get(col + 1) + arr.get(row).get(col + 2);
sum += arr.get(row + 1).get(col + 1);
sum += arr.get(row + 2).get(col) + arr.get(row + 2).get(col + 1) + arr.get(row + 2).get(col + 2);
return sum;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java 2D Array
You are viewing a single comment's thread. Return to all comments →
My solution in using java 7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List;
public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
}