Skip to content

Commit 79f01aa

Browse files
author
Gonzalo Diaz
committed
[BUGFIX] [Hacker Rank] Interview Preparation Kit: Dynamic Programming: Max Array Sum. FIXED ✅.
* BigInt required for big cases * New test case added * Test cases data moved to JSON.
1 parent 2bcf385 commit 79f01aa

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed

src/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.test.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,19 @@ import { logger as console } from '../../../logger';
33

44
import { maxSubsetSum } from './max_array_sum';
55

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-
];
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;
2312

2413
describe('max_array_sum', () => {
2514
it('maxSubsetSum test cases', () => {
26-
expect.assertions(3);
15+
expect.assertions(4);
2716

28-
TEST_CASES.forEach((test) => {
29-
const answer = maxSubsetSum(test.input);
17+
ALL_TEST_CASES.forEach((test) => {
18+
const answer = maxSubsetSum(test.input).toString(DECIMAL_RADIX);
3019

3120
console.debug(`maxSubsetSum(${test.input}) solution found: ${answer}`);
3221

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+
]

src/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum-solution-notes.md]]
44
*/
55

6+
const bigIntMax = (...args: bigint[]): bigint =>
7+
args.reduce((m, e) => (e > m ? e : m), BigInt(0));
8+
69
export function maxSubsetSum(arr: number[]): number {
7-
const arrCopy = [...arr];
10+
const arrCopy: bigint[] = arr.map((x: number): bigint => BigInt(x));
811

912
if (arrCopy.length == 0) {
1013
return 0;
@@ -13,19 +16,19 @@ export function maxSubsetSum(arr: number[]): number {
1316
const total = arrCopy.length;
1417

1518
if (total == 1) {
16-
return arrCopy[0];
19+
return Number(arrCopy[0]);
1720
}
1821

19-
let t_max = Math.max(arrCopy[0], arrCopy[1]);
22+
let t_max = bigIntMax(arrCopy[0], arrCopy[1]);
2023
arrCopy[1] = t_max;
2124

2225
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+
t_max = bigIntMax(arrCopy[i - 2] + arrCopy[i], t_max);
27+
t_max = bigIntMax(arrCopy[i], t_max);
28+
arrCopy[i] = t_max;
2629
}
2730

28-
return t_max;
31+
return Number(t_max);
2932
}
3033

3134
export default { maxSubsetSum };

0 commit comments

Comments
 (0)