From f792db5ff41573d12f8a40c9ac3b4dd499285b65 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Thu, 12 Mar 2026 18:30:36 -0300 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Dynamic=20Programming:=20Max=20Array=20Sum.=20Solved=20?= =?UTF-8?q?=E2=9C=93.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dynamic_programming/MaxArraySum.java | 45 +++++++ .../dynamic_programming/MaxArraySumTest.java | 77 ++++++++++++ .../max_array_sum.testcases.json | 17 +++ .../max_array_sum-solution-notes.md | 8 ++ .../dynamic_programming/max_array_sum.md | 113 ++++++++++++++++++ 5 files changed, 260 insertions(+) create mode 100644 algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dynamic_programming/MaxArraySum.java create mode 100644 algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/dynamic_programming/MaxArraySumTest.java create mode 100644 algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.testcases.json create mode 100644 docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum-solution-notes.md create mode 100644 docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.md diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dynamic_programming/MaxArraySum.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dynamic_programming/MaxArraySum.java new file mode 100644 index 00000000..0593bbc1 --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dynamic_programming/MaxArraySum.java @@ -0,0 +1,45 @@ +// @link Problem definition +// [[docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.md]] + +package ae.hackerrank.interview_preparation_kit.dynamic_programming; + +import java.util.List; + +/** + * MaxArraySum. + * + * @link Problem definition + * [[docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.md]] + * @link Solution notes + * [[docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum-solution-notes.md]] + */ +public class MaxArraySum { + private MaxArraySum() {} + + /** maxSubsetSum. */ + public static int maxSubsetSum(List arr) { + int total = arr.size(); + + if (total == 0) { // Edge case + return 0; + } + + if (total == 1) { // Edge case + return arr.get(0); + } + + // Base case start from index 0 and 1 + int tMax = Math.max(arr.get(0), arr.get(1)); + arr.set(1, tMax); + + for (int i = 2; i < total; i++) { + tMax = Math.max(arr.get(i - 2) + arr.get(i), tMax); // Max uptill now + tMax = Math.max(arr.get(i), tMax); // Max in special case where + // arr[i] + previous max is still less than arr[i] + // update our inplace array with max for future calculations + arr.set(i, tMax); + } + + return tMax; + } +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/dynamic_programming/MaxArraySumTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/dynamic_programming/MaxArraySumTest.java new file mode 100644 index 00000000..9d33c0c5 --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/dynamic_programming/MaxArraySumTest.java @@ -0,0 +1,77 @@ +package ae.hackerrank.interview_preparation_kit.dynamic_programming; + +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; + +/** MaxArraySumTest. */ +@TestInstance(Lifecycle.PER_CLASS) +class MaxArraySumTest { + public static class MaxArraySumTestCase { + public String title; + public List input; + public Integer expected; + } + + private final List testCases = new ArrayList<>(); + + @BeforeAll + void setup() throws IOException { + String path = + String.join( + "/", + "hackerrank", + "interview_preparation_kit", + "dynamic_programming", + "max_array_sum.testcases.json"); + + this.testCases.clear(); + this.testCases.addAll(JsonLoader.loadJson(path, MaxArraySumTestCase.class)); + } + + @Test + void testMaxArraySum() { + for (MaxArraySumTestCase test : testCases) { + int solutionFound = MaxArraySum.maxSubsetSum(test.input); + + assertEquals( + test.expected, + solutionFound, + "%s(%s) answer must be: %d" + .formatted("MaxArraySum.maxSubsetSum", test.input.toString(), test.expected)); + } + } + + @Test + void testMaxArraySumZero() { + List testInput = List.of(); + int testExpected = 0; + int solutionFound = MaxArraySum.maxSubsetSum(testInput); + + assertEquals( + testExpected, + solutionFound, + "%s(%s) answer must be: %d" + .formatted("MaxArraySum.maxSubsetSum", testInput.toString(), testExpected)); + } + + @Test + void testMaxArraySumSingle() { + List testInput = List.of(1); + int testExpected = 1; + int solutionFound = MaxArraySum.maxSubsetSum(testInput); + + assertEquals( + testExpected, + solutionFound, + "%s(%s) answer must be: %d" + .formatted("MaxArraySum.maxSubsetSum", testInput.toString(), testExpected)); + } +} diff --git a/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.testcases.json b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.testcases.json new file mode 100644 index 00000000..cdc02e4f --- /dev/null +++ b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.testcases.json @@ -0,0 +1,17 @@ +[ + { + "title": "Sample Test case 0", + "input": [3, 7, 4, 6, 5], + "expected": 13 + }, + { + "title": "Sample Test case 1", + "input": [2, 1, 5, 8, 4], + "expected": 11 + }, + { + "title": "Sample Test case 2", + "input": [3, 5, -7, 8, 10], + "expected": 15 + } +] diff --git a/docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum-solution-notes.md b/docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum-solution-notes.md new file mode 100644 index 00000000..c197c7ed --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum-solution-notes.md @@ -0,0 +1,8 @@ +# [Dynamic Programming: Max Array Sum](https://www.hackerrank.com/challenges/max-array-sum) + +- Difficulty: `#medium` +- Category: `#ProblemSolvingIntermediate` `#dynamicprogramming` + +## Sources + +- [Max Array Sum — HackerRank Medium Using Inplace Dynamic Programming](https://iawale.medium.com/max-array-sum-hackerrank-medium-using-inplace-dynamic-programming-215a620d7705) diff --git a/docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.md b/docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.md new file mode 100644 index 00000000..fda4faa0 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.md @@ -0,0 +1,113 @@ +# [Dynamic Programming: Max Array Sum](https://www.hackerrank.com/challenges/max-array-sum) + +- Difficulty: `#medium` +- Category: `#ProblemSolvingIntermediate` `#dynamicprogramming` + +Given an array of integers, find the subset of +non-adjacent elements with the maximum sum. +Calculate the sum of that subset. +It is possible that the maximum sum is `0`, the case when all elements are negative. + +## Example + +`arr = [-2, 1, 3, -4, 5]` + +The following subsets with more than element exist. +These exclude the empty subset and single element subsets which are also valid. + +```text +Subset Sum +[-2, 3, 5] 6 +[-2, 3] 1 +[-2, -4] -6 +[-2, 5] 3 +[1, -4] -3 +[1, 5] 6 +[3, 5] 8 +``` + +The maximum subset sum is . Note that any individual element is a subset as well. + +`arr = [-2, -3, -1]` + +In this case, it is best to choose no element: return `0`. + +## Function Description + +Complete the `maxSubsetSum` function in the editor below. + +maxSubsetSum has the following parameter(s): + +- `int arr[n]`: an array of integers + +## Returns + +- `int`: the maximum subset sum + +## Input Format + +The first line contains an integer, `n`. +The second line contains `n` space-separated integers `arr[i]`. + +## Constraints + +- $ 1 \leq n \leq 10^5 $ +- $ -10^4 \leq arr[i] \leq 10^4 $ + +## Sample Input 0 + +```text +5 +3 7 4 6 5 +``` + +## Sample Output 0 + +```text +13 +``` + +## Explanation 0 + +Our possible subsets are `[3, 4, 5]`. `[3, 4]`, `[3, 6]`, `[3, 5]`, `[7, 6]`, +`[7, 5]` and `[4, 5]`. +The largest subset sum is `13` from subset `[7, 6]` + +## Sample Input 1 + +```text +5 +2 1 5 8 4 +``` + +## Sample Output 1 + +```text +11 +``` + +## Explanation 1 + +Our subsets are `[2, 5, 4]`, `[2, 5]`, `[2, 8]`, `[2, 4]`, `[1, 8]`, +`[1, 4]` and `[5, 4]`. +The maximum subset sum is `11` from the first subset listed. + +## Sample Input 2 + +```text +5 +3 5 -7 8 10 +``` + +## Sample Output 2 + +```text +15 +``` + +## Explanation 2 + +Our subsets are `[3, -7, 10]`, `[3, 8]`, `[3,10]`, `[5, 8]`, +`[5, 10]` and `[-7, 10]`. + +The maximum subset sum is `15` from the fifth subset listed.