diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/TwoDarray.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/TwoDarray.java new file mode 100644 index 0000000..22beca9 --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/TwoDarray.java @@ -0,0 +1,66 @@ +package ae.hackerrank.interview_preparation_kit.arrays; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +/** + * 2D Array - DS. + * + * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/2d_array.md]] + */ + +public class TwoDarray { + private TwoDarray() { } + + private static List getHourGlass(List> arr, int positionX, int positionY) { + List result = new ArrayList<>(); + + // top + result.add(arr.get(positionX - 1).get(positionY - 1)); + result.add(arr.get(positionX - 1).get(positionY)); + result.add(arr.get(positionX - 1).get(positionY + 1)); + + // middle + result.add(arr.get(positionX).get(positionY)); + + // bottom + result.add(arr.get(positionX + 1).get(positionY - 1)); + result.add(arr.get(positionX + 1).get(positionY)); + result.add(arr.get(positionX + 1).get(positionY + 1)); + + return result; + } + + /** + * hourglassSum. + */ + public static Integer hourglassSum(List> arr) { + int matrixSize = 0; + + if (arr == null) { + return null; + } + + matrixSize = arr.size(); + + int matrixStartIndex = 1; + int matrixEndIndex = matrixSize - 2; + + Integer maxHourGlassSum = null; + + for (int i = matrixStartIndex; i <= matrixEndIndex; i++) { + for (int j = matrixStartIndex; j <= matrixEndIndex; j++) { + List currentHourGlass = getHourGlass(arr, i, j); + int hourGlassSum = currentHourGlass.stream() + .collect(Collectors.summingInt(Integer::intValue)); + + if (maxHourGlassSum == null || hourGlassSum > maxHourGlassSum) { + maxHourGlassSum = hourGlassSum; + } + } + } + + return maxHourGlassSum; + } +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/TwoDarrayTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/TwoDarrayTest.java new file mode 100644 index 0000000..6d7ea80 --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/TwoDarrayTest.java @@ -0,0 +1,76 @@ +package ae.hackerrank.interview_preparation_kit.arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; +import util.JsonLoader; + + +@TestInstance(Lifecycle.PER_CLASS) +class TwoDarrayTest { + + public static class TwoDarrayTestCase { + public String title; + public List> input; + public long expected; + } + + private List testCases; + + @BeforeAll + public void setup() throws IOException { + String path = String.join("/", "hackerrank", + "interview_preparation_kit", + "arrays", + "2d_array.testcases.json"); + + this.testCases = JsonLoader.loadJson(path, TwoDarrayTestCase.class); + } + + + @Test void testHourglassSum() { + for (TwoDarrayTestCase testCase : testCases) { + long solutionFound = TwoDarray.hourglassSum(testCase.input); + + assertEquals(testCase.expected, solutionFound, + "%s(%s) answer must be: %s".formatted( + "TwoDarray.hourglassSum", + testCase.input.toString(), + testCase.expected + ) + ); + } + } + + @Test void testHourglassSumEdgeCases() { + List> input = null; + Integer expected = null; + Integer solutionFound = TwoDarray.hourglassSum(null); + assertEquals(expected, solutionFound, + "%s(%s) answer must be: %s".formatted( + "TwoDarray.hourglassSum", + input, + expected + ) + ); + + input = new ArrayList>(); + expected = null; + solutionFound = TwoDarray.hourglassSum(null); + + assertEquals(expected, solutionFound, + "%s(%s) answer must be: %s".formatted( + "TwoDarray.hourglassSum", + input, + expected + ) + ); + } +} + diff --git a/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/2d_array.testcases.json b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/2d_array.testcases.json new file mode 100644 index 0000000..f32bdef --- /dev/null +++ b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/2d_array.testcases.json @@ -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 + } +] diff --git a/docs/hackerrank/interview_preparation_kit/arrays/2d_array.md b/docs/hackerrank/interview_preparation_kit/arrays/2d_array.md new file mode 100644 index 0000000..9c92ffe --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/arrays/2d_array.md @@ -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 +```