Skip to content
Advertisement

Java – How to Solve this 2D Array Hour Glass?

I am working on a problem where I’ve to print the largest sum among all the hourglasses in the array. You can find the details about the problem here-

What I tried:

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int arr[][] = new int[6][6];
        for (int arr_i = 0; arr_i < 6; arr_i++) {
            for (int arr_j = 0; arr_j < 6; arr_j++) {
                arr[arr_i][arr_j] = in.nextInt();
            }
        }

        int sum = 0;
        int tmp_sum = 0;
        for (int arr_i = 0; arr_i < 4; arr_i++) {
            for (int arr_j = 0; arr_j < 4; arr_j++) {
                if (arr[arr_i][arr_j] > 0) {
                    sum = sum + (arr[arr_i][arr_j]) + (arr[arr_i][arr_j + 1]) + (arr[arr_i][arr_j + 2]);
                    sum = sum + (arr[arr_i + 1][arr_j + 1]);
                    sum = sum + (arr[arr_i + 2][arr_j]) + (arr[arr_i + 2][arr_j + 1]) + (arr[arr_i + 2][arr_j + 2]);
                    if (tmp_sum < sum) {
                        tmp_sum = sum;
                    }
                    sum = 0;
                }
            }
        }
        System.out.println(tmp_sum);
    }
}

Input:

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 9 2 -4 -4 0
0 0 0 -2 0 0
0 0 -1 -2 -4 0

Output:

12

Expected Output:

13

Screenshot: enter image description here

I don’t know where I’m doing wrong. I cannot understand why the expected output is 13. According to the description given in the problem it should be 10. Is this a wrong question or my understanding about this is wrong?

Advertisement

Answer

Remove the if (arr[arr_i][arr_j] > 0) statement. It prevents finding the answer at row 1, column 0, because that cell is 0.

Comments for other improvements to your code:

  • What if the best hourglass sum is -4? You should initialize tmp_sum to Integer.MIN_VALUE. And name it maxSum, to better describe it’s purpose.

  • You shouldn’t define sum outside the loop. Declare it when it is first assigned, then you don’t have to reset it to 0 afterwards.

  • Your iterators should be just i and j. Those are standard names for integer iterators, and keeps code … cleaner.
    If you prefer longer names, use row and col, since that is what they represent.

  • You don’t need parenthesis around the array lookups.

  • For clarity, I formatted the code below to show the hourglass shape in the array lookups.

Scanner in = 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] = in.nextInt();
    }
}

int maxSum = Integer.MIN_VALUE;
for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
        int sum = 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];
        if (maxSum < sum) {
            maxSum = sum;
        }
    }
}
System.out.println(maxSum);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement