Skip to content

Commit e121eaf

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

File tree

4 files changed

+237
-0
lines changed

4 files changed

+237
-0
lines changed
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+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/2d_array.md]]
3+
*/
4+
5+
export function gethourGlass(
6+
arr,
7+
positionX,
8+
positionY
9+
) {
10+
const result = [];
11+
12+
// top
13+
result.push(arr[positionX - 1][positionY - 1]);
14+
result.push(arr[positionX - 1][positionY]);
15+
result.push(arr[positionX - 1][positionY + 1]);
16+
// middle
17+
result.push(arr[positionX][positionY]);
18+
// bottom
19+
result.push(arr[positionX + 1][positionY - 1]);
20+
result.push(arr[positionX + 1][positionY]);
21+
result.push(arr[positionX + 1][positionY + 1]);
22+
return result;
23+
}
24+
25+
export function hourglassSum(arr) {
26+
let matrixSize = 0;
27+
28+
if (arr?.[0]) {
29+
matrixSize = arr.length;
30+
}
31+
32+
const matrixStartIndex = 1;
33+
const matrixStopIndex = matrixSize - 2;
34+
35+
console.debug(`matrix size ${matrixSize}`);
36+
37+
let maxHourglassSum = null;
38+
39+
// recorrido
40+
for (let i = matrixStartIndex; i <= matrixStopIndex; i++) {
41+
for (let j = matrixStartIndex; j <= matrixStopIndex; j++) {
42+
// hourglass centers
43+
console.debug(`posicion (${i},${j}): ${arr[i][j]}`);
44+
45+
const houglassValues = gethourGlass(arr, i, j);
46+
47+
const thisHourglassSum = houglassValues.reduce(
48+
(a, b) => a + b,
49+
0
50+
);
51+
52+
console.debug(houglassValues, `thisHourglassSum: ${thisHourglassSum}`);
53+
54+
if (
55+
maxHourglassSum === undefined ||
56+
maxHourglassSum === null ||
57+
thisHourglassSum > maxHourglassSum
58+
) {
59+
maxHourglassSum = thisHourglassSum;
60+
}
61+
}
62+
}
63+
64+
return maxHourglassSum;
65+
}
66+
67+
export default { hourglassSum };
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger.js';
3+
4+
import { hourglassSum } from './2d_array.js';
5+
import TEST_CASES from './2d_array.testcases_test.json';
6+
7+
describe('arrays: 2d Array hourglassSum', () => {
8+
it('hourglassSum Test Cases', () => {
9+
expect.assertions(1);
10+
11+
TEST_CASES.forEach((test) => {
12+
const answer = hourglassSum(test.input);
13+
14+
console.debug(
15+
`gethourGlass(${test.input.toString()}) solution found: ${answer}`
16+
);
17+
18+
expect(answer).toStrictEqual(test.expected);
19+
});
20+
});
21+
});
Lines changed: 14 additions & 0 deletions
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+
]

0 commit comments

Comments
 (0)