Skip to content

Commit 3bf8035

Browse files
committed
Updated maxSubArraySum.
1 parent 5e5e44b commit 3bf8035

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

numbers/list/max-array-sum/index.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,41 @@
44
*/
55

66
const maxSubArraySum = (arr, size) => {
7-
if (arr.length === 0 || size > arr.length) return null;
8-
let i = 0;
9-
let j = size - 1;
10-
let sum = -Infinity;
7+
// if (arr.length === 0 || size > arr.length) return null;
8+
// let i = 0;
9+
// let j = size - 1;
10+
// let sum = -Infinity;
11+
12+
// while (j < arr.length) {
13+
// const total = arr.slice(i, j+1);
14+
// const sumTotal = total.reduce((a, b) => a + b);
15+
// if (sumTotal > sum) {
16+
// sum = sumTotal;
17+
// }
18+
// i++;
19+
// j++;
20+
// }
21+
// return sum;
1122

12-
while (j < arr.length) {
13-
const total = arr.slice(i, j+1);
14-
const sumTotal = total.reduce((a, b) => a + b);
15-
if (sumTotal > sum) {
16-
sum = sumTotal;
17-
}
23+
// Another solution
24+
// This solutions performance is O(n)
25+
if (arr.length === 0 || size > arr.length) return null;
26+
let tempSum = 0;
27+
// Add the first size elements of the array
28+
for (let i = 0; i < size; i++) {
29+
tempSum += arr[i];
30+
}
31+
let maxSum = tempSum;
32+
let i = size;
33+
// Loop through the elements from the size of the array
34+
while (i < arr.length) {
35+
// Minus the first element and add the size position of the array
36+
tempSum = tempSum - arr[i - size] + arr[i];
37+
maxSum = Math.max(maxSum, tempSum);
1838
i++;
19-
j++;
2039
}
21-
return sum;
40+
return maxSum;
2241
};
2342

43+
2444
module.exports = maxSubArraySum;

numbers/list/max-array-sum/test.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
*/
55
const maxSubArraySum = require('./index');
66
test('maxSubArraySum is a function', () => {
7-
expect(typeof maxSubArraySum).toEqual('function');
7+
expect(typeof maxSubArraySum).toEqual('function');
88
});
99
test('maxSubArraySum [8,2,54,65,23,7,45] to give 109', () => {
10-
expect(maxSubArraySum([8,2,54,65,23,7,45], 2)).toEqual(119);
10+
expect(maxSubArraySum([8,2,54,65,23,7,45], 2)).toEqual(119);
1111
});
1212
test('maxSubArraySum [1, 9, 11, 13, 2, 3, 7, 5] to give 33', () => {
13-
expect(maxSubArraySum([1, 9, 11, 13, 2, 3, 7, 5], 3)).toEqual(33);
13+
expect(maxSubArraySum([1, 9, 11, 13, 2, 3, 7, 5], 3)).toEqual(33);
1414
});
1515
test('maxSubArraySum [] to give null', () => {
16-
expect(maxSubArraySum([], 3)).toEqual(null);
16+
expect(maxSubArraySum([], 3)).toEqual(null);
1717
});
1818
test('maxSubArraySum [-10,-9,-3,-5,-100,-56,0] to give -17', () => {
19-
expect(maxSubArraySum([-10,-9,-3,-5,-100,-56,0], 3)).toEqual(-17);
19+
expect(maxSubArraySum([-10,-9,-3,-5,-100,-56,0], 3)).toEqual(-17);
20+
});
21+
test('maxSubArraySum [2,6,9,2,1,8,5,6,3] to give 19', () => {
22+
expect(maxSubArraySum([2,6,9,2,1,8,5,6,3], 3)).toEqual(19);
2023
});

0 commit comments

Comments
 (0)