forked from iamayushdaspro1/Programming_basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhourglass_challenge_java2.0.java
More file actions
59 lines (47 loc) · 1.64 KB
/
hourglass_challenge_java2.0.java
File metadata and controls
59 lines (47 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.Scanner;
class Codechef {
public static void main(String[] args) {
try
{
Scanner in = new Scanner(System.in);
int rows = 5;
int cols = 5;
int minValueInArray = -9;
int elementsInHourGlass = 7;
int maxHourGlassSum = minValueInArray * elementsInHourGlass;
int matrix[][] = new int[rows][cols];
int hourGlassSum;
for(int i=0; i < rows; i++){
for(int j=0; j < cols; j++){
matrix[i][j] = in.nextInt();
}
}
for(int i=0; i < rows; i++){
for(int j=0; j < cols; j++){
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
for (int i=0; i < (rows-2); i++) {
for (int j=0; j<(cols-2); j++) {
System.out.println ("Checking hourglass at (" + i + "," + j + ")");
System.out.println(matrix[i][j] + " " + matrix[i][j+1] + " " + matrix[i][j+2]);
System.out.println(" " + matrix[i+1][j+1]);
System.out.println(matrix[i+2][j] + " " + matrix[i+2][j+1] + " " + matrix[i+2][j+2]);
hourGlassSum = matrix[i][j] + matrix[i][j+1] + matrix[i][j+2] +
matrix[i+1][j+1] +
matrix[i+2][j] + matrix[i+2][j+1] + matrix[i+2][j+2];
System.out.println("hour glass sum = " + hourGlassSum);
if (hourGlassSum > maxHourGlassSum) {
maxHourGlassSum = hourGlassSum;
}
}
}
System.out.println("Maximum Hour Glass Sum = " + maxHourGlassSum);
}
catch(Exception e)
{
return;
}
}
}