Skip to content

Instantly share code, notes, and snippets.

@akshat-fsociety
Created December 26, 2020 08:46
Show Gist options
  • Save akshat-fsociety/f8246b4c31d02e9c58585253ad592970 to your computer and use it in GitHub Desktop.
Save akshat-fsociety/f8246b4c31d02e9c58585253ad592970 to your computer and use it in GitHub Desktop.
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 left=0, right=0, sum=0;
int max = Integer.MIN_VALUE;
while(right < n){
sum += a[right]; //summing of the right end of window
if(sum < k)
right++; // if sum < k increase the right end of window pane
else if(sum == k){
max = Math.max(max, right-left+1); // If the condition is reached update max with the size of window
right++;
}
else if(sum > k){ //If sum > k subtract the left side of the pane and maintain the sum == k
while(sum > k){
sum -= a[left];
left++;
}
right++;
}
}
System.out.println(max);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment