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 int maxHourglassSum(int[][] arr) {
int maxSum = Integer.MIN_VALUE;
for (int i = 0; i < 4; i++) { // we go from row 0 to 3
for (int j = 0; j < 4; j++) { // the same with col we go 0 to 3
int hourglassSum = (
arr[i][j] + arr[i][j+1] + arr[i][j+2] +
arr[i+1][j+1] +
arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]
);
maxSum = Math.max(maxSum, hourglassSum);
}
}
return maxSum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[][] arr = new int[6][6];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
arr[i][j] = scanner.nextInt();
}
}
scanner.close();
System.out.println(maxHourglassSum(arr));
}
}
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 →
import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.regex.*;
public class Solution { public static int maxHourglassSum(int[][] arr) { int maxSum = Integer.MIN_VALUE;
}