Skip to content

Commit 2bcf385

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

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger';
3+
4+
import { maxSubsetSum } from './max_array_sum';
5+
6+
const TEST_CASES = [
7+
{
8+
title: 'Sample Test case 0',
9+
input: [3, 7, 4, 6, 5],
10+
expected: 13
11+
},
12+
{
13+
title: 'Sample Test case 1',
14+
input: [2, 1, 5, 8, 4],
15+
expected: 11
16+
},
17+
{
18+
title: 'Sample Test case 2',
19+
input: [3, 5, -7, 8, 10],
20+
expected: 15
21+
}
22+
];
23+
24+
describe('max_array_sum', () => {
25+
it('maxSubsetSum test cases', () => {
26+
expect.assertions(3);
27+
28+
TEST_CASES.forEach((test) => {
29+
const answer = maxSubsetSum(test.input);
30+
31+
console.debug(`maxSubsetSum(${test.input}) solution found: ${answer}`);
32+
33+
expect(answer).toStrictEqual(test.expected);
34+
});
35+
});
36+
37+
it('maxSubsetSum edge case zero', () => {
38+
expect.assertions(1);
39+
40+
const input: number[] = [];
41+
const expected: number = 0;
42+
43+
const answer = maxSubsetSum(input);
44+
45+
console.debug(`maxSubsetSum(${input}) solution found: ${answer}`);
46+
47+
expect(answer).toStrictEqual(expected);
48+
});
49+
50+
it('maxSubsetSum edge case one', () => {
51+
expect.assertions(1);
52+
53+
const input: number[] = [1];
54+
const expected: number = 1;
55+
56+
const answer = maxSubsetSum(input);
57+
58+
console.debug(`maxSubsetSum(${input}) solution found: ${answer}`);
59+
60+
expect(answer).toStrictEqual(expected);
61+
});
62+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
export function maxSubsetSum(arr: number[]): number {
7+
const arrCopy = [...arr];
8+
9+
if (arrCopy.length == 0) {
10+
return 0;
11+
}
12+
13+
const total = arrCopy.length;
14+
15+
if (total == 1) {
16+
return arrCopy[0];
17+
}
18+
19+
let t_max = Math.max(arrCopy[0], arrCopy[1]);
20+
arrCopy[1] = t_max;
21+
22+
for (let i = 2; i < total; i++) {
23+
t_max = Math.max(arr[i - 2] + arr[i], t_max);
24+
t_max = Math.max(arr[i], t_max);
25+
arr[i] = t_max;
26+
}
27+
28+
return t_max;
29+
}
30+
31+
export default { maxSubsetSum };

0 commit comments

Comments
 (0)