We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 03bb8ee commit 19f5103Copy full SHA for 19f5103
Implementing Sliding Window/Max_Sum_Subarray.java
@@ -0,0 +1,29 @@
1
+import java.util.Scanner;
2
+public class Max_Sum_Subarray{
3
+ public static void main(String[] args) {
4
+ Scanner in = new Scanner(System.in);
5
+ int k = in.nextInt(); //window size
6
+ int n = in.nextInt(); //limit of array
7
+ int[] arr = new int[n];
8
+ for(int i=0;i<n;i++){
9
+ arr[i] = in.nextInt();
10
+ }
11
+ int j=0;
12
+ int i=0;
13
+ int sum=0;
14
+ int maxSum = Integer.MIN_VALUE;
15
+ while(j<arr.length){
16
+ sum+=arr[j];
17
+ if(j-i+1<k){
18
+ j++;
19
20
+ else if(j-i+1==k){
21
+ maxSum=Math.max(maxSum,sum);
22
+ sum-=arr[i];
23
+ i++;
24
25
26
27
+ System.out.println(maxSum);
28
29
+}
0 commit comments