DEV Community

Abhi Raj
Abhi Raj

Posted on

1

learn sliding window algorithm easily in Hindi | maximum sum subarray of size k with sliding window


in this video i have explained how to learn sliding window algorithm easily in Hindi

for those of you who don't want to watch video here is the code:

//find the maximum sum of size ‘k’ consecutive elements in the array.

let a = [4, 2, 3, 5, 1, 2];
//       0  1  2  3  4  5
let n = a.length; // 6
let k = 3;

let maxSum = -Infinity;
let windowStart = 0;
let windowSum = 0;

for (let windowEnd = 0; windowEnd < n; windowEnd++) {
  windowSum = windowSum + a[windowEnd];
  if (windowEnd - windowStart + 1 == k) {
    maxSum = Math.max(maxSum, windowSum);
    windowSum = windowSum - a[windowStart];
    windowStart++;
  }
}

console.log(maxSum);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay