A sliding window is a range of consecutive positions that moves through an array from left to right. As it moves, we keep a summary of the values inside it, and here the summary is their sum. Each time the window moves, we correct the summary for the one value that enters and the one that leaves. For a sum, the correction is one addition and one subtraction instead of the additions it takes to add up the window again.
Let the window grow and shrink
A window of fixed length answers questions about subarrays of one given length. Many problems leave the length open and ask for the longest subarray that satisfies a condition. Here is one: given an array nums of nonnegative numbers and a limit, find the length of the longest subarray whose sum is at most the limit. We will use the array [2, 1, 3, 1] with a limit of 4. The subarrays [2, 1], [1, 3] and [3, 1] are within the limit, while the two subarrays of length 3, [2, 1, 3] and [1, 3, 1], sum to 6 and 5, and the whole array to 7, all over 4. So the answer is 2.
Checking every subarray is too slow for a long array. An array of values has subarrays that start at position 0, that start at position 1, and so on down to the one that starts at the last position, which makes in all. Even at one operation per subarray, means 50 seconds of work at operations a second:
A window finds the answer in one pass. We keep two positions, left and right, the first and last positions of the window, and total, the sum of the values from left to right. We move right forward one position at a time and add each new value to total. Whenever total goes over the limit, we subtract nums[left] from it and move left forward by one, and we repeat that until the window is within the limit again. Then we compare the length of the window, right - left + 1, with the longest seen so far.
Moving left forward gives up on a starting position for good, so we need a reason why that loses nothing. Take the moment when [2, 1, 3] goes over the limit with a sum of 6. Any longer window that starts at the 2 contains these three values and more, and the extra values are not negative, so its sum is at least 6 as well. No window that starts at position 0 and ends at position 2 or later can be within the limit. The shorter ones, [2] and [2, 1], were measured at earlier steps, so we are done with position 0 as a start. The same reasoning applies every time left moves.
Choose each move yourself
Now choose the moves yourself. The array is [2, 1, 3, 1] with three more values, 1, 1, 4, added at the end, and the limit is still 4. At each step, extend the window on the right or shrink it on the left. If you pick a move the loop would not make, the widget explains why and leaves the window as it is. The dashed outline marks the longest window so far, and the two counters show how many times each end has moved.
The longest window is [1, 1, 1], positions 3 to 5. When the 4 arrives, the sum jumps to 7, and all three 1s have to leave before the window is within the limit again. Over the whole run, right moves 7 times and left 6 times. Neither counter can pass 7, the length of the array.