Skip to content

Instantly share code, notes, and snippets.

@moazmohamed20
Last active May 7, 2022 22:59
Show Gist options
  • Save moazmohamed20/3eefdfd399d20bd7e800ebd265c6c10f to your computer and use it in GitHub Desktop.
Save moazmohamed20/3eefdfd399d20bd7e800ebd265c6c10f to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
public class Solution {
public static int getHourGlassSum(int[][] matrix, int hearRow, int headColumn, int HourGlassWidth) {
int sum = 0;
// Sum HourGlass Top Half
for (int r = 0; r <= HourGlassWidth/2; r++)
for (int c = r; c < HourGlassWidth-r; c++)
sum += matrix[hearRow+r][headColumn+c];
// Sum HourGlass Bottom Half
for (int r = 0; r < HourGlassWidth/2; r++)
for (int c = HourGlassWidth/2-r-1; c <= HourGlassWidth/2+r+1; c++)
sum += matrix[hearRow+HourGlassWidth/2+r+1][headColumn+c];
return sum;
}
public static void main(String[] args) throws IOException {
// Read The Maatrix
Scanner scan = new Scanner(System.in);
int[][] matrix = new int[6][6];
for (int r = 0; r < 6; r++) {
for (int c = 0; c < 6; c++) {
matrix[r][c] = scan.nextInt();
}
}
scan.close();
// Find Max Sum
int maxSum = Integer.MIN_VALUE;
for (int r = 0; r < 4; r++)
for (int c = 0; c < 4; c++) {
int sum = getHourGlassSum(matrix, r, c, 3);
if (sum > maxSum) maxSum = sum;
}
System.out.println(maxSum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment