Skip to content

Added Sliding Window folder with problems #6072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Implementing Sliding Window/Max_Sum_Subarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Scanner;
public class Max_Sum_Subarray{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int k = in.nextInt(); //window size
int n = in.nextInt(); //limit of array
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = in.nextInt();
}
int j=0;
int i=0;
int sum=0;
int maxSum = Integer.MIN_VALUE;
while(j<arr.length){
sum+=arr[j];
if(j-i+1<k){
j++;
}
else if(j-i+1==k){
maxSum=Math.max(maxSum,sum);
sum-=arr[i];
i++;
j++;
}
}
System.out.println(maxSum);
}
}