-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava_2D_Array(hourglassSum).java
More file actions
57 lines (48 loc) · 2 KB
/
Java_2D_Array(hourglassSum).java
File metadata and controls
57 lines (48 loc) · 2 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
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);
//Complete the hourglassSum function below..
static int hourglassSum(int arr[][])
{
//calculating the length of the row.
int rows = arr.length;
//calculating the columns of the row..
int columns = arr[0].length;
//int max_hourglass_sum = Integer.MIN_VALUE;//storing the mimimum value..
int max_hourglass_sum = -63; //It is because the smallest value can be -9..and the max sum(hourglass) can be (-9*7=-63)..Hence it is better to use this...
for(int i=0;i<rows-2;i++)//here we are subtracting 2 bcoz of array exception.
{
for(int j=0;j<columns-2;j++)//same with this loop ...
{
//calculating the sum of the current hourglass sum...
int current_hourglass_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];
//checking whether the current sum is greater than max sum or not..
max_hourglass_sum = Math.max(max_hourglass_sum,current_hourglass_sum);
}
}
return max_hourglass_sum; //returning the maximum hourglass value..
}
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 result = hourglassSum(arr); //calling the function..
System.out.println(result);
scanner.close();
}
}