---
title: "Discussion on \"Sliding Window Algorithm\" | Hashnode"
description: "Discussion on \"Sliding Window Algorithm\". Optimize Sliding Window Algorithm for arrays, strings; solve max sum, longest, min window substrings. Master LeetCode challenges"
url: "https://hashnode.com/posts/sliding-window-algorithm/645879ac911934ba87f23b94"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Sliding Window Algorithm](https://blog.austn.net/sliding-window-algorithm?utm_source=hashnode&utm_medium=feed)

Optimize Sliding Window Algorithm for arrays, strings; solve max sum, longest, min window substrings. Master LeetCode challenges

May 8, 2023 · 2 min read · blog.austn.net  
Read the article

-   [#algorithms](https://hashnode.com/tag/algorithms)
-   [#ruby](https://hashnode.com/tag/ruby)

## Written by

[Austin](https://hashnode.com/@austn)

@austn

  
[View profile](https://hashnode.com/@austn)

Software Engineer

Professional software engineer working at CoverMyMeds, an enterprise healthtech firm that ensures patients get access to the medication they need. Primarily focused on backend and systems design through Ruby and Elixir, but experienced with JavaScript and all things frontend Interested in learning and teaching the craft of software; technical or otherwise Join me as I innovate by iterating!

## Responses 3

Markdown supported · ⌘↵ to post

[yetanothertroll](https://hashnode.com/@yetanothertroll)

[May 24, 2023](https://hashnode.com/posts/sliding-window-algorithm/645879ac911934ba87f23b94/comment/646d97d6aa3b783ef11a8406)

The maximum subarray sum with one deletion does not appear to be faster than O(n²) even using sliding window. I can almost but not quite grasp a "dynamic programming" approach to speed it up aargh

[Austin](https://hashnode.com/@austn)

[May 24, 2023](https://hashnode.com/posts/sliding-window-algorithm/645879ac911934ba87f23b94/comment/646d97d6aa3b783ef11a8406#reply-646e24dcb16e4c4af29109f0)

I think if you're reaching an O(n^2) solution, something must be going wrong.

Good luck though, if it can be done in BASIC, you're the one to figure it out!

[yetanothertroll](https://hashnode.com/@yetanothertroll)

[May 25, 2023](https://hashnode.com/posts/sliding-window-algorithm/645879ac911934ba87f23b94/comment/646d97d6aa3b783ef11a8406#reply-646eb0bd5bd7d54f0c7ea330)

[Austin](https://hashnode.com/@austn) I think I can once I understand Kaldane's algorithm thoroughly. :)

[yetanothertroll](https://hashnode.com/@yetanothertroll)

[May 30, 2023](https://hashnode.com/posts/sliding-window-algorithm/645879ac911934ba87f23b94/comment/646d97d6aa3b783ef11a8406#reply-647564aa731d78b677df6b43)

[Austin](https://hashnode.com/@austn) My stabs at the leetcode problems can be found here [forums.craigslist.org](https://forums.craigslist.org/?forumID=9510)

[yetanothertroll](https://hashnode.com/@yetanothertroll)

[May 21, 2023](https://hashnode.com/posts/sliding-window-algorithm/645879ac911934ba87f23b94/comment/6469724a900ad00abe59c769)

Wow, code in comments gets mangled and trying to edit comments fails

[yetanothertroll](https://hashnode.com/@yetanothertroll)

[May 21, 2023](https://hashnode.com/posts/sliding-window-algorithm/645879ac911934ba87f23b94/comment/6469712d900ad00abe59c757)

Why not use BASIC? Admittedly adding a good data structure for tracking duplicated elements within the window, dropping a negative element, and maybe max sum of sums might be 'interesting' in BASIC lol

Written in X11 BASIC for Android ver. 1.28-65 18/8/2022 build

Not the tightest code ever, but we all learn the hard way to optimize for readability when a bug is found a year later don't we

Program Sliders

! Can Nums() include negative numbers?

Nums() = \[ 2, 1, 5, 1, 3, 2 \]

k% = 3

Clr c\_l%, c\_r%, c\_sum

Clr max\_l%, max\_r%, max\_sum

Print k%, Dim?(Nums())

! max\_sum = -1e200

For c\_r% = 0 To Dim?(Nums()) - 1

If k% < (c\_r% - c\_l% + 1) ! Beware of fencepost error

Sub c\_sum, Nums(c\_l%)

Inc c\_l%

EndIf

Add c\_sum, Nums(c\_r%)

! Invariants: (Hopefully lol)

! Count of Nums(c\_l% : c\_r%) <= k%

! Sum of Nums(c\_l% : c\_r%) = c\_sum

Print "Check", c\_l%, c\_r%, c\_sum

If (k% = (c\_r% - c\_l% + 1)) And (c\_sum > max\_sum)

max\_l% = c\_l%

max\_r% = c\_r%

max\_sum = c\_sum

Print "New max", max\_l%, max\_r%, max\_sum

EndIf

Next c\_r%

Print "Result", max\_l%, max\_r%, max\_sum

Print "OK"

End
