0% found this document useful (0 votes)
11 views4 pages

Sliding Window CheatSheet Explained

The document provides implementations of various sliding window algorithms in C++, including finding the maximum sum of subarrays, counting occurrences of anagrams, and checking for permutations in strings. Each algorithm is accompanied by a brief description and a cheat code for quick reference. The document covers a total of ten different problems, with some solutions utilizing data structures like deque and frequency arrays.

Uploaded by

saikumar2632003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Sliding Window CheatSheet Explained

The document provides implementations of various sliding window algorithms in C++, including finding the maximum sum of subarrays, counting occurrences of anagrams, and checking for permutations in strings. Each algorithm is accompanied by a brief description and a cheat code for quick reference. The document covers a total of ten different problems, with some solutions utilizing data structures like deque and frequency arrays.

Uploaded by

saikumar2632003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

// Sliding Window - Fixed Size using Optimized Fixed Window Pattern

#include <bits/stdc++.h>
using namespace std;

/*
------------------------------------------------------
| 1. Maximum Sum Subarray of Size K |
| Cheat Code: Fixed window of size k, track max sum |
------------------------------------------------------
*/
int maxSumSubarrayOfSizeK(vector<int>& nums, int k) {
int window_sum = 0, max_sum = 0;
for (int i = 0; i < k; i++) window_sum += nums[i];
max_sum = window_sum;
for (int i = k; i < nums.size(); i++) {
window_sum += nums[i] - nums[i - k];
max_sum = max(max_sum, window_sum);
}
return max_sum;
}

/*
------------------------------------------------------------------
| 2. Number of Subarrays with Average >= Threshold |
| Cheat Code: Fixed window, check if average >= threshold |
------------------------------------------------------------------
*/
int numOfSubarrays(vector<int>& nums, int k, int threshold) {
int window_sum = 0, count = 0;
for (int i = 0; i < k; i++) window_sum += nums[i];
if (window_sum / k >= threshold) count++;
for (int i = k; i < nums.size(); i++) {
window_sum += nums[i] - nums[i - k];
if (window_sum / k >= threshold) count++;
}
return count;
}

/*
----------------------------------------------------------
| 3. First Negative Number in Every Window of Size K |
| Cheat Code: Use deque to track first negative in range |
----------------------------------------------------------
*/
vector<int> firstNegativeInWindow(vector<int>& nums, int k) {
deque<int> dq;
vector<int> result;

for (int i = 0; i < nums.size(); i++) {


if (nums[i] < 0) dq.push_back(i);
if (i >= k - 1) {
if (!dq.empty() && dq.front() < i - k + 1) dq.pop_front();
result.push_back(dq.empty() ? 0 : nums[dq.front()]);
}
}
return result;
}

/*
-------------------------------------------------------
| 4. Maximum of All Subarrays of Size K |
| Cheat Code: Use deque to track max in each window |
-------------------------------------------------------
*/
vector<int> maxOfAllSubarrays(vector<int>& nums, int k) {
deque<int> dq;
vector<int> result;

for (int i = 0; i < nums.size(); i++) {


while (!dq.empty() && nums[i] >= nums[dq.back()]) dq.pop_back();
dq.push_back(i);
if (i >= k - 1) {
if (dq.front() <= i - k) dq.pop_front();
result.push_back(nums[dq.front()]);
}
}
return result;
}

/*
---------------------------------------------------------------
| 5. Count Occurrences of Anagram |
| Cheat Code: Frequency array, compare each window with goal |
---------------------------------------------------------------
*/
int countAnagrams(string txt, string pat) {
vector<int> freq(26, 0);
for (char ch : pat) freq[ch - 'a']++;

int count = 0, k = pat.size();


vector<int> window(26, 0);
for (int i = 0; i < txt.size(); i++) {
window[txt[i] - 'a']++;
if (i >= k) window[txt[i - k] - 'a']--;
if (i >= k - 1 && window == freq) count++;
}
return count;
}

/*
----------------------------------------------------------
| 6. Permutation in String |
| Cheat Code: Same as anagram check, return on match |
----------------------------------------------------------
*/
bool checkInclusion(string s1, string s2) {
if (s1.size() > s2.size()) return false;
vector<int> s1_count(26, 0), s2_count(26, 0);
for (char c : s1) s1_count[c - 'a']++;

for (int i = 0; i < s2.size(); i++) {


s2_count[s2[i] - 'a']++;
if (i >= s1.size()) s2_count[s2[i - s1.size()] - 'a']--;
if (s1_count == s2_count) return true;
}
return false;
}

/*
-------------------------------------------------------------
| 7. Find All Anagrams in a String |
| Cheat Code: Compare each sliding window with freq of p |
-------------------------------------------------------------
*/
vector<int> findAnagrams(string s, string p) {
vector<int> s_count(26, 0), p_count(26, 0);
vector<int> result;
if (s.size() < p.size()) return result;

for (int i = 0; i < p.size(); i++) {


s_count[s[i] - 'a']++;
p_count[p[i] - 'a']++;
}

if (s_count == p_count) result.push_back(0);

for (int i = p.size(); i < s.size(); i++) {


s_count[s[i] - 'a']++;
s_count[s[i - p.size()] - 'a']--;
if (s_count == p_count) result.push_back(i - p.size() + 1);
}

return result;
}

/*
----------------------------------------------------------
| 8. Repeated DNA Sequences |
| Cheat Code: Hash each 10-letter substring, count freq |
----------------------------------------------------------
*/
vector<string> findRepeatedDnaSequences(string s) {
unordered_map<string, int> mp;
vector<string> result;
int k = 10;
for (int i = 0; i + k <= s.length(); i++) {
string sub = s.substr(i, k);
if (++mp[sub] == 2) result.push_back(sub);
}
return result;
}

/*
--------------------------------------------------
| 9. Sliding Subarray Beauty (Skipped for now) |
--------------------------------------------------
*/

/*
--------------------------------------------------
| 10. Sliding Window Maximum - Already Covered |
--------------------------------------------------
*/

You might also like