# sumit-22/sliding-window-algorithm architecture

> Architecture diagram of the GitHub repository sumit-22/sliding-window-algorithm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sumit-22/sliding-window-algorithm
- Repository: https://github.com/sumit-22/sliding-window-algorithm

## Overview

1. **Repository purpose**
   - This repo is a collection of **Java implementations of sliding-window algorithms**.
   - It is not an application with a runtime workflow; it is a **problem-solution library / study repo** organized by individual LeetCode-style exercises.

2. **Top-level structure**
   - Most `.java` files are **independent solution modules**, each focused on one array/string/window problem.
   - `README.md` is minimal and only names the theme: **Sliding-Window-Algorithm**.
   - `template.txt` likely serves as a scratchpad or reusable pattern reference for solving window problems.

3. **Primary architectural boundary**
   - The main boundary is **one problem per file**.
   - There is no evidence of shared package structure, service layer, persistence layer, or cross-file runtime dependencies.
   - Each class/function likely accepts an array/string/stream-like input and returns a computed result.

4. **Core algorithm family**
   - The repo centers on the **sliding window technique**, including:
     - fixed-size windows,
     - variable-size windows,
     - frequency/count-based windows,
     - monotonic/constraint-driven windows,
     - streaming windows.
   - This is the dominant architectural pattern across the repository.

5. **Fixed-size window problems**
   - These files suggest classic k-length window computations:
     - `MaxOfAllSubarrays.java`
     - `firstNegInKSizeArray.java`
     - `MaxSumSubArray.java`
     - `X-SumOfK-longSubarrays(LC-3318).java`
     - `findThePowerOfK-SizeSubarrays(lc_3254)`
   - Data flow here is typically: **iterate array → maintain rolling state → emit per-window result**.

6. **Variable-size / constraint-based windows**
   - Several files solve problems where the window grows/shrinks to satisfy a constraint:
     - `LongestSubarraySumK.java`
     - `shortestSubarraySum>=k.java`
     - `LC1438–LongestSubarrayAbsoluteDiff≤Limit.java`
     - `CountOfNiceSubarrays.java`
     - `subarrayWithKDistinctIntegers.java`
   - These solutions usually manage two pointers plus auxiliary state such as sums, counts, or deques.

7. **Substring and pattern-matching windows**
   - String-centric window problems are grouped implicitly by filenames:
     - `minimumWindowSubstring.java`
     - `MinWindowSubsequence.java`
     - `findAllAnagrams(LC-438).java`
     - `permutationInString(lc-567).java`
     - `CountOccurencesOfAnagram.java`
     - `NoOfSubstringsContainingAllThreeCharacters.java`
     - `CharacterReplacement.java`
   - These rely on character frequency maps/arrays and a window-validity check.

8. **Frequency / distinct-element window problems**
   - Some files emphasize counts, uniqueness, and distribution:
     - `MaxSumOfDistinctSubarrayWithLengthK(LC-2461).java`
     - `calculateCompressedMean(LC_2985).java`
     - `Pick_Toys.java`
     - `Long…SpecialSubstring…Thrice(LC-2981).java`
   - The common structure is **track frequency table → adjust window until constraints hold → compute score/length**.

9. **Streaming / online processing**
   - `MovingAverageFromDataStream(lc_346).java` is the only file that clearly models a **streaming data structure** rather than a batch array/string solution.
   - Architectural difference: it likely keeps state across calls, so the boundary is **incremental updates in, rolling statistic out**.

10. **Subarray counting and sum-based analytics**
    - Files such as `binarySubarrayWithSum.java`, `MaxSumSubArray.java`, and `CountOfNiceSubarrays.java` indicate sum/count-oriented analytics over contiguous regions.
    - These usually sit at the intersection of windowing and prefix-like reasoning, but still fit the repo’s broader “contiguous segment processing” theme.

11. **Naming and organization signals**
    - File names are the primary documentation.
    - LeetCode references in filenames (`LC-...`) indicate problem provenance and help classify by pattern.
    - There is no indication of packages/modules; the repo appears intentionally flat for easy lookup and comparison.

12. **How to diagram this repo**
    - The cleanest architecture diagram would show:
      - **Input data**: arrays, strings, or streams
      - **Per-file algorithm module**: one sliding-window solution each
      - **Auxiliary state**: pointers, counts, sums, queues/deques
      - **Output**: length, count, max/min, subarray/substring index, or streaming statistic
    - The important message is that the repo is a **catalog of algorithmic components**, not a cohesive system with inter-service flows.

13. **Overall takeaway**
    - This is a **single-domain algorithm repository** centered on contiguous-window techniques.
    - Its architecture is intentionally simple: **independent problem solvers sharing a common design pattern**, with minimal shared infrastructure.

Main entry points: [`README.md`](https://github.com/sumit-22/sliding-window-algorithm/blob/main/README.md) (README), [`template.txt`](https://github.com/sumit-22/sliding-window-algorithm/blob/main/template.txt) (Template), [`MaxOfAllSubarrays.java`](https://github.com/sumit-22/sliding-window-algorithm/blob/main/MaxOfAllSubarrays.java) (Max subarrays), [`firstNegInKSizeArray.java`](https://github.com/sumit-22/sliding-window-algorithm/blob/main/firstNegInKSizeArray.java) (First negative)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_meta["Repo meta"]
  node_repo{{"Sliding Window<br/>algorithm catalog"}}
  node_readme["README<br/>overview<br/>[README.md]"]
  node_template["Template<br/>scratchpad<br/>[template.txt]"]
end

subgraph group_fixed["Fixed windows"]
  node_fixed_hub["Fixed window<br/>pattern hub"]
  node_max_subarrays["Max subarrays<br/>fixed max"]
  node_first_negative["First negative<br/>fixed queue"]
  node_max_sum_window["Max sum window<br/>fixed sum"]
  node_x_sum["X-sum<br/>fixed score"]
  node_power_k["Power k<br/>fixed score"]
end

subgraph group_variable["Variable windows"]
  node_variable_hub["Variable window<br/>pattern hub"]
  node_longest_sum_k["Sum k<br/>variable sum"]
  node_shortest_ge_k["Shortest >=k<br/>variable sum"]
  node_abs_diff_limit["Abs diff<br/>variable deque"]
  node_nice_subarrays["Nice subarrays<br/>variable parity"]
  node_k_distinct["K distinct<br/>variable distinct"]
  node_max_distinct_sum["Distinct sum<br/>variable distinct"]
  node_binary_sum["Binary sum<br/>variable count"]
  node_pick_toys["Pick toys<br/>variable distinct<br/>[Pick_Toys.java]"]
  node_special_thrice["Special thrice<br/>variable freq"]
end

subgraph group_string["String windows"]
  node_string_hub["String window<br/>pattern hub"]
  node_min_window_substring["Min window<br/>string cover"]
  node_min_window_subseq["Min subseq<br/>string subseq"]
  node_find_anagrams["Anagrams<br/>string match"]
  node_permute_in_string["Permutation<br/>string match"]
  node_count_anagram["Count anagrams<br/>string count"]
  node_three_chars["Three chars<br/>string cover"]
  node_char_replace["Replace chars<br/>string freq"]
end

subgraph group_stream["Streaming"]
  node_stream_hub["Stream state<br/>pattern hub"]
  node_moving_average(("Moving average<br/>stream statistic"))
  node_compressed_mean["Compressed mean<br/>stream metric"]
end

node_repo -->|"documents"| node_readme
node_repo -->|"guides"| node_template
node_repo -->|"organizes"| node_fixed_hub
node_repo -->|"organizes"| node_variable_hub
node_repo -->|"organizes"| node_string_hub
node_repo -->|"organizes"| node_stream_hub
node_fixed_hub -->|"includes"| node_max_subarrays
node_fixed_hub -->|"includes"| node_first_negative
node_fixed_hub -->|"includes"| node_max_sum_window
node_fixed_hub -->|"includes"| node_x_sum
node_fixed_hub -->|"includes"| node_power_k
node_variable_hub -->|"includes"| node_longest_sum_k
node_variable_hub -->|"includes"| node_shortest_ge_k
node_variable_hub -->|"includes"| node_abs_diff_limit
node_variable_hub -->|"includes"| node_nice_subarrays
node_variable_hub -->|"includes"| node_k_distinct
node_variable_hub -->|"includes"| node_max_distinct_sum
node_variable_hub -->|"includes"| node_binary_sum
node_variable_hub -->|"includes"| node_pick_toys
node_variable_hub -->|"includes"| node_special_thrice
node_string_hub -->|"includes"| node_min_window_substring
node_string_hub -->|"includes"| node_min_window_subseq
node_string_hub -->|"includes"| node_find_anagrams
node_string_hub -->|"includes"| node_permute_in_string
node_string_hub -->|"includes"| node_count_anagram
node_string_hub -->|"includes"| node_three_chars
node_string_hub -->|"includes"| node_char_replace
node_stream_hub -->|"includes"| node_moving_average
node_stream_hub -->|"includes"| node_compressed_mean

click node_readme "https://github.com/sumit-22/sliding-window-algorithm/blob/main/README.md"
click node_template "https://github.com/sumit-22/sliding-window-algorithm/blob/main/template.txt"
click node_max_subarrays "https://github.com/sumit-22/sliding-window-algorithm/blob/main/MaxOfAllSubarrays.java"
click node_first_negative "https://github.com/sumit-22/sliding-window-algorithm/blob/main/firstNegInKSizeArray.java"
click node_max_sum_window "https://github.com/sumit-22/sliding-window-algorithm/blob/main/MaxSumSubArray.java"
click node_x_sum "https://github.com/sumit-22/sliding-window-algorithm/blob/main/X-SumOfK-longSubarrays(LC-3318).java"
click node_power_k "https://github.com/sumit-22/sliding-window-algorithm/tree/main/findThePowerOfK-SizeSubarrays(lc_3254)"
click node_longest_sum_k "https://github.com/sumit-22/sliding-window-algorithm/blob/main/LongestSubarraySumK.java"
click node_shortest_ge_k "https://github.com/sumit-22/sliding-window-algorithm/blob/main/shortestSubarraySum>=k.java"
click node_abs_diff_limit "https://github.com/sumit-22/sliding-window-algorithm/blob/main/LC1438–LongestSubarrayAbsoluteDiff≤Limit.java"
click node_nice_subarrays "https://github.com/sumit-22/sliding-window-algorithm/blob/main/CountOfNiceSubarrays.java"
click node_k_distinct "https://github.com/sumit-22/sliding-window-algorithm/blob/main/subarrayWithKDistinctIntegers.java"
click node_max_distinct_sum "https://github.com/sumit-22/sliding-window-algorithm/blob/main/MaxSumOfDistinctSubarrayWithLengthK(LC-2461).java"
click node_binary_sum "https://github.com/sumit-22/sliding-window-algorithm/blob/main/binarySubarrayWithSum.java"
click node_pick_toys "https://github.com/sumit-22/sliding-window-algorithm/blob/main/Pick_Toys.java"
click node_special_thrice "https://github.com/sumit-22/sliding-window-algorithm/blob/main/LongestSpecialSubstringthatOccursThrice(LC-2981).java"
click node_min_window_substring "https://github.com/sumit-22/sliding-window-algorithm/blob/main/minimumWindowSubstring.java"
click node_min_window_subseq "https://github.com/sumit-22/sliding-window-algorithm/blob/main/MinWindowSubsequence.java"
click node_find_anagrams "https://github.com/sumit-22/sliding-window-algorithm/blob/main/findAllAnagrams(LC-438).java"
click node_permute_in_string "https://github.com/sumit-22/sliding-window-algorithm/blob/main/permutationInString(lc-567).java"
click node_count_anagram "https://github.com/sumit-22/sliding-window-algorithm/blob/main/CountOccurencesOfAnagram.java"
click node_three_chars "https://github.com/sumit-22/sliding-window-algorithm/blob/main/NoOfSubstringsContainingAllThreeCharacters.java"
click node_char_replace "https://github.com/sumit-22/sliding-window-algorithm/blob/main/CharacterReplacement.java"
click node_moving_average "https://github.com/sumit-22/sliding-window-algorithm/blob/main/MovingAverageFromDataStream(lc_346).java"
click node_compressed_mean "https://github.com/sumit-22/sliding-window-algorithm/blob/main/calculateCompressedMean(LC_2985).java"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_repo,node_readme,node_template toneBlue
class node_fixed_hub,node_max_subarrays,node_first_negative,node_max_sum_window,node_x_sum,node_power_k toneAmber
class node_variable_hub,node_longest_sum_k,node_shortest_ge_k,node_abs_diff_limit,node_nice_subarrays,node_k_distinct,node_max_distinct_sum,node_binary_sum,node_pick_toys,node_special_thrice toneMint
class node_string_hub,node_min_window_substring,node_min_window_subseq,node_find_anagrams,node_permute_in_string,node_count_anagram,node_three_chars,node_char_replace toneRose
class node_stream_hub,node_moving_average,node_compressed_mean toneIndigo
```

## Components

### Repo meta

Documentation and shared notes for the study repo.

- **Sliding Window**: algorithm catalog · Flat collection of independent sliding-window solutions.
- [README](https://github.com/sumit-22/sliding-window-algorithm/blob/main/README.md): overview · Minimal theme note for the repository. · `README.md`
- [Template](https://github.com/sumit-22/sliding-window-algorithm/blob/main/template.txt): scratchpad · Reusable pattern notes or solution template. · `template.txt`

### Fixed windows

Classic k-length rolling window problems over arrays.

- **Fixed window**: pattern hub · Representative boundary for fixed-size rolling window solutions.
- [Max subarrays](https://github.com/sumit-22/sliding-window-algorithm/blob/main/MaxOfAllSubarrays.java): fixed max · Maximum over all size-k subarrays. · `MaxOfAllSubarrays.java`
- [First negative](https://github.com/sumit-22/sliding-window-algorithm/blob/main/firstNegInKSizeArray.java): fixed queue · First negative value in each k-sized window. · `firstNegInKSizeArray.java`
- [Max sum window](https://github.com/sumit-22/sliding-window-algorithm/blob/main/MaxSumSubArray.java): fixed sum · Maximum sum over a fixed-length subarray. · `MaxSumSubArray.java`
- [X-sum](https://github.com/sumit-22/sliding-window-algorithm/blob/main/X-SumOfK-longSubarrays(LC-3318).java): fixed score · K-length window scoring variant. · `X-SumOfK-longSubarrays(LC-3318).java`
- [Power k](https://github.com/sumit-22/sliding-window-algorithm/tree/main/findThePowerOfK-SizeSubarrays(lc_3254)): fixed score · Computes a derived value for each size-k subarray. · `findThePowerOfK-SizeSubarrays(lc_3254)`

### Variable windows

Two-pointer windows that expand and contract to satisfy constraints.

- **Variable window**: pattern hub · Representative boundary for expand-shrink window solutions.
- [Sum k](https://github.com/sumit-22/sliding-window-algorithm/blob/main/LongestSubarraySumK.java): variable sum · Longest subarray meeting a sum target. · `LongestSubarraySumK.java`
- [Shortest >=k](https://github.com/sumit-22/sliding-window-algorithm/blob/main/shortestSubarraySum>=k.java): variable sum · Shortest subarray with sum at least k. · `shortestSubarraySum>=k.java`
- [Abs diff](https://github.com/sumit-22/sliding-window-algorithm/blob/main/LC1438–LongestSubarrayAbsoluteDiff≤Limit.java): variable deque · Longest window where max-min stays within a limit. · `LC1438–LongestSubarrayAbsoluteDiff≤Limit.java`
- [Nice subarrays](https://github.com/sumit-22/sliding-window-algorithm/blob/main/CountOfNiceSubarrays.java): variable parity · Counts windows based on odd-count constraints. · `CountOfNiceSubarrays.java`
- [K distinct](https://github.com/sumit-22/sliding-window-algorithm/blob/main/subarrayWithKDistinctIntegers.java): variable distinct · Subarrays constrained by distinct-element count. · `subarrayWithKDistinctIntegers.java`
- [Distinct sum](https://github.com/sumit-22/sliding-window-algorithm/blob/main/MaxSumOfDistinctSubarrayWithLengthK(LC-2461).java): variable distinct · Maximum sum for a length-k window with unique values. · `MaxSumOfDistinctSubarrayWithLengthK(LC-2461).java`
- [Binary sum](https://github.com/sumit-22/sliding-window-algorithm/blob/main/binarySubarrayWithSum.java): variable count · Counting subarrays in a binary array by sum. · `binarySubarrayWithSum.java`
- [Pick toys](https://github.com/sumit-22/sliding-window-algorithm/blob/main/Pick_Toys.java): variable distinct · Longest window with limited categories. · `Pick_Toys.java`
- [Special thrice](https://github.com/sumit-22/sliding-window-algorithm/blob/main/LongestSpecialSubstringthatOccursThrice(LC-2981).java): variable freq · Longest special substring occurring thrice. · `LongestSpecialSubstringthatOccursThrice(LC-2981).java`

### String windows

Substring and pattern-matching window problems over character data.

- **String window**: pattern hub · Representative boundary for substring and character-window solutions.
- [Min window](https://github.com/sumit-22/sliding-window-algorithm/blob/main/minimumWindowSubstring.java): string cover · Minimum substring containing all required characters. · `minimumWindowSubstring.java`
- [Min subseq](https://github.com/sumit-22/sliding-window-algorithm/blob/main/MinWindowSubsequence.java): string subseq · Minimum window subsequence search. · `MinWindowSubsequence.java`
- [Anagrams](https://github.com/sumit-22/sliding-window-algorithm/blob/main/findAllAnagrams(LC-438).java): string match · Find all anagram start positions. · `findAllAnagrams(LC-438).java`
- [Permutation](https://github.com/sumit-22/sliding-window-algorithm/blob/main/permutationInString(lc-567).java): string match · Checks whether a permutation appears in a string. · `permutationInString(lc-567).java`
- [Count anagrams](https://github.com/sumit-22/sliding-window-algorithm/blob/main/CountOccurencesOfAnagram.java): string count · Counts anagram occurrences in text. · `CountOccurencesOfAnagram.java`
- [Three chars](https://github.com/sumit-22/sliding-window-algorithm/blob/main/NoOfSubstringsContainingAllThreeCharacters.java): string cover · Counts substrings containing all three required characters. · `NoOfSubstringsContainingAllThreeCharacters.java`
- [Replace chars](https://github.com/sumit-22/sliding-window-algorithm/blob/main/CharacterReplacement.java): string freq · Longest repeating-character window with replacements. · `CharacterReplacement.java`

### Streaming

Incremental stateful processing over an input stream.

- **Stream state**: pattern hub · Representative boundary for stateful streaming solutions.
- [Moving average](https://github.com/sumit-22/sliding-window-algorithm/blob/main/MovingAverageFromDataStream(lc_346).java): stream statistic · Stateful rolling average over a data stream. · `MovingAverageFromDataStream(lc_346).java`
- [Compressed mean](https://github.com/sumit-22/sliding-window-algorithm/blob/main/calculateCompressedMean(LC_2985).java): stream metric · Windowed or compressed mean calculation. · `calculateCompressedMean(LC_2985).java`

## Connections

- Sliding Window → README: documents · Minimal repository theme note.
- Sliding Window → Template: guides · Reusable sliding-window pattern reference.
- Sliding Window → Fixed window: organizes · Representative fixed-window solution boundary.
- Sliding Window → Variable window: organizes · Representative variable-window solution boundary.
- Sliding Window → String window: organizes · Representative string-window solution boundary.
- Sliding Window → Stream state: organizes · Representative streaming solution boundary.
- Fixed window → Max subarrays: includes · Example fixed-size rolling window problem.
- Fixed window → First negative: includes · Example fixed-size rolling window problem.
- Fixed window → Max sum window: includes · Example fixed-size rolling window problem.
- Fixed window → X-sum: includes · Example fixed-size rolling window problem.
- Fixed window → Power k: includes · Example fixed-size rolling window problem.
- Variable window → Sum k: includes · Example expand-shrink window problem.
- Variable window → Shortest >=k: includes · Example expand-shrink window problem.
- Variable window → Abs diff: includes · Example expand-shrink window problem.
- Variable window → Nice subarrays: includes · Example expand-shrink window problem.
- Variable window → K distinct: includes · Example expand-shrink window problem.
- Variable window → Distinct sum: includes · Example expand-shrink window problem.
- Variable window → Binary sum: includes · Example contiguous subarray counting problem.
- Variable window → Pick toys: includes · Example expand-shrink window problem.
- Variable window → Special thrice: includes · Example frequency-driven window problem.
- String window → Min window: includes · Example character-cover window problem.
- String window → Min subseq: includes · Example ordered subsequence window problem.
- String window → Anagrams: includes · Example anagram-window problem.
- String window → Permutation: includes · Example permutation-match problem.
- String window → Count anagrams: includes · Example anagram-counting problem.
- String window → Three chars: includes · Example coverage counting problem.
- String window → Replace chars: includes · Example frequency-based replacement problem.
- Stream state → Moving average: includes · Stateful data-stream statistic.
- Stream state → Compressed mean: includes · Rolling metric over streaming input.
