0% found this document useful (0 votes)
69 views1 page

Maximum Sum of Hourglass in A Matrix

This document contains code for a Java program that takes in row and column values from the user, inputs an integer matrix of those dimensions, and finds the maximum sum of values within a 3x3 subsection of the matrix. It initializes variables, inputs the matrix values from the user, checks that the dimensions are at least 3x3, calculates the sums of all 3x3 subsections, and outputs the maximum sum.

Uploaded by

Krijay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views1 page

Maximum Sum of Hourglass in A Matrix

This document contains code for a Java program that takes in row and column values from the user, inputs an integer matrix of those dimensions, and finds the maximum sum of values within a 3x3 subsection of the matrix. It initializes variables, inputs the matrix values from the user, checks that the dimensions are at least 3x3, calculates the sums of all 3x3 subsections, and outputs the maximum sum.

Uploaded by

Krijay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

/******************************************************************************

Online Java Compiler.


Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int r = s.nextInt();
int c = s.nextInt();
int sum = 0, max =0;
int a[][] = new int[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
a[i][j]=s.nextInt();
}
}
if (r<3 || c<3)
{
System.exit(0);
}
for(int i =0;i< r-2;i++)
{
for(int j=0;j< c-2;j++)
{
sum = (a[i][j]+a[i][i+1]+a[i][i+2])+(a[i+1][j+1])+(a[i+2][j]
+a[i+2][j+1]+a[i+2][j+2]);
max = Math.max(max,sum);
}
}
System.out.println(max);
}
}

You might also like