An Introduction to Algorithmic Thinking With Sliding Window
an intuitive approach to solving coding problems
In this article, I will explain the basics ideas on solving coding questions that use the sliding window technique. As a prerequisite, one should feel comfortable working with arrays, arraylists, and fundamental concepts such as looping and if-else statements. I covered the latter concept in my previous article regarding the basics of Java, and I will soon publish my article on arrays and arraylists in Java!
Let’s hop straight into an example, where we will go through the logic and code for a sliding window problem.
“Tell me and I forget, teach me and I may remember, involve me and I learn.” — Benjamin Franklin
Laying Out the Question
Suppose we are given an array of integers of size n. Each integer array[i], where 0 ≤ i < array.length can be positive or negative. We are also given an integer m, which represents the size of a subarray within the original array, and 1 ≤ m ≤ n. We want to find the subarray of length m which has the highest average out of all subarrays of length m in the array, and then return the value of that average as a double.