Advertisement
deyanmalinov

05. Maximum Sum of 2x2 Submatrix

May 24th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner scan = new Scanner(System.in);
  5.         String[] matrSize = scan.nextLine().split(", ");
  6.         int rows = Integer.parseInt(matrSize[0]);
  7.         int cols = Integer.parseInt(matrSize[1]);
  8.         int[][] matrix = new int[rows][cols];
  9.         for (int row = 0; row < matrix.length; row++) {
  10.             String[] line= scan.nextLine().split(", ");
  11.             for (int col = 0; col < matrix[0].length; col++) {
  12.                 matrix[row][col]= Integer.parseInt(line[col]);
  13.             }
  14.         }
  15.         int maxSum = 0;
  16.         int rowMax = 0;
  17.         int colMax = 0;
  18.         for (int i = 0; i < rows-1; i++) {
  19.             for (int j = 0; j < cols-1; j++) {
  20.                 int colSum = matrix[i][j] + matrix[i+1][j] + matrix[i][j+1] + matrix[i+1][j+1];
  21.                 if (colSum > maxSum){
  22.                     maxSum = colSum;
  23.                     rowMax = i;
  24.                     colMax = j;
  25.                 }
  26.             }
  27.         }
  28.         System.out.println(matrix[rowMax][colMax]+ " " + matrix[rowMax][colMax+1]);
  29.         System.out.println(matrix[rowMax+1][colMax]+ " " + matrix[rowMax+1][colMax+1]);
  30.         System.out.println(maxSum);
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement