Posts

Showing posts from January, 2025

22. Sliding Window & 2 pointer

Image
Conditions for Sliding Window- Monotonic condition Subarray sum problems with +ve numbers Window behave predictably upon expand / shrink If, above conditions gets violated in a subarray problem, we go with below methods - 1. prefix sum + hashmap 2. DP 3. Binary Search --------------------------------------------------------------------------------------------------------------------------- 4 Patterns in Sliding Window- (striver) 1. Fix window - ❄️ B uild 1st window of size k => slide window (L++, R++) => update state =>update ans ----------------------------------------------------- 2.  Variable window-1 - (Find longest subarray satisfying a condition ) - for each R, the  smallest  index L (which keeps window valid) is chosen. ❄️ expand R => while window invalid: (shrink L) => now, update answer --------------------------------------------------- 3. Variable window-2 - (Count subarrays satisfying a condition ) - (current window invalid => all large...

21. Backtracking

Image
# Two types of Recursion - 1. parameterised- ans is in the parameters of the function call and it gets updated in each call 2. functional- function returns answer in each call --------------------------------------------------------------------------------------------------------------- # Note - 1. S ubsequence is formed by either picking or skipping each element while maintaining order.  2. Subset do not follow any order in the elements 3. For n elements there are 2^n subsequences. 4. If you get duplicates, then first sort the array and then in the backtracking code, in the not take part remember to take into effect of skipping other same elements. 5. order: (base case check--> pruning --> recursion ) 6.  In backtracking, any value you want to accumulate across recursive calls must be passed by reference 7. Types of pick not pick - print all/ count / check if exists / print any one ----- # converting print all to print any one code- - Stop and return the answer the mom...