Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hacker Rank] Interview Preparation Kit: Array: 2D Array - DS. Solved ✅. #650

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions docs/hackerrank/interview_preparation_kit/arrays/two_d_array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# [Arrays: 2D Array - DS](https://www.hackerrank.com/challenges/2d-array)

- Difficulty: ` #easy `
- Category: ` #ProblemSolvingBasic `

Given a 6 × 6 2D Array, `arr`:

```text
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
```

An hourglass in `A` is a subset of values with indices falling in this pattern
in `arr`'s graphical representation:

```text
a b c
d
e f g
```

There are `16` hourglasses in `arr`.
An hourglass sum is the sum of an hourglass' values.
Calculate the hourglass sum for every hourglass in `arr`,
then print the maximum hourglass sum. The array will always be 6 × 6.

## Example

arr =

```text
-9 -9 -9 1 1 1
0 -9 0 4 3 2
-9 -9 -9 1 2 3
0 0 8 6 6 0
0 0 0 -2 0 0
0 0 1 2 4 0
```

The `16` hourglass sums are:

```text
-63, -34, -9, 12,
-10, 0, 28, 23,
-27, -11, -2, 10,
9, 17, 25, 18
```

The highest hourglass sum is `26` from the hourglass beginning
at row `1`, column `2`:

```text
0 4 3
1
8 6 6
````

**Note**: If you have already solved the Java domain's Java 2D Array challenge,
you may wish to skip this challenge.

## Function Description

Complete the function hourglassSum in the editor below.

hourglassSum has the following parameter(s):

- `int arr[6][6]`: an array of integers

## Returns

- int: the maximum hourglass sum

## Input Format

Each of the `6` lines of inputs `arr[i]` contains space-separated integers `arr[i][j]`.

## Constraints

- $9 \leq arr[i][j] \leq 9$
- $0 \leq i, j \leq 5$

## Output Format

Print the largest (maximum) hourglass sum found in `arr`.

## Sample Input

```text
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
```

## Sample Output

```text
19
```

## Explanation

`arr` contains the following hourglasses:

```text
111 110 100 000
1 0 0 0
111 110 100 000

010 100 000 000
0 1 0 0
002 024 244 440

111 110 100 000
0 2 4 4
000 002 020 200

002 024 244 440
0 0 2 0
001 012 124 240
```

The hourglass with the maximum sum (`19`) is:

```text
2 4 4
2
1 2 4
```
45 changes: 45 additions & 0 deletions src/hackerrank/interview_preparation_kit/arrays/two_d_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# @link Problem definition
# [[docs/hackerrank/interview_preparation_kit/arrays/two_d_array.md]]

import logging

LOGGER = logging.getLogger(__name__)


def gethourglass(arr: list[list[int]], position_x: int, position_y: int) -> list[int]:
result: list[int] = []

# top
result.append(arr[position_x - 1][position_y - 1])
result.append(arr[position_x - 1][position_y])
result.append(arr[position_x - 1][position_y + 1])

# middle
result.append(arr[position_x][position_y])

# bottom
result.append(arr[position_x + 1][position_y - 1])
result.append(arr[position_x + 1][position_y])
result.append(arr[position_x + 1][position_y + 1])

return result


def hourglass_sum(arr: list[list[int]]) -> int | None:
matrix_size: int = len(arr)

matrix_start_index: int = 1
matrix_stop_index: int = matrix_size - 1

max_hourglass_sum: int | None = None

for i in range(matrix_start_index, matrix_stop_index):
for j in range(matrix_start_index, matrix_stop_index):

houglass_values: list[int] = gethourglass(arr, i, j)
this_hourglass_sum: int = sum(houglass_values)

if max_hourglass_sum is None or this_hourglass_sum > max_hourglass_sum:
max_hourglass_sum = this_hourglass_sum

return max_hourglass_sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"title": "Sample Test Case 0",
"input": [
[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]
],
"expected": 19
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
from pathlib import Path

from ....hackerrank.lib.loader import load_test_cases
from .two_d_array import hourglass_sum

FILE_PATH = str(Path(__file__).resolve().parent)

TEST_CASES = load_test_cases(
FILE_PATH + '/two_d_array.testcases.json')


class Test2dArray(unittest.TestCase):

def test_hourglass_sum(self):
for _, _tt in enumerate(TEST_CASES):

self.assertEqual(
hourglass_sum(_tt['input']), _tt['expected'],
f"{_} | hourglass_sum({_tt['input']}) must be "
f"=> {_tt['expected']}")
Loading