shreyansh

Pattern + Template

  1. Constant window size (not asked many times)
    • arr = [-1, 3, 4, 5, -1] & k = 4
    • Max sum that you can get by picking up k=4 consecutive elements.
    • Here we'll keep the window size constant and move the window till right < arr.length.
sum = 7, l = 0, r = k - 1

while(r < arr.length - 1){
	sum = sum - arr[l];
	l++;
	r++;
	sum += arr[r];
	maxSum = max(maxSum, sum);
}
  1. Longest subarray/substring where <condition>
    1. This is basically acquire and release strategy Acquire and Release Strategy or Expand and Shrink strategy.
      1. 1004. Max Consecutive Ones III
    2. Let's consider this question and see the different strategies to solve it.
AcquireNrelease_4_1.jpg
AcquireNrelease_2.jpg
AcquireNrelease_3.jpg