1 min read algorithm
In the sliding window approach, a window of fixed length moves over the data, piece by piece, and the statistic is computed over the data in the window.
The sliding window approach is a very exciting technique used to puzzle out some of the complex problems requiring an array or a string. It is commonly used to scale down the time complexity of the issue from O(n²) to O(n).
The basic idea is to provide a window that provides the problem constraints. A window can be expressed by taking two pointers, for illustration, left and right, that point to a different index in the array or peculiar character in case of a string.
Example of Sliding Window Approach Solution for Finding Average of Subarray: