- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxSubarraySum.js
More file actions
Latest commit
23 lines (19 loc) · 520 Bytes
/
Copy pathmaxSubarraySum.js
File metadata and controls
23 lines (19 loc) · 520 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
functionmaxSubarraySum(arr,n){
letmax=0;
lettemp=0;
// if(arr.length<n) return null;
for(leti=0;i<n;i++){
temp+=arr[i];
}
max=temp;
for(letj=n;j<arr.length;j++){
max=max-arr[j-n]+arr[j];
temp=Math.max(temp,max);
}
returntemp;
}
// maxSubarraySum([100,200,300,400,500],2)//700
// maxSubarraySum([-3,4,0,-2,6,-1],2)//5
// maxSubarraySum([2,3],3)//null
maxSubarraySum([1,4,2,10,23,3,1,0,20],4)//39
///Sliding window-->