-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTwoDArrayDS.java
43 lines (39 loc) · 1.22 KB
/
TwoDArrayDS.java
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
package datastructures.arrays;
import org.junit.Assert;
/**
* 2D Array - DS
* https://www.hackerrank.com/challenges/2d-array/problem
* Difficulty : Easy
* Related Topics : Arrays
*
* created by Cenk Canarslan on 2021-01-10
*/
public class TwoDArrayDS {
static int hourglassSum(int[][] arr) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (i <= arr.length-3 && j <= arr[i].length - 3) {
int 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];
if (sum > max) {
max = sum;
}
}
}
}
return max;
}
public static void main(String[] args) {
int[][] arr = {
{1, 1, 1, 0, 0, 0},
{0, 1, 0, 0, 0, 0},
{1, 1, 1, 0, 0, 0},
{0, 0, 2, 4, 4, 0},
{0, 0, 0, 2, 0, 0},
{0, 0, 1, 2, 4, 0}};
int result = hourglassSum(arr);
Assert.assertEquals(19, result);
}
}