Start one pointer at each end and move them towards each other, using a rule to decide which one moves.
Clues
- Sorted array or string
- Find a pair (or triple) with a target sum
- Palindrome checks
- "Container" or "area between two lines"
Time O(n) · Space O(1)
A fast pointer reads every element and a slow pointer marks where the next kept element should be written.
Clues
- Remove or move elements in place
- "Return the new length"
- Remove duplicates from a sorted array
- Move zeroes to the end
Time O(n) · Space O(1)
Keep a window of exactly k elements; add the element entering and remove the one leaving instead of recomputing.
Clues
- "Subarray or substring of size k"
- Maximum or average of every k-length window
- Anagram or permutation of a fixed-length pattern inside a string
Time O(n) · Space O(1) or O(alphabet)
Grow the window with the right pointer; when it breaks a rule, shrink it from the left until it's valid again.
Clues
- "Longest" or "shortest" substring or subarray with a condition
- "At most K distinct", "without repeating", "sum at least S"
- Contiguous range plus a constraint
Time O(n) · Space O(alphabet) or O(k)
Store running totals so the sum of any range is one subtraction: prefix[r + 1] - prefix[l].
Clues
- Many range-sum queries
- "Sum of subarray from i to j"
- Values don't change between queries
- Product or count over ranges
Time O(n) build, O(1) per query · Space O(n)
Apply many "add v to range [l, r]" updates in O(1) each by marking +v at l and -v at r + 1, then take a prefix sum once.
Clues
- Many range updates, then read the final array once
- Bookings, flights or seats over ranges of days
- "Add value to every element between i and j"
Time O(n + updates) · Space O(n)
Walk the input once and keep a few variables (best so far, minimum so far, a count) that summarise everything seen.
Clues
- "Maximum/minimum so far"
- Best profit from buying before selling
- Answer depends only on a summary of the past, not every past element
- O(n) time and O(1) space expected
Time O(n) · Space O(1)
Treat every index (and every gap between two indexes) as the middle of a palindrome and grow outwards while both sides match.
Clues
- Longest palindromic substring
- Count palindromic substrings
- Symmetry around a middle point
- n up to a few thousand (O(n²) is fine)
Time O(n²) · Space O(1)
Scan once, keeping the best sum of a subarray ending here: either extend the previous one or start fresh.
Clues
- Maximum (or minimum) subarray sum
- "Contiguous subarray" with the best total
- Best profit from one buy and one sell
Time O(n) · Space O(1)
Walk a 2D grid in a controlled order (rows, columns, spiral, diagonals) using boundaries or direction arrays.
Clues
- 2D array or grid input
- Spiral order
- Rotate an image
- Set rows/columns to zero
Time O(rows × cols) · Space O(1) extra