Skip to content

Commit 86992dd

Browse files
authored
Merge pull request #169 from sir-gon/feature/2d_array
[Hacker Rank] Interview Preparation Kit: Array: 2D Array - DS. Solved ✅.
2 parents 8d4847f + bd3aef0 commit 86992dd

File tree

4 files changed

+249
-0
lines changed
  • algorithm_exercises_csharp/src/hackerrank/interview_preparation_kit/arrays
  • algorithm_exercises_csharp_test
  • docs/hackerrank/interview_preparation_kit/arrays

4 files changed

+249
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
using System.Diagnostics.CodeAnalysis;
4+
5+
/**
6+
* 2D Array - DS.
7+
*
8+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/2d_array.md]]
9+
*/
10+
11+
public class TwoDArray
12+
{
13+
[ExcludeFromCodeCoverage]
14+
protected TwoDArray() { }
15+
16+
private static List<int> getHourGlass(List<List<int>> arr, int positionX, int positionY)
17+
{
18+
List<int> result = [];
19+
20+
// top
21+
result.Add(arr[positionX - 1][positionY - 1]);
22+
result.Add(arr[positionX - 1][positionY]);
23+
result.Add(arr[positionX - 1][positionY + 1]);
24+
25+
// middle
26+
result.Add(arr[positionX][positionY]);
27+
28+
// bottom
29+
result.Add(arr[positionX + 1][positionY - 1]);
30+
result.Add(arr[positionX + 1][positionY]);
31+
result.Add(arr[positionX + 1][positionY + 1]);
32+
33+
return result;
34+
}
35+
36+
public static int hourglassSum(List<List<int>> arr)
37+
{
38+
int matrixSize = 0;
39+
40+
if (arr.Count > 0)
41+
{
42+
matrixSize = arr.Count;
43+
}
44+
45+
int matrixStartIndex = 1;
46+
int matrixEndIndex = matrixSize - 2;
47+
48+
int? maxHourGlassSum = null;
49+
50+
for (int i = matrixStartIndex; i <= matrixEndIndex; i++)
51+
{
52+
for (int j = matrixStartIndex; j <= matrixEndIndex; j++)
53+
{
54+
int hourGlassSum = getHourGlass(arr, i, j).Sum();
55+
56+
if (maxHourGlassSum == null || hourGlassSum > maxHourGlassSum)
57+
{
58+
maxHourGlassSum = hourGlassSum;
59+
}
60+
}
61+
}
62+
63+
return maxHourGlassSum ?? 0;
64+
}
65+
}
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
[TestClass]
4+
public class TwoDArrayTest
5+
{
6+
public class TwoDArrayTestCase
7+
{
8+
public string title { get; set; } = default!;
9+
public List<List<int>> input { get; set; } = default!;
10+
public long expected { get; set; } = default!;
11+
}
12+
13+
private List<TwoDArrayTestCase> testCases { get; set; } = default!;
14+
15+
[TestInitialize]
16+
public void testInitialize()
17+
{
18+
testCases = JsonLoader.resourceLoad<List<TwoDArrayTestCase>>(
19+
"hackerrank/interview_preparation_kit/arrays/2d_array.testcases.json"
20+
) ?? [];
21+
}
22+
23+
[TestMethod]
24+
public void testHourglassSum()
25+
{
26+
long result;
27+
28+
foreach (TwoDArrayTestCase test in testCases)
29+
{
30+
result = TwoDArray.hourglassSum(test.input);
31+
Assert.AreEqual(test.expected, result);
32+
}
33+
}
34+
}
35+
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)