Skip to content

Commit bb1c22b

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Dynamic Programming: Max Array Sum. Solved ✓.
1 parent c174706 commit bb1c22b

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// @link Problem definition
2+
// [[docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.md]]
3+
4+
package ae.hackerrank.interview_preparation_kit.dynamic_programming;
5+
6+
import java.util.List;
7+
8+
public class MaxArraySum {
9+
private MaxArraySum() {}
10+
11+
public static int maxSubsetSum(List<Integer> arr) {
12+
int total = arr.size();
13+
14+
if (total == 0) { // Edge case
15+
return 0;
16+
}
17+
18+
if (total == 1) { // Edge case
19+
return arr.get(0);
20+
}
21+
22+
// Base case start from index 0 and 1
23+
int tMax = Math.max(arr.get(0), arr.get(1));
24+
arr.set(1, tMax);
25+
26+
for (int i = 2; i < total; i++) {
27+
tMax = Math.max(arr.get(i - 2) + arr.get(i), tMax); // Max uptill now
28+
tMax = Math.max(arr.get(i), tMax); // Max in special case where
29+
// arr[i] + previous max is still less than arr[i]
30+
// update our inplace array with max for future calculations
31+
arr.set(i, tMax);
32+
}
33+
34+
return tMax;
35+
}
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ae.hackerrank.interview_preparation_kit.dynamic_programming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import org.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.TestInstance;
11+
import org.junit.jupiter.api.TestInstance.Lifecycle;
12+
import util.JsonLoader;
13+
14+
/** MaxArraySumTest. */
15+
@TestInstance(Lifecycle.PER_CLASS)
16+
class MaxArraySumTest {
17+
public static class MaxArraySumTestCase {
18+
public String title;
19+
public List<Integer> input;
20+
public Integer expected;
21+
}
22+
23+
private final List<MaxArraySumTestCase> testCases = new ArrayList<>();
24+
25+
@BeforeAll
26+
void setup() throws IOException {
27+
String path =
28+
String.join(
29+
"/",
30+
"hackerrank",
31+
"interview_preparation_kit",
32+
"dynamic_programming",
33+
"max_array_sum.testcases.json");
34+
35+
this.testCases.clear();
36+
this.testCases.addAll(JsonLoader.loadJson(path, MaxArraySumTestCase.class));
37+
}
38+
39+
@Test
40+
void testMaxArraySum() {
41+
for (MaxArraySumTestCase test : testCases) {
42+
int solutionFound = MaxArraySum.maxSubsetSum(test.input);
43+
44+
assertEquals(
45+
test.expected,
46+
solutionFound,
47+
"%s(%s) answer must be: %d"
48+
.formatted("MaxArraySum.maxSubsetSum", test.input.toString(), test.expected));
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"title": "Sample Test case 0",
4+
"input": [3, 7, 4, 6, 5],
5+
"expected": 13
6+
},
7+
{
8+
"title": "Sample Test case 1",
9+
"input": [2, 1, 5, 8, 4],
10+
"expected": 11
11+
},
12+
{
13+
"title": "Sample Test case 2",
14+
"input": [3, 5, -7, 8, 10],
15+
"expected": 15
16+
}
17+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [Dynamic Programming: Max Array Sum](https://www.hackerrank.com/challenges/max-array-sum)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#ProblemSolvingIntermediate` `#dynamicprogramming`
5+
6+
## Sources
7+
8+
- [Max Array Sum — HackerRank Medium Using Inplace Dynamic Programming](https://iawale.medium.com/max-array-sum-hackerrank-medium-using-inplace-dynamic-programming-215a620d7705)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# [Dynamic Programming: Max Array Sum](https://www.hackerrank.com/challenges/max-array-sum)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#ProblemSolvingIntermediate` `#dynamicprogramming`
5+
6+
Given an array of integers, find the subset of
7+
non-adjacent elements with the maximum sum.
8+
Calculate the sum of that subset.
9+
It is possible that the maximum sum is `0`, the case when all elements are negative.
10+
11+
## Example
12+
13+
`arr = [-2, 1, 3, -4, 5]`
14+
15+
The following subsets with more than element exist.
16+
These exclude the empty subset and single element subsets which are also valid.
17+
18+
```text
19+
Subset Sum
20+
[-2, 3, 5] 6
21+
[-2, 3] 1
22+
[-2, -4] -6
23+
[-2, 5] 3
24+
[1, -4] -3
25+
[1, 5] 6
26+
[3, 5] 8
27+
```
28+
29+
The maximum subset sum is . Note that any individual element is a subset as well.
30+
31+
`arr = [-2, -3, -1]`
32+
33+
In this case, it is best to choose no element: return `0`.
34+
35+
## Function Description
36+
37+
Complete the `maxSubsetSum` function in the editor below.
38+
39+
maxSubsetSum has the following parameter(s):
40+
41+
- `int arr[n]`: an array of integers
42+
43+
## Returns
44+
45+
- `int`: the maximum subset sum
46+
47+
## Input Format
48+
49+
The first line contains an integer, `n`.
50+
The second line contains `n` space-separated integers `arr[i]`.
51+
52+
## Constraints
53+
54+
- $ 1 \leq n \leq 10^5 $
55+
- $ -10^4 \leq arr[i] \leq 10^4 $
56+
57+
## Sample Input 0
58+
59+
```text
60+
5
61+
3 7 4 6 5
62+
```
63+
64+
## Sample Output 0
65+
66+
```text
67+
13
68+
```
69+
70+
## Explanation 0
71+
72+
Our possible subsets are `[3, 4, 5]`. `[3, 4]`, `[3, 6]`, `[3, 5]`, `[7, 6]`,
73+
`[7, 5]` and `[4, 5]`.
74+
The largest subset sum is `13` from subset `[7, 6]`
75+
76+
## Sample Input 1
77+
78+
```text
79+
5
80+
2 1 5 8 4
81+
```
82+
83+
## Sample Output 1
84+
85+
```text
86+
11
87+
```
88+
89+
## Explanation 1
90+
91+
Our subsets are `[2, 5, 4]`, `[2, 5]`, `[2, 8]`, `[2, 4]`, `[1, 8]`,
92+
`[1, 4]` and `[5, 4]`.
93+
The maximum subset sum is `11` from the first subset listed.
94+
95+
## Sample Input 2
96+
97+
```text
98+
5
99+
3 5 -7 8 10
100+
```
101+
102+
## Sample Output 2
103+
104+
```text
105+
15
106+
```
107+
108+
## Explanation 2
109+
110+
Our subsets are `[3, -7, 10]`, `[3, 8]`, `[3,10]`, `[5, 8]`,
111+
`[5, 10]` and `[-7, 10]`.
112+
113+
The maximum subset sum is `15` from the fifth subset listed.

0 commit comments

Comments
 (0)