You are viewing a single comment's thread. Return to all comments →

I suggest

import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { int a[][] = new int[6][6]; int maxSum = Integer.MIN_VALUE; try (Scanner scanner = new Scanner(System.in);) { for(int i = 0; i < 6; i++) { for(int j = 0; j < 6; j++) { a[i][j] = scanner.nextInt(); if (i > 1 && j > 1) { int sum = a[i][j] + a[i][j-1] + a[i][j-2] + a[i-1][j-1] + a[i-2][j] + a[i-2][j-1] + a[i-2][j-2]; if (sum > maxSum) {maxSum = sum;} } } } } System.out.println(maxSum); } }

It has the advantage to not run through the array a second time and instead calculate the results as soon as the data to do it is available.

## Java 2D Array

You are viewing a single comment's thread. Return to all comments →

I suggest

It has the advantage to not run through the array a second time and instead calculate the results as soon as the data to do it is available.