---
title: "Comment by yetanothertroll on \"Sliding Window Algorithm\" | Hashnode"
description: "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"
url: "https://hashnode.com/posts/sliding-window-algorithm/645879ac911934ba87f23b94/comment/6469712d900ad00abe59c757"
---

[Discussion](https://hashnode.com/posts/sliding-window-algorithm/645879ac911934ba87f23b94)

Comment [Sliding Window Algorithm](https://hashnode.com/posts/sliding-window-algorithm/645879ac911934ba87f23b94)

[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
