SLIDING WINDOW
SLIDING WINDOW
https://www.geeksforgeeks.org/problems/max-sum-subarray-of-size-k5313/1
https://www.geeksforgeeks.org/problems/first-negative-integer-in-every-
window-of-size-k3345/1
3. Count of occurrences of anagrams(fixed window)
https://www.geeksforgeeks.org/problems/count-occurences-of-
anagrams5839/1
1. Step that will take place in any way: Insert array element in the deque, but
pop all the smaller elements.
2. If window size is hit: Add the first element, and before shifting ‘i’ check, if it
is present in deque.
https://www.geeksforgeeks.org/problems/maximum-of-all-subarrays-of-size-
k3101/1
1. Step that will take place in any way: Add element to sum.
2. If sum==k: Update answer
3. If sum exceeds k: keep subtracting until it exceeds, then check, whether
now it has got equal to k
https://www.naukri.com/code360/problems/longest-subarray-with-sum-
k_6682399?leftPanelTabValue=PROBLEM
6. Longest Substring with K uniques - (no need to remove key)
https://www.geeksforgeeks.org/problems/longest-k-unique-characters-
substring0853/1
7. Longest substring without repeating characters
https://www.geeksforgeeks.org/problems/longest-distinct-characters-in-
string5848/1
8. Fruit into Baskets(lc-904)
Note: Here we get a possible answer at <=2, not ==2
if(mapSize<=2){
maxFruits = Math.max(maxFruits,j-i+1);
j++;
}
else if(mapSize>2){
while(mapSize>2){
int key2 = fruits[i];
int value2 =
map.getOrDefault(key2,0)-1;
map.put(key2,value2);
if(value2==0) mapSize--;
i++;
}
if(mapSize==2){
maxFruits = Math.max(maxFruits,j-
i+1);
}
j++;
}
}
return maxFruits;
while(j<n){
if(map.containsKey(s1.charAt(j))){
char key2 = s1.charAt(j);
int val2 = map.get(key2)-1;
map.put(key2,val2);
if(val2==0) mapSize--;
}
if(mapSize>0) j++;
else if(mapSize==0){
if(j-i+1<smallestLength){
smallestLength = j-i+1;
result = s1.substring(i,j+1);
}
while(mapSize==0){
if(map.containsKey(s1.charAt(i))){
char key3 = s1.charAt(i);
int val3 = map.get(key3)+1;
map.put(key3,val3);
if(val3==1){
if(j-i+1<smallestLength){
smallestLength = j-i+1;
result =
s1.substring(i,j+1);
}
mapSize++;
}
}
i++;
}
j++;
}
}
if(smallestLength == Integer.MAX_VALUE) return "";
return result;
}