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
Original file line number Diff line number Diff line change
@@ -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<Integer> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer> input;
public Integer expected;
}

private final List<MaxArraySumTestCase> 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<Integer> 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<Integer> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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
}
]
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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.
Loading