Skip to content

Commit e714635

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Array: 2D Array - DS. Solved ✅.
1 parent 7a11445 commit e714635

File tree

4 files changed

+291
-0
lines changed

4 files changed

+291
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package ae.hackerrank.interview_preparation_kit.arrays;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
/**
8+
* 2D Array - DS.
9+
*
10+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/2d_array.md]]
11+
*/
12+
13+
public class TwoDarray {
14+
private TwoDarray() { }
15+
16+
private static List<Integer> getHourGlass(List<List<Integer>> arr, int positionX, int positionY) {
17+
List<Integer> result = new ArrayList<>();
18+
19+
// top
20+
result.add(arr.get(positionX - 1).get(positionY - 1));
21+
result.add(arr.get(positionX - 1).get(positionY));
22+
result.add(arr.get(positionX - 1).get(positionY + 1));
23+
24+
// middle
25+
result.add(arr.get(positionX).get(positionY));
26+
27+
// bottom
28+
result.add(arr.get(positionX + 1).get(positionY - 1));
29+
result.add(arr.get(positionX + 1).get(positionY));
30+
result.add(arr.get(positionX + 1).get(positionY + 1));
31+
32+
return result;
33+
}
34+
35+
/**
36+
* hourglassSum.
37+
*/
38+
public static Integer hourglassSum(List<List<Integer>> arr) {
39+
int matrixSize = 0;
40+
41+
if (arr == null) {
42+
return null;
43+
}
44+
45+
matrixSize = arr.size();
46+
47+
int matrixStartIndex = 1;
48+
int matrixEndIndex = matrixSize - 2;
49+
50+
Integer maxHourGlassSum = null;
51+
52+
for (int i = matrixStartIndex; i <= matrixEndIndex; i++) {
53+
for (int j = matrixStartIndex; j <= matrixEndIndex; j++) {
54+
List<Integer> currentHourGlass = getHourGlass(arr, i, j);
55+
int hourGlassSum = currentHourGlass.stream()
56+
.collect(Collectors.summingInt(Integer::intValue));
57+
58+
if (maxHourGlassSum == null || hourGlassSum > maxHourGlassSum) {
59+
maxHourGlassSum = hourGlassSum;
60+
}
61+
}
62+
}
63+
64+
return maxHourGlassSum;
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package ae.hackerrank.interview_preparation_kit.arrays;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import org.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.TestInstance;
11+
import org.junit.jupiter.api.TestInstance.Lifecycle;
12+
import util.JsonLoader;
13+
14+
15+
@TestInstance(Lifecycle.PER_CLASS)
16+
class TwoDarrayTest {
17+
18+
public static class TwoDarrayTestCase {
19+
public String title;
20+
public List<List<Integer>> input;
21+
public long expected;
22+
}
23+
24+
private List<TwoDarrayTestCase> testCases;
25+
26+
@BeforeAll
27+
public void setup() throws IOException {
28+
String path = String.join("/", "hackerrank",
29+
"interview_preparation_kit",
30+
"arrays",
31+
"2d_array.testcases.json");
32+
33+
this.testCases = JsonLoader.loadJson(path, TwoDarrayTestCase.class);
34+
}
35+
36+
37+
@Test void testHourglassSum() {
38+
for (TwoDarrayTestCase testCase : testCases) {
39+
long solutionFound = TwoDarray.hourglassSum(testCase.input);
40+
41+
assertEquals(testCase.expected, solutionFound,
42+
"%s(%s) answer must be: %s".formatted(
43+
"TwoDarray.hourglassSum",
44+
testCase.input.toString(),
45+
testCase.expected
46+
)
47+
);
48+
}
49+
}
50+
51+
@Test void testHourglassSumEdgeCases() {
52+
List<List<Integer>> input = null;
53+
Integer expected = null;
54+
Integer solutionFound = TwoDarray.hourglassSum(null);
55+
assertEquals(expected, solutionFound,
56+
"%s(%s) answer must be: %s".formatted(
57+
"TwoDarray.hourglassSum",
58+
input,
59+
expected
60+
)
61+
);
62+
63+
input = new ArrayList<List<Integer>>();
64+
expected = null;
65+
solutionFound = TwoDarray.hourglassSum(null);
66+
67+
assertEquals(expected, solutionFound,
68+
"%s(%s) answer must be: %s".formatted(
69+
"TwoDarray.hourglassSum",
70+
input,
71+
expected
72+
)
73+
);
74+
}
75+
}
76+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"input": [
5+
[1, 1, 1, 0, 0, 0],
6+
[0, 1, 0, 0, 0, 0],
7+
[1, 1, 1, 0, 0, 0],
8+
[0, 0, 2, 4, 4, 0],
9+
[0, 0, 0, 2, 0, 0],
10+
[0, 0, 1, 2, 4, 0]
11+
],
12+
"expected": 19
13+
}
14+
]
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# [Arrays: 2D Array - DS](https://www.hackerrank.com/challenges/2d-array)
2+
3+
- Difficulty: ` #easy `
4+
- Category: ` #ProblemSolvingBasic `
5+
6+
Given a 6 × 6 2D Array, `arr`:
7+
8+
```text
9+
1 1 1 0 0 0
10+
0 1 0 0 0 0
11+
1 1 1 0 0 0
12+
0 0 0 0 0 0
13+
0 0 0 0 0 0
14+
0 0 0 0 0 0
15+
```
16+
17+
An hourglass in `A` is a subset of values with indices falling in this pattern
18+
in `arr`'s graphical representation:
19+
20+
```text
21+
a b c
22+
d
23+
e f g
24+
```
25+
26+
There are `16` hourglasses in `arr`.
27+
An hourglass sum is the sum of an hourglass' values.
28+
Calculate the hourglass sum for every hourglass in `arr`,
29+
then print the maximum hourglass sum. The array will always be 6 × 6.
30+
31+
## Example
32+
33+
arr =
34+
35+
```text
36+
-9 -9 -9 1 1 1
37+
0 -9 0 4 3 2
38+
-9 -9 -9 1 2 3
39+
0 0 8 6 6 0
40+
0 0 0 -2 0 0
41+
0 0 1 2 4 0
42+
```
43+
44+
The `16` hourglass sums are:
45+
46+
```text
47+
-63, -34, -9, 12,
48+
-10, 0, 28, 23,
49+
-27, -11, -2, 10,
50+
9, 17, 25, 18
51+
```
52+
53+
The highest hourglass sum is `26` from the hourglass beginning
54+
at row `1`, column `2`:
55+
56+
```text
57+
0 4 3
58+
1
59+
8 6 6
60+
````
61+
62+
**Note**: If you have already solved the Java domain's Java 2D Array challenge,
63+
you may wish to skip this challenge.
64+
65+
## Function Description
66+
67+
Complete the function hourglassSum in the editor below.
68+
69+
hourglassSum has the following parameter(s):
70+
71+
- `int arr[6][6]`: an array of integers
72+
73+
## Returns
74+
75+
- int: the maximum hourglass sum
76+
77+
## Input Format
78+
79+
Each of the `6` lines of inputs `arr[i]` contains space-separated integers `arr[i][j]`.
80+
81+
## Constraints
82+
83+
- $9 \leq arr[i][j] \leq 9$
84+
- $0 \leq i, j \leq 5$
85+
86+
## Output Format
87+
88+
Print the largest (maximum) hourglass sum found in `arr`.
89+
90+
## Sample Input
91+
92+
```text
93+
1 1 1 0 0 0
94+
0 1 0 0 0 0
95+
1 1 1 0 0 0
96+
0 0 2 4 4 0
97+
0 0 0 2 0 0
98+
0 0 1 2 4 0
99+
```
100+
101+
## Sample Output
102+
103+
```text
104+
19
105+
```
106+
107+
## Explanation
108+
109+
`arr` contains the following hourglasses:
110+
111+
```text
112+
111 110 100 000
113+
1 0 0 0
114+
111 110 100 000
115+
116+
010 100 000 000
117+
0 1 0 0
118+
002 024 244 440
119+
120+
111 110 100 000
121+
0 2 4 4
122+
000 002 020 200
123+
124+
002 024 244 440
125+
0 0 2 0
126+
001 012 124 240
127+
```
128+
129+
The hourglass with the maximum sum (`19`) is:
130+
131+
```text
132+
2 4 4
133+
2
134+
1 2 4
135+
```

0 commit comments

Comments
 (0)