Skip to content
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/2d_arrays.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
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <vector>

using std::vector;

namespace hackerrank::interview_preparation_kit::arrays {

int hourglassSum(const vector<vector<int>> &arr);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

#include <exercises/hackerrank/interview_preparation_kit/arrays/2d_arrays.hpp>

#include <vector>
using std::vector;

/**
* @link Problem definition
* [[docs/hackerrank/interview_preparation_kit/arrays/2d_arrays.md]]
*/

namespace hackerrank::interview_preparation_kit::arrays {

int hourglassSum(const vector<vector<int>> &arr) {
int max_sum = -63; // Minimum possible hourglass sum (-9 * 7)
for (int i = 0; i <= arr.size() - 3; i++) {
for (int j = 0; j <= arr[0].size() - 3; j++) {
int current_sum =
// top
arr[i][j] + arr[i][j + 1] + arr[i][j + 2] +
// middle
arr[i + 1][j + 1] +
// bottom
arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
if (current_sum > max_sum) {
max_sum = current_sum;
}
}
}
return max_sum;
}

} // namespace hackerrank::interview_preparation_kit::arrays
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 3rd party libs
#include <catch2/catch_test_macros.hpp>
#include <nlohmann/json.hpp>

// local libs
#include <exercises/hackerrank/interview_preparation_kit/arrays/2d_arrays.hpp>

// std libs
#include <filesystem>
#include <fstream>
#include <vector>

using json = nlohmann::json;

TEST_CASE("hourglassSum JSON Test Cases",
"[hackerrank] [jsontestcase] [arrays]") {
std::filesystem::path path = std::filesystem::path(__FILE__).parent_path() /
"2d_arrays.testcases.json";

INFO("hourglassSum JSON test cases FILE: " << path.string());

std::ifstream f(path);
json data = json::parse(f);

for (auto testcase : data) {
std::vector<std::vector<int>> matrix = testcase["input"];
std::vector<int *> rows;
rows.reserve(matrix.size());

std::transform(
matrix.begin(), matrix.end(), std::back_inserter(rows),
[](const auto &row) { return const_cast<int *>(row.data()); });

long long result =
hackerrank::interview_preparation_kit::arrays::hourglassSum(matrix);

CHECK(result == testcase["expected"]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"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
},
{
"title": "Sample Test Case 1",
"input": [
[1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[0, 9, 2, -4, -4, 0],
[0, 0, 0, -2, 0, 0],
[0, 0, -1, -2, -4, 0]
],
"expected": 13
},
{
"title": "Sample Test Case 2",
"input": [
[-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]
],
"expected": 28
}
]
Loading