forked from NKaty/Algorithms-and-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax-subarray-sum.js
34 lines (25 loc) · 1.03 KB
/
max-subarray-sum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Sliding Window - maxSubarraySum
// Given an array of integers and a number, write a function called maxSubarraySum,
// which finds the maximum sum of a subarray with the length of the number passed to the function.
// Note that a subarray must consist of consecutive elements from the original array.
// In the first example below, [100, 200, 300] is a subarray of the original array, but [100, 300] is not.
// maxSubarraySum([100,200,300,400], 2) // 700
// Constraints:
// Time Complexity - O(N)
// Space Complexity - O(1)
function maxSubarraySum(arr, num) {
if (arr.length < num) return null;
let tempSum = 0;
for (let i = 0; i < num; i++) {
tempSum += arr[i];
}
let maxSum = tempSum;
for (let i = 0; i < arr.length - num; i++) {
tempSum = tempSum - arr[i] + arr[i + num];
maxSum = Math.max(maxSum, tempSum);
}
return maxSum;
}
console.log(maxSubarraySum([1, 4, 2, 10, 23, 3, 1, 0, 20], 4)); // 39
console.log(maxSubarraySum([3, -2, 7, -4, 1, -1, 4, -2, 1], 2)); // 5
console.log(maxSubarraySum([2, 3], 3)); // null