Java 2D Array
For Explanation Watch Video:
Sample Input
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
Sample Output
19
Code:
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 {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) { int[][] arr = new int[6][6];
for (int i = 0; i < 6; i++) { String[] arrRowItems = scanner.nextLine().split(" "); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int j = 0; j < 6; j++) { int arrItem = Integer.parseInt(arrRowItems[j]); arr[i][j] = arrItem; } } int max = Integer.MIN_VALUE; for(int i=0;i<=3;i++){ for(int j=0;j<=3;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(sum>max){ max = sum; } } } System.out.println(max); scanner.close(); }}
Comments
Post a Comment