Overview
Common algorithmic techniques and patterns used in competitive programming to optimize solutions.Two Pointers
A technique using two pointers to traverse data structures, often reducing time complexity from O(n²) to O(n).Pattern 1: Two Sequences (Pointer-1 and Pointer-2)
Two pointers moving through different sequences.Pattern 2: Left and Right Boundary
Two pointers starting from opposite ends moving toward each other.Pattern 3: Slow and Fast Pointers
Two pointers moving at different speeds, often used for cycle detection.Pattern 4: Old and New State
Tracking previous and current positions.Sliding Window
A technique for finding subarrays that satisfy certain conditions.Common Sliding Window Problems
Fixed Size Window:Divide and Conquer
Break problem into smaller subproblems, solve recursively, and combine results.Sweep Line
Process events in sorted order, often used for interval problems.Available Implementations
| Technique | File | Description |
|---|---|---|
| Two Pointers (Two Sequences) | techniques_two_pointer1_pointer2.cpp | Different sequences |
| Two Pointers (Left-Right) | techniques_two_pointer_left_right_boundary.cpp | Opposite ends |
| Two Pointers (Slow-Fast) | techniques_two_pointers_slow_fast.cpp | Different speeds |
| Two Pointers (Old-New) | techniques_two_pointers_old_and_new_state.cpp | State tracking |
| Sliding Window | techniques_sliding_windows.cpp | Variable window |
| Divide and Conquer | techniques_divide_and_conquer.cpp | Recursive splitting |
| Sweep Line | techniques_sweep_line.cpp | Event processing |
Complexity Analysis
| Technique | Time | Space | Best For |
|---|---|---|---|
| Two Pointers | O(n) | O(1) | Sorted arrays |
| Sliding Window | O(n) | O(k) | Subarray problems |
| Divide and Conquer | O(n log n) | O(log n) | Recursive problems |
| Sweep Line | O(n log n) | O(n) | Interval problems |
When to Use
Two Pointers
Use for sorted arrays, pair problems, or when you need O(n) instead of O(n²)
Sliding Window
Use for contiguous subarray/substring problems with conditions
Divide and Conquer
Use when problem can be split into independent subproblems
Sweep Line
Use for interval overlap, merge, or event-based problems
See Also
Search Algorithms
Binary and ternary search techniques
Data Structures
Structures that complement these techniques