Coordinate two indices through a sequence to eliminate redundant candidate combinations in O(1) per step, dropping O(N²) brute force to O(N).
DSA Patterns
Learn common problem patterns, templates and techniques to solve DSA problems efficiently.
Maintain a moving contiguous window across arrays or strings to solve subarray/substring range queries in O(N) linear time, replacing redundant O(N²) or O(N·K) nested recalculations.
Precompute cumulative sums in a single O(N) pass to answer any contiguous range sum query in O(1) time and detect target subarray sums in O(N) using Hash Map frequency tracking.
Systematically halve monotonic search spaces at every step to locate target values or determine optimal feasibility boundaries in logarithmic O(log N) time instead of O(N) linear scan.
Move two pointers at different speeds (1x, 2x) to detect cycles and locate middle elements in linked structures.
Explore solution decision trees, prune invalid paths, and generate permutations, subsets, and combinations.
Solve complex recursive problems by breaking them into overlapping subproblems and memoizing states.
Construct global optimal solutions through a sequence of locally optimal choices without backtracking.
Maintain strictly increasing or decreasing element order in linear structures to solve next/previous queries in O(N).
Explore nodes and connected components systematically using queue-based BFS and recursive stack DFS.
Linear ordering of vertices in Directed Acyclic Graphs to resolve prerequisites and schedule task dependencies.
Tree structure storing character sequences along edges for fast prefix lookups, autocompletion, and word search.
Data structure that tracks partitioned disjoint sets with near-constant time union and find operations.
Use direct bitwise operations (AND, OR, XOR, shifts) for ultra-fast, space-efficient computation.
