Skip to content

Commit

Permalink
Merge pull request #277 from sir-gon/feature/2d_array
Browse files Browse the repository at this point in the history
[Hacker Rank] Interview Preparation Kit: Array: 2D Array - DS. Solved ✅.
  • Loading branch information
sir-gon authored Jan 25, 2025
2 parents 7f37496 + c3c84b1 commit 743b8d3
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 0 deletions.
135 changes: 135 additions & 0 deletions docs/hackerrank/warmup/interview_preparation_kit/2d_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
```
50 changes: 50 additions & 0 deletions exercises/hackerrank/interview_preparation_kit/arrays/2d_array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @link Problem definition [[docs/hackerrank/warmup/interview_preparation_kit/2d_array.md]]
*/

package hackerrank

func getHourGlass(arr [][]int32, positionX int32, positionY int32) []int32 {
result := []int32{}

// top
result = append(result, arr[positionX-1][positionY-1])
result = append(result, arr[positionX-1][positionY])
result = append(result, arr[positionX-1][positionY+1])

// middle
result = append(result, arr[positionX][positionY])

// bottom
result = append(result, arr[positionX+1][positionY-1])
result = append(result, arr[positionX+1][positionY])
result = append(result, arr[positionX+1][positionY+1])

return result
}

func hourglassSum(arr [][]int32) int32 {
// Write your code here
matrixSize := len(arr)

matrixStartIndex := 1
matrixEndIndex := matrixSize - 2

var maxHourglassSum int32 = 0

for i := int32(matrixStartIndex); i <= int32(matrixEndIndex); i++ {
for j := int32(matrixStartIndex); j <= int32(matrixEndIndex); j++ {
var currentHourglassSum int32 = 0
currentHourglass := getHourGlass(arr, i, j)
for k := 0; k < len(currentHourglass); k++ {
currentHourglassSum += currentHourglass[k]
}

if currentHourglassSum > maxHourglassSum {
maxHourglassSum = currentHourglassSum
}
}
}

return maxHourglassSum
}
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,44 @@
package hackerrank

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
"gon.cl/algorithms/utils"
)

type TestCase struct {
Input [][]int32 `json:"input"`
Expected int32 `json:"expected"`
}

var testCases []TestCase

// You can use testing.T, if you want to test the code without benchmarking
func setupSuite(t testing.TB) {
wd, _ := os.Getwd()
filepath := wd + "/2d_array.testcases.json"
t.Log("Setup test cases from JSON: ", filepath)

var _, err = utils.LoadJSON(filepath, &testCases)
if err != nil {
t.Log(err)
}
}

func Test2DArray(t *testing.T) {

setupSuite(t)

for _, tt := range testCases {
testname := fmt.Sprintf("hourglassSum(%v) => %v \n", tt.Input, tt.Expected)
t.Run(testname, func(t *testing.T) {

ans := hourglassSum(tt.Input)
assert.Equal(t, tt.Expected, ans)
})

}
}

0 comments on commit 743b8d3

Please sign in to comment.