Created
December 25, 2020 14:42
-
-
Save akshat-fsociety/46d2da51d85e67198f25281c511a197c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) | |
{ | |
FastReader s=new FastReader(); | |
int n = s.nextInt(); | |
int k = s.nextInt(); | |
int a[] = new int[n]; | |
for(int i=0; i<n; i++)a[i] = s.nextInt(); | |
int maximum_sum = Integer.MIN_VALUE; | |
//Making two pointer (left end to right end of the window) | |
int left=0, right=0, curr_sum=0; | |
while(right < n){ | |
//Adding the values of array till the window size is reached | |
curr_sum += a[right]; | |
//if window size is small increase the right pointer/end the window by 1 | |
if(right-left+1 < k) | |
right++; | |
//When window size is reached calculate the max value and update | |
else if(right-left+1 == k){ | |
maximum_sum = Math.max(curr_sum, maximum_sum); | |
//Moving the left end of window forward | |
curr_sum -= a[left]; | |
left++; | |
right++; | |
} | |
} | |
System.out.println(maximum_sum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment