DSA: Sliding Window Pattern

JavaScript Sliding window pattern
The use of the Sliding Window technique can be done in a very specific scenario, where the size of window for computation is fixed throughout the complete nested loop. Only then the time complexity can be reduced.

How to use Sliding Window Technique?

The general use of Sliding window technique can be demonstrated as following:

  1. Find the size of window required
  2. Compute the result for 1st window, i.e. from start of data structure
  3. Then use a loop to slide the window by 1, and keep computing the result window by window.

Write a function called maxSubarraySum which accepts an array of integers and a number called num. The function should calculate the maximum sum of num consecutive elements in the array.

Time Complexity — O(N)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luis Castillo

Luis Castillo


Software Engineer / Full-Stack Developer in Ruby, Ruby on Rails, JavaScript, ReactJS, Redux, SQL, HTML5, CSS3