-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMax_SubArraySum.java
More file actions
45 lines (28 loc) · 958 Bytes
/
Max_SubArraySum.java
File metadata and controls
45 lines (28 loc) · 958 Bytes
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
35
36
37
38
39
40
41
42
43
44
45
import java.util.*;
public class Solution {
public static int maxSum(int arr[], int n , int k){
if(n <k){
return -1;
}
int res =0;
for(int i=0; i<k; i++)
res += arr[i];
int currSum = res;
for(int i=k; i<n; i++){
// System.out.println( currSum + " " +arr[i-k] + " " + arr[i] + " "+ arr[k] );
currSum += arr[i] - arr[i-k];
res = Math.max(res, currSum);
}
return res;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int arr[] = new int[n];
for(int i =0; i<n; i++){
arr[i] = sc.nextInt();
}
System.out.println(maxSum(arr,n,k));
}
}