Skip to content

Commit c6a88c0

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

File tree

6 files changed

+228
-0
lines changed

6 files changed

+228
-0
lines changed
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`
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`
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.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.md]]
3+
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum-solution-notes.md]]
4+
*/
5+
6+
const bigIntMax = (...args) =>
7+
args.reduce((m, e) => (e > m ? e : m), BigInt(0));
8+
9+
export function maxSubsetSum(arr) {
10+
const arrCopy = arr.map((x) => BigInt(x));
11+
12+
if (arrCopy.length === 0) {
13+
return 0;
14+
}
15+
16+
const total = arrCopy.length;
17+
18+
if (total === 1) {
19+
return Number(arrCopy[0]);
20+
}
21+
22+
let tMax = bigIntMax(arrCopy[0], arrCopy[1]);
23+
arrCopy[1] = tMax;
24+
25+
for (let i = 2; i < total; i++) {
26+
tMax = bigIntMax(arrCopy[i - 2] + arrCopy[i], tMax);
27+
tMax = bigIntMax(arrCopy[i], tMax);
28+
arrCopy[i] = tMax;
29+
}
30+
31+
return Number(tMax);
32+
}
33+
34+
export default { maxSubsetSum };
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger.js';
3+
4+
import { maxSubsetSum } from './max_array_sum.js';
5+
6+
import TEST_CASES from './max_array_sum.testcases.json';
7+
import TEST_CASE3 from './max_array_sum.testcase3.json';
8+
9+
const ALL_TEST_CASES = [...TEST_CASES, TEST_CASE3];
10+
11+
const DECIMAL_RADIX = 10;
12+
13+
describe('max_array_sum', () => {
14+
it('maxSubsetSum test cases', () => {
15+
expect.assertions(4);
16+
17+
ALL_TEST_CASES.forEach((test) => {
18+
const answer = maxSubsetSum(test.input).toString(DECIMAL_RADIX);
19+
20+
console.debug(`maxSubsetSum(${test.input}) solution found: ${answer}`);
21+
22+
expect(answer).toStrictEqual(test.expected);
23+
});
24+
});
25+
26+
it('maxSubsetSum edge case zero', () => {
27+
expect.assertions(1);
28+
29+
const input = [];
30+
const expected = 0;
31+
32+
const answer = maxSubsetSum(input);
33+
34+
console.debug(`maxSubsetSum(${input}) solution found: ${answer}`);
35+
36+
expect(answer).toStrictEqual(expected);
37+
});
38+
39+
it('maxSubsetSum edge case one', () => {
40+
expect.assertions(1);
41+
42+
const input = [1];
43+
const expected = 1;
44+
45+
const answer = maxSubsetSum(input);
46+
47+
console.debug(`maxSubsetSum(${input}) solution found: ${answer}`);
48+
49+
expect(answer).toStrictEqual(expected);
50+
});
51+
});

src/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.testcase3.json

Lines changed: 5 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 17 additions & 0 deletions
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+
]

0 commit comments

Comments
 (0)