Skip to content

Commit 9e403c9

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

File tree

1 file changed

+135
-0
lines changed
  • docs/hackerrank/interview_preparation_kit/arrays

1 file changed

+135
-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+
```

0 commit comments

Comments
 (0)