Skip to content

Commit b602a23

Browse files
authored
Merge pull request #839 from sir-gon/develop
Develop
2 parents a727ad4 + e535816 commit b602a23

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it } from '@jest/globals';
22
import { logger as console } from '../../../logger';
33

4-
import { maxSubsetSum } from './max_array_sum';
4+
import { maxSubsetSum, bigIntMax } from './max_array_sum';
55

66
import TEST_CASES from './max_array_sum.testcases.json';
77
import TEST_CASE3 from './max_array_sum.testcase3.json';
@@ -10,6 +10,29 @@ const ALL_TEST_CASES = [...TEST_CASES, TEST_CASE3];
1010

1111
const DECIMAL_RADIX = 10;
1212

13+
describe('bigIntMax', () => {
14+
it('bigIntMax test cases', () => {
15+
expect.assertions(1);
16+
17+
const inputs = [1n, 3n, 2n, 5n, 4n];
18+
const expected = 5n;
19+
20+
const answer = bigIntMax(...inputs);
21+
22+
console.debug(`bigIntMax(${inputs.toString()}) solution found: ${answer}`);
23+
24+
expect(answer).toStrictEqual(expected);
25+
});
26+
27+
it('bigIntMax edge case', () => {
28+
expect.assertions(1);
29+
30+
expect(() => {
31+
bigIntMax();
32+
}).toThrow('bigIntMax requires at least one argument.');
33+
});
34+
});
35+
1336
describe('max_array_sum', () => {
1437
it('maxSubsetSum test cases', () => {
1538
expect.assertions(5);

src/hackerrank/interview_preparation_kit/dynamic_programming/max_array_sum.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
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) => {
8-
const _e = BigInt(e);
9-
const _m = BigInt(m);
10-
return _e > _m ? _e : _m;
11-
}, BigInt(0));
6+
const bigIntMax = (...args: bigint[]): bigint => {
7+
if (args.length === 0) {
8+
throw new Error('bigIntMax requires at least one argument.');
9+
}
10+
return args.reduce((max, current) => {
11+
if (current > max) {
12+
return current;
13+
}
14+
return max;
15+
}, 0n);
16+
};
1217

1318
function maxSubsetSum(arr: number[]): number {
14-
const arrCopy: bigint[] = arr.map((x: number): bigint => BigInt(x));
19+
const arrCopy: bigint[] = arr.map(BigInt);
1520

1621
if (arrCopy.length === 0) {
1722
return 0;
@@ -36,4 +41,4 @@ function maxSubsetSum(arr: number[]): number {
3641
}
3742

3843
export default { maxSubsetSum };
39-
export { maxSubsetSum };
44+
export { maxSubsetSum, bigIntMax };

0 commit comments

Comments
 (0)