-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxSubArrayK.java
64 lines (56 loc) · 1.52 KB
/
MaxSubArrayK.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Given an array and a number k, find the largest sum of the subarray containing at least k numbers. It may be assumed that the size of array is at-least k.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the array. Then the following line contains n space separated integers. The last line of the input contains the number k.
Output:
Print the value of the largest sum of the subarray containing at least k numbers.
Constraints:
1<=T<=10^5
1<=n<=10^5
1<=a[i]<=10^5
1<=k<=n
Example:
Input:
2
4
-4 -2 1 -3
2
6
1 1 1 1 1 1
2
Output:
-1
6*/
import java.util.*;
public class GFG{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t--!=0){
int n = sc.nextInt();
long arr[] = new long[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextLong();
}
int k = sc.nextInt();
System.out.println(getSub(arr,k));
}
}
static long getSub(long arr[],int k){
int n = arr.length;
long left[] = new long[n],res=-Long.MAX_VALUE,sum=0;
for(int i=0;i<n;i++){
left[i] = arr[i];
if(i>0 && left[i-1]>0){
left[i] += left[i-1];
}
if(i<k)sum+=arr[i];
}
res = sum;
for(int i=1;i+k-1<n;i++){
sum = sum - arr[i-1] + arr[i+k-1];
res = Math.max(res,Math.max(sum,sum+left[i-1]));
}
return res;
}
}