A topic-wise C++ library of core algorithms, data structures and templates for competitive programming (Codeforces, CodeChef, AtCoder) and coding interviews. Every implementation is a standalone, runnable file with a matching theory note.
- 74 runnable C++ implementations across arrays, number theory, binary search, sorting, bit tricks, graphs, trees and range-query structures.
- A theory note for every implementation: when to use it, complexity, the core idea and the classic pitfalls.
- Contest cheatsheets: full STL reference, math formulas, modular arithmetic, combinatorics, probability, and a page of quick CP facts.
- Generic templates (Segment Tree, Lazy Segment Tree, Sparse Table) where you only change
Node/Updateper problem. - Verified: CI compiles every file and checks that every link and code fence in the docs is valid.
| I want to... | Go to |
|---|---|
| Copy a starting template | 8_misc/1_boilerplate.cpp |
| Remember an STL call | STL Cheatsheet |
| Look up a formula or identity | Math Formulas ยท CP Facts |
| Do range queries fast | Prefix Sum โ Fenwick โ Segment Tree |
| Solve a shortest-path problem | Dijkstra ยท Bellman-Ford ยท Floyd-Warshall |
| Answer |
Optimised nCr |
All conceptual notes, proofs and formula sheets live under 0_theory/. The layout mirrors the code folders, so 2_math/11_binary_exponentiation.cpp is explained in 0_theory/2_math/11_binary_exponentiation.md.
| Subject / Topic | Core Concepts Covered |
|---|---|
| ๐ฆ Arrays & Range Queries | Prefix sums, 2D prefix sums, suffix sums, difference arrays, subarray sum, two pointers, sliding window, Kadane |
| ๐ข Mathematics & Number Theory | Sieve, smallest prime factor, factorization, divisors, factorials, nCr, Legendre's formula, binary exponentiation, Euler's totient, extended GCD |
| ๐ Binary Search | Standard search, lower/upper bound, binary search on the answer (integer and decimal) |
| ๐ Sorting Algorithms | Merge sort (divide & conquer), quick sort (partitioning & pivot choice) |
| โก Bit Manipulation | Bit tricks, set/unset bit positions, subset enumeration, bitset and bitset subset-sum |
| ๐ธ๏ธ Graph Algorithms | DFS, BFS, Dijkstra, bipartite check, DSU, Kruskal, cycle detection, topological sort, SCC, Bellman-Ford, Floyd-Warshall, Tarjan bridges, DP on DAG |
| ๐ฒ Trees | Parent / children / subtree size, height, diameter, ancestor query, binary lifting + LCA, Euler tour flattening |
| ๐งฑ Segment Trees & Fenwick | Generic segment tree, lazy propagation, Fenwick tree (BIT) |
| ๐ ๏ธ Miscellaneous & Templates | Fast I/O + PBDS, sort map by values, matrix rotation, sparse table, full STL cheatsheet |
| Subject / Topic | Core Concepts Covered |
|---|---|
| ๐ Mathematics Formulas | Arithmetic, algebraic, geometric, volume and perimeter formulas |
| ๐ Coordinate Geometry | Midpoints, distances, triangle area via coordinates |
| ๐ชต Logarithms | Logarithmic identities, change of base, digit-counting tricks |
| ๐ฒ Combinatorics | Permutations, combinations, circular arrangements, Stars & Bars |
| ๐ Probability | Axioms, conditional probability, expected value, linearity |
| ๐ข Modular Arithmetic | Modular add/sub/mul/div rules, modular inverse |
| โก Fermat's Little Theorem | Statement, modular inverse via binary exponentiation, template |
| ๐งฎ GCD & LCM | Euclidean algorithm, LCMโGCD relation, GCD/LCM properties |
| ๐ Divisibility Rules | Quick divisibility checks for 2 through 12, 25 and 100 |
| ๐งฟ Euler's Totient |
|
| ๐ค ASCII Table | ASCII values, char arithmetic, string conversion utilities |
| ๐ก CP Facts & Observations | Prime gaps, divisor counts, coprime facts, rounding to multiples, pigeonhole, limits table |
| ๐งฐ STL Cheatsheet | Algorithms, conversions, string, vector, set, map, pair, stack, queue, deque, priority queue |
Click a filename for the implementation, or ๐ for the theory note.
๐ฆ 1. Arrays & Range Queries ย ยทย 8 implementations
| # | Algorithm / Technique | Code | Notes |
|---|---|---|---|
| 1 | Prefix Sum Array | 1_prefix_sum.cpp |
๐ |
| 2 | 2D Prefix Sum Matrix | 2_2d_prefix_sum.cpp |
๐ |
| 3 | Suffix Sum Array | 3_suffix_sum.cpp |
๐ |
| 4 | Difference Array | 4_difference_array.cpp |
๐ |
| 5 | Subarray Sum Equals K | 5_subarray_sum.cpp |
๐ |
| 6 | Two Pointers Technique | 6_two_pointers.cpp |
๐ |
| 7 | Sliding Window Max Sum | 7_sliding_windows.cpp |
๐ |
| 8 | Kadane's Algorithm (Max Subarray) | 8_kadane_algorithm.cpp |
๐ |
๐ข 2. Mathematics & Number Theory ย ยทย 16 implementations
| # | Algorithm / Technique | Code | Notes |
|---|---|---|---|
| 1 | Sieve of Eratosthenes | 1_sieve_of_eratosthenes.cpp |
๐ |
| 2 | Sieve โ Smallest Prime Factor (SPF) | 2_sieve_smallest_prime_factor.cpp |
๐ |
| 3 | Distinct Prime Factorization | 3_prime_factors.cpp |
๐ |
| 4 | Efficient Factorization via SPF | 4_efficient_prime_factor_using_spf.cpp |
๐ |
| 5 | Smallest Prime Factor (Single) | 5_smallest_prime_factor.cpp |
๐ |
| 6 | Prime Check (Trial Division) | 6_prime_check.cpp |
๐ |
| 7 | Divisors / Factors of a Number | 7_factors_of_number.cpp |
๐ |
| 8 | Factorial with Modulo | 8_factorial.cpp |
๐ |
| 9 | Inverse Factorial (Fermat's LT) | 9_inverse_factorial.cpp |
๐ |
| 10 | Optimized nCr Calculations | 10_optimised_ncr.cpp |
๐ |
| 11 | Binary Exponentiation | 11_binary_exponentiation.cpp |
๐ |
| 12 | nCr Simple Formula | 12_ncr.cpp |
๐ |
| 13 | Legendre's Formula (Power of p in N!) | 13_power_of_x_in_factorial.cpp |
๐ |
| 14 | Power of x in N | 14_power_of_x_in_n.cpp |
๐ |
| 15 | Euler's Totient (Single + Sieve) | 15_euler_totient.cpp |
๐ |
| 16 | Extended GCD & Modular Inverse (Any Modulus) | 16_extended_gcd.cpp |
๐ |
๐ 3. Binary Search ย ยทย 5 implementations
| # | Algorithm / Technique | Code | Notes |
|---|---|---|---|
| 1 | Standard Binary Search | 1_binary_search.cpp |
๐ |
| 2 | Lower Bound | 2_lower_bound.cpp |
๐ |
| 3 | Upper Bound | 3_upper_bound.cpp |
๐ |
| 4 | Binary Search on Answers (Integer) | 4_binary_search_on_answers.cpp |
๐ |
| 5 | Binary Search on Answers (Decimal) | 5_binary_search_on_decimals.cpp |
๐ |
๐ 4. Sorting Algorithms ย ยทย 2 implementations
| # | Algorithm / Technique | Code | Notes |
|---|---|---|---|
| 1 | Merge Sort | 1_merge_sort.cpp |
๐ |
| 2 | Quick Sort | 2_quick_sort.cpp |
๐ |
โก 5. Bit Manipulation ย ยทย 8 implementations
| # | Algorithm / Technique | Code | Notes |
|---|---|---|---|
| 1 | Count Set Bits (Kernighan's Algorithm) | 1_count_the_set_bits.cpp |
๐ |
| 2 | Lowest Set Bit Position | 2_lowest_set_bit_position.cpp |
๐ |
| 3 | Highest Set Bit Position | 3_highest_set_bit_position.cpp |
๐ |
| 4 | Lowest Unset Bit Position | 4_lowest_unset_bit_position.cpp |
๐ |
| 5 | Highest Unset Bit Position | 5_highest_unset_bit_position.cpp |
๐ |
| 6 | Highest Unset Bit (Significant Bits) | 6_highest_unset_bit_among_significant_bits.cpp |
๐ |
| 7 | Generate All Subsets via Bitmasking | 7_generate_all_subset_using_bitmasking.cpp |
๐ |
| 8 | Bitset Subset Sum | 8_bitset_subset_sum.cpp |
๐ |
๐ธ๏ธ 6. Graph Algorithms ย ยทย 16 implementations
| # | Algorithm / Technique | Code | Notes |
|---|---|---|---|
| 1 | Depth First Search (DFS) | 1_depth_first_search.cpp |
๐ |
| 2 | Breadth First Search (BFS) | 2_breadth_first_search.cpp |
๐ |
| 3 | Find Level of Every Node | 3_find_level_of_every_node.cpp |
๐ |
| 4 | DFS Entry / Exit Timers (Euler Tour) | 4_in_time_out_time.cpp |
๐ |
| 5 | Dijkstra (SSSP, Non-Negative Weights) | 5_dijkstra.cpp |
๐ |
| 6 | Bipartite Check | 6_bipartite_check.cpp |
๐ |
| 7 | Disjoint Set Union (DSU) | 7_disjoint_set_union.cpp |
๐ |
| 8 | Kruskal Minimum Spanning Tree | 8_kruskal_mst.cpp |
๐ |
| 9 | Directed Cycle Detection | 9_directed_cycle_detection.cpp |
๐ |
| 10 | Topological Sort (Kahn) | 10_topological_sort_kahn.cpp |
๐ |
| 11 | Topological Sort (DFS) | 11_topological_sort_dfs.cpp |
๐ |
| 12 | Kosaraju SCC | 12_kosaraju_scc.cpp |
๐ |
| 13 | Bellman-Ford | 13_bellman_ford.cpp |
๐ |
| 14 | Floyd-Warshall | 14_floyd_warshall.cpp |
๐ |
| 15 | Tarjan's Bridges | 15_tarjan_bridges.cpp |
๐ |
| 16 | DP on a DAG (Longest Path) | 16_dp_on_dag.cpp |
๐ |
๐ฒ 7. Trees ย ยทย 12 implementations
| # | Algorithm / Technique | Code | Notes |
|---|---|---|---|
| 1 | Precompute Parent of Every Node | 1_get_the_parent_of_the_node.cpp |
๐ |
| 2 | Count Children for Every Node | 2_number_of_children_for_a_node.cpp |
๐ |
| 3 | Find Subtree Size | 3_find_subtree_size.cpp |
๐ |
| 4 | Farthest Leaf Node (Node Height) | 4_farthest_leaf_node_inside_subtree.cpp |
๐ |
| 5 | Diameter of a Tree | 5_diameter_of_tree.cpp |
๐ |
| 6 | Ancestor Query in O(1) | 6_ancestor_query.cpp |
๐ |
| 7 | Depth First Search (DFS) | 7_depth_first_search.cpp |
๐ |
| 8 | Breadth First Search (BFS) | 8_breadth_first_search.cpp |
๐ |
| 9 | Find Level of Every Node | 9_find_level_of_every_node.cpp |
๐ |
| 10 | DFS Entry / Exit Timers (Euler Tour) | 10_in_time_out_time.cpp |
๐ |
| 11 | Binary Lifting + LCA | 11_binary_lifting_lca.cpp |
๐ |
| 12 | Euler Tour Flattening (Subtree to Range) | 12_euler_tour_flattening.cpp |
๐ |
๐ ๏ธ 8. Miscellaneous & Boilerplate ย ยทย 4 implementations
| # | Algorithm / Technique | Code | Notes |
|---|---|---|---|
| 1 | Competitive Programming Boilerplate (Fast I/O + PBDS) | 1_boilerplate.cpp |
๐ |
| 2 | Sort std::map by Values |
2_sort_map_by_values.cpp |
๐ |
| 3 | Matrix Rotation & Mirroring | 3_matrix_rotation.cpp |
๐ |
| 4 | Sparse Table (Generic, O(1) Idempotent Queries) | 4_sparse_table.cpp |
๐ |
๐งฑ 9. Segment Trees & Fenwick ย ยทย 3 implementations
| # | Algorithm / Technique | Code | Notes |
|---|---|---|---|
| 1 | Segment Tree (Generic, Point Update + Range Query) | 1_segment_tree.cpp |
๐ |
| 2 | Lazy Segment Tree (Generic, Range Update + Range Query) | 2_lazy_segment_tree.cpp |
๐ |
| 3 | Fenwick Tree (BIT) | 3_fenwick_tree.cpp |
๐ |
.
โโโ 0_theory/ # notes: mirrors the code folders + reference sheets
โ โโโ 1_arrays/ โฆ 9_segment_tree/
โ โโโ *.md # math_formulas, combinatorics, cp_facts, ...
โโโ 1_arrays/ # prefix sums, two pointers, Kadane, ...
โโโ 2_math/ # sieve, factorization, nCr, totient, extgcd, ...
โโโ 3_binary_search/
โโโ 4_sorting/
โโโ 5_bit_manipulation/
โโโ 6_graphs/ # traversals, shortest paths, DSU, SCC, bridges, ...
โโโ 7_trees/ # LCA, diameter, Euler tour, ...
โโโ 8_misc/ # boilerplate, sparse table, matrix rotation, ...
โโโ 9_segment_tree/ # segment tree, lazy segment tree, Fenwick
โโโ scripts/ # check.sh (compile all) + lint_docs.py (validate docs)
โโโ .github/workflows/ # CI