DSA Notes
Essential algorithmic techniques every competitive programmer must know: two pointers, sliding window, binary search on answer, prefix sums, difference arrays, and bit manipulation.
Overview
These six techniques form the bread and butter of competitive programming. They appear in 70%+ of contest problems either directly or as building blocks for more complex solutions. Master these, and you can solve most Div 2 A-C problems on Codeforces.
Two Pointers
The two-pointer technique uses two indices that move through an array in a coordinated way, typically reducing an O(n²) brute force to O(n).
Pattern 1: Converging Pointers
Used when searching for pairs in a sorted array:
Pattern 2: Same-Direction Pointers
Used for problems involving subarrays or removing duplicates:
When to Use
- Array is sorted (or can be sorted without losing information)
- Looking for pairs/triplets with a target sum
- Merging two sorted arrays
- Partitioning problems
Sliding Window
A special case of two pointers where you maintain a "window" over a contiguous subarray, expanding the right boundary and shrinking the left boundary as needed.
Fixed-Size Window
Variable-Size Window (Shrinkable)
Variable-Size Window (Non-Shrinkable)
A clever variant where the window only grows or slides but never shrinks — useful for "find the longest" problems:
Binary Search on Answer
When the problem asks "what is the minimum/maximum value X such that condition(X) is true?", and condition is monotonic (once true, stays true), you can binary search on X.
Template
Classic Example: Minimum Days to Make M Bouquets
How to Recognize
- "Minimize the maximum" or "Maximize the minimum"
- Feasibility is monotonic: if X works, X+1 also works (or vice versa)
- You can write a
check(X)function that runs in O(n) or O(n log n)
Prefix Sums
Prefix sums let you compute the sum of any subarray in O(1) after O(n) preprocessing.
2D Prefix Sums
For matrix subregion queries:
Difference Arrays
The inverse of prefix sums. When you need to add a value to an entire range [l, r] multiple times, do it in O(1) per update and reconstruct the array at the end.
Use case: Q queries each adding a value to range [l, r]. Naive is O(Q * N). With difference arrays: O(Q + N).
Bit Manipulation
Bits let you encode sets, toggle states, and perform arithmetic tricks in O(1).
Essential Operations
x & (x - 1) // Remove lowest set bit (Brian Kernighan)
x | (1 << i) // Set bit i
x & ~(1 << i) // Clear bit i
x ^ (1 << i) // Toggle bit i
x & (-x) // Isolate lowest set bit
__builtin_popcount(x) // Count set bits
__builtin_clz(x) // Count leading zerosSubset Enumeration with Bitmasks
XOR Properties
a ^ a = 0(self-cancellation)a ^ 0 = a(identity)- XOR of all elements finds the unique element in "every element appears twice except one"
int findSingle(vector<int>& nums) {
int result = 0;
for (int x : nums) result ^= x;
return result;
}Summary Table
| Technique | Time Saved | When to Use |
|---|---|---|
| Two Pointers | O(n²) → O(n) | Sorted array, pair finding |
| Sliding Window | O(n²) → O(n) | Contiguous subarray optimization |
| Binary Search on Answer | Varies | Monotonic feasibility function |
| Prefix Sums | O(n) per query → O(1) | Range sum queries |
| Difference Arrays | O(n) per update → O(1) | Range update queries |
| Bit Manipulation | O(n) → O(1) | Set operations, subset enumeration |
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Common Competitive Programming Techniques.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, competitive, programming, common
Related Data Structures & Algorithms Topics