Sliding Window ( Two Pointer )


I have been exploring the concept of the sliding window technique using two pointers. Here are some of my understandings along with examples:
Length of subarray problem
If there is question to find a length of subarray there is a common pattern in all the examples
Initialize Two Pointers: Set
l = 0
andr = 0
.Initialize Maximum Length: Set
maxlength = 0
.Iterate with a While Loop: Use
while (r < n)
, wheren
is the size of the array.Check the Condition: Evaluate the condition specified in the problem.
Update Maximum Length: If the condition is met, update
maxlength
.Window Size: Always remember that the size of the window is
(r - l + 1)
.Increase Right Pointer: Increment the right pointer
r
.
We have to focus on two parts in sliding window questions
Expanding and Shrinking
Epanding : increasing size of window as per condition ( r++ ).
Shrinking : decreasing the size of window if condition goes out of bound ( l++ ).
Here is example ( Type 1 ): Longest subarray with sum <=k
public int maxLengthSubarray(int nums[], int k) {
int l = 0;
int r = 0;
int maxLength = 0;
int sum = 0;
int n = nums.length;
while (r < n) {
sum += nums[r];
if (sum > k) { //here it is a optimized approach for counting length only.
sum -= nums[l++];
}
if (sum <= k) {
maxLength = Math.max(maxLength, r - l + 1);
}
r++;
}
return maxLength;
}
( Type 2 ) : Number of Subarrays
This is where instead of just one maximum length, we need to count how many subarrays satisfy a condition.
For example: Number of subarrays with sum exactly equal to k
We can't use the shrinking technique directly here, because the moment we shrink, we might miss valid subarrays.
So instead, we use a prefix sum + hashmap strategy:
public int subarraySum(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1); // base case
int count = 0, sum = 0;
for (int num : nums) {
sum += num;
if (map.containsKey(sum - k)) {
count += map.get(sum - k);
}
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
return count;
}
Why this works:
If sum - k
has been seen before, that means there's a subarray ending at this index whose sum is exactly k
.
You can also have Type 2 problems where the number of subarrays is to be found with:
At most
k
distinct integersSum ≤
k
Product <
k
(in which case you may use sliding window with count logic)
Conclusion
Understanding the sliding window technique has really helped me approach array and string problems more efficiently. The key is to always focus on how the window is being expanded and when it needs to shrink based on the condition.
For length-based problems, maintaining the window size and updating max length works best.
For count-based problems, prefix sums and hashmaps are often the go-to strategy.
It’s not just about memorizing patterns — it’s about understanding why the window changes and how to manage it dynamically.
I’ll keep practicing different variations to get more confident, especially with edge cases. This concept definitely feels like a core tool for DSA.
Subscribe to my newsletter
Read articles from Tejas Dherange directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
