Recognize problem input signals, inspect step-by-step visual chalkboard diagrams, apply core tricks, and copy code blueprints!
Use two index pointers to traverse a data structure (array, string, or linked list) from opposite ends or at different speeds.
Shrink or expand search space from both ends. If `sum < target`, move `left++` to increase value. If `sum > target`, move `right--` to decrease value.
[ 1 , 3 , 5 , 7 , 11 , 15 ]
↑ ↑
Left Right
(sum = 16 > 12 -> move Right left ◄─)
[ 1 , 3 , 5 , 7 , 11 , 15 ]
↑ ↑
Left Right
(sum = 12 === 12 -> Target Found! ✓)Maintain a contiguous subarray/substring window that dynamically expands or shrinks based on problem constraints.
Store cumulative prefix sums in a Hash Map to find contiguous subarrays whose sum equals K or matches a target modulo/remainder in O(N) time.
Sort an array of 3 distinct values (e.g. 0s, 1s, and 2s) in a single pass in-place using 3 pointers (`low`, `mid`, `high`).
Find the element that appears more than ⌊N/2⌋ times in an array in O(N) time and O(1) extra space.
Calculate an array `res` where `res[i]` is the product of all elements except `nums[i]` in O(N) time without using division.
Rotate an array by K steps or find the next lexicographical permutation by reversing sub-sections of the array in-place.
Use two pointers moving at different speeds (usually 1 step vs 2 steps) to detect cycles or find middle nodes in linked lists and arrays.
Sort an array of numbers in the range 1 to N in O(N) time and O(1) space by placing each number at its correct index (`nums[i] - 1`).
Reverse pointers of a linked list in-place without creating new nodes using 3 tracking pointers: `prev`, `curr`, and `next`.
Sort overlapping intervals by start time, then merge adjacent intervals that overlap.
Find the maximum sum of a contiguous subarray in O(N) time using DP local vs global running sum.
Find palindromic substrings by expanding outward from each character (odd length) and pair of characters (even length).
Traverse a tree or graph level by level using a Queue (FIFO data structure).
Traverse deep down a tree path before backtracking using Recursion or an explicit Stack.
Traverse a 2D grid/matrix using DFS or BFS with 4-directional or 8-directional cell movements.
Start BFS queue with ALL source nodes simultaneously to find shortest distance transform or propagation time.
Find shortest distance from source node to all other nodes in a weighted graph with non-negative edge weights.
Find shortest paths between ALL pairs of vertices in a weighted graph in O(V^3) time using 3D/2D DP.
Order vertices of a Directed Acyclic Graph (DAG) such that for every directed edge u -> v, u comes before v.
Maintain a collection of disjoint sets, supporting near O(1) operations to find set representative and union two sets.
Explore all possible combinations/permutations by building candidate solutions incrementally and undoing choices (backtracking) when a branch fails.
Prune redundant branch choices when input array contains duplicate values (e.g. Subsets II, Combination Sum II).
Break complex optimization/counting problems into overlapping subproblems, storing subproblem results to eliminate redundant computation.
Choose a subset of items each with weight and value to maximize total value without exceeding capacity W (each item used at most ONCE).
Select items to reach a target sum or max value where items can be reused UNLIMITED times.
Compare two strings `s1` and `s2` to find matching character sequences or transform distance.
Maintain elements in stack/queue in strictly increasing or decreasing order to quickly find the Next Greater or Next Smaller Element.
Keep track of the top K largest or smallest elements from an unsorted collection or stream.
Find the K-th smallest/largest element in an unsorted array in average O(N) linear time using QuickSort partition logic.
Merge K sorted arrays or K sorted linked lists into a single sorted list using a Min-Heap of size K.
Tree structure storing characters at each node to enable fast string insertion, prefix lookup, and auto-complete.
Search for pattern P in text T in O(N + M) time using Longest Proper Prefix which is also Suffix (LPS) array to avoid redundant comparisons.
Use a fixed-size frequency array of 26 integers or Hash Map to validate anagrams, isomorphic strings, and character distributions.
Use polynomial rolling hash to search for pattern P of length M in text T of length N in average O(N + M) time.
Compress consecutive identical characters in a string/array in-place by writing character followed by its frequency count.
Use a Stack to parse nested string structures, parenthesized expressions, and multiplier strings like `3[a2[c]]`.
Utilize bitwise operators (`&`, `|`, `^`, `~`, `<<`, `>>`) for fast constant-time arithmetic and boolean logic.
Make the locally optimal choice at each step to reach a global optimum solution.