<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Manav Verma</title>
    <description>The latest articles on DEV Community by Manav Verma (@vmanav).</description>
    <link>https://dev.to/vmanav</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2378139%2Fcbe24ac4-5ad4-4579-b000-1300ec43ff91.jpg</url>
      <title>DEV Community: Manav Verma</title>
      <link>https://dev.to/vmanav</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vmanav"/>
    <language>en</language>
    <item>
      <title>The DSA Toolkit: #1 Sliding Window Format</title>
      <dc:creator>Manav Verma</dc:creator>
      <pubDate>Mon, 03 Nov 2025 23:10:05 +0000</pubDate>
      <link>https://dev.to/vmanav/the-dsa-toolkit-sliding-window-3j1n</link>
      <guid>https://dev.to/vmanav/the-dsa-toolkit-sliding-window-3j1n</guid>
      <description>&lt;p&gt;&lt;strong&gt;How to identify a Problem is a Sliding window problem?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It is an &lt;strong&gt;Array&lt;/strong&gt; or a &lt;strong&gt;String&lt;/strong&gt; question.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Subarray&lt;/strong&gt; or &lt;strong&gt;Substring&lt;/strong&gt; is asked in the question.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Either &lt;code&gt;windowSize&lt;/code&gt; or &lt;code&gt;windowCondition&lt;/code&gt; is given &lt;em&gt;(and the other one is asked)&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Identify the format of the problem: fixed-size Sliding Window or variable-sized window.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also, the window size is calculated &lt;code&gt;windowSize = (j-i+1)&lt;/code&gt; for any interval.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, we have two types of sliding window questions: first, a fixed-sized window whose size is explicitly stated in the question; and second, a variable-sized window whose size is not specified in the question and depends on a condition given in the question.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;1. Fixed-Size Sliding Window Format&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In these types of questions, the window size will be given in the question, and we will be asked to either minimize or maximize a stated condition.&lt;/p&gt;

&lt;p&gt;For example: “Find the maximum sum of a subarray of size k.” Here, the window size will be &lt;strong&gt;“k”,&lt;/strong&gt; and we have to “&lt;strong&gt;maximize the sum”&lt;/strong&gt; of the subarrays.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;windowSize → &lt;strong&gt;&lt;em&gt;GIVEN&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;condition → &lt;strong&gt;&lt;em&gt;Maximize or Minimize&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Do Calculations&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;windowSize&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Increase Window by adding more elements&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;windowSize&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="c1"&gt;// Get answer from the calculations&lt;/span&gt;

        &lt;span class="c1"&gt;// Remove the calculations for element `i` (Window start element)&lt;/span&gt;
        &lt;span class="c1"&gt;// Slide Window from i gradually&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;windowSize&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;GIVEN&lt;/span&gt;
&lt;span class="n"&gt;Condition&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;MAXIMIZE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Problems:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/problems/max-sum-subarray-of-size-k5313/1" rel="noopener noreferrer"&gt;GeeksforGeeks - Max Sum Subarray of size K&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/problems/first-negative-integer-in-every-window-of-size-k3345/1" rel="noopener noreferrer"&gt;GeeksforGeeks - First negative in every window of size k&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://leetcode.com/problems/find-all-anagrams-in-a-string/" rel="noopener noreferrer"&gt;LeetCode - Find All Anagrams in a String&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://leetcode.com/problems/sliding-window-maximum/description/" rel="noopener noreferrer"&gt;LeetCode - Sliding Window Maximum&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://leetcode.com/problems/permutation-in-string/description/" rel="noopener noreferrer"&gt;LeetCode - Permutation in String&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;2. Variable-size Sliding Window Format&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In these types of questions, the window size is not specified. Instead, a condition is stated, and we have to either &lt;em&gt;maximize or minimize the window based on that..&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For example: “Find the Longest subarray with sum K.” Here, the condition is explicitly stated in the question that the &lt;strong&gt;sum should be equal to “k”&lt;/strong&gt; &lt;code&gt;(sum == k)&lt;/code&gt;&lt;strong&gt;,&lt;/strong&gt; and we have to “&lt;strong&gt;maximize”&lt;/strong&gt; the window, i.e., find the longest subarray.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;condition → &lt;strong&gt;&lt;em&gt;GIVEN&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;windowSize → &lt;strong&gt;&lt;em&gt;Maximize or Minimize&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Do Calculations&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Increase Window if condition not met&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="c1"&gt;// Get answer from calculations&lt;/span&gt;
        &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// condition &amp;gt;k&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
                &lt;span class="c1"&gt;// Remove the calculations for element `i` (Window Start)&lt;/span&gt;
                &lt;span class="c1"&gt;// Slide Window from i&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="c1"&gt;// Check answer at this point as well&lt;/span&gt;
            &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Problems:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://takeuforward.org/plus/dsa/problems/longest-subarray-with-sum-k" rel="noopener noreferrer"&gt;takeUforward - Longest subarray with sum K&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/problems/longest-k-unique-characters-substring0853/1" rel="noopener noreferrer"&gt;GeeksforGeeks - Longest Substring with K Uniques&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/" rel="noopener noreferrer"&gt;LeetCode - Longest Substring Without Repeating Characters&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://leetcode.com/problems/minimum-window-substring/description/" rel="noopener noreferrer"&gt;LeetCode - Minimum Window Substring&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>datastructures</category>
      <category>programming</category>
      <category>learning</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
