Independently published, 2021. — 371 p. — ISBN B08V51PVCX.
Preparing for your incoming programming contest or coding interview? This book contains detailed explanations and source code for algorithms used in competitive programing. Written by software engineers with experience in programming competitions and developing high scalable systems for some of the biggest tech companies. It covers algorithms in the areas of Graph Theory, Number Theory, Combinatorics, Dynamic Programming, Geometry, and more. Competitive programming is frequently associated with algorithms, but an algorithm is just a set of instructions that are given to the computer to solve a specific problem that has already been solved in the head of the programmer. The goal of this book is to provide two things for each algorithm, a brief description of how it works and a source code that implements the theory behind the algorithm. Each section contains exercises and their respective solutions. Also the appendices contain problems with non-trivial solutions that we have selected to show the reader how sometimes simple algorithms can solve complex problems, and also how simple problems may require a complex solution. The source code of the exercises and other algorithms in this book can be found in the GitHub page: https://github.com/Cheetos/afcp. We have written this book because of our passion for algorithms, its content comes from trainings for programming competitions, from coaching and mentoring others, from failures and successes on tech interviews, from our experience in the tech industry, etc. We hope that you find this book useful for your objectives, and that you enjoy reading it as much as we enjoyed writing it.
Programming Contests
Coding Interviews
Online Judges
C and C++
Chapter Notes
FundamentalsRecursion
MemoizationAlgorithm Analysis
Asymptotic Notations
Master Theorem
P and NPBitwise Operations
AND (&) operator
OR (|) operator
XOR operator
Two’s complementChapter Notes
Exercises
Data StructuresLinear Data Structures
Stack
Queue
Linked ListTrees
Tree Traversal
Heap
Binary Search Tree (BST)
AVL Tree
Segment Tree
Binary Indexed Tree (BIT)
TrieStandard Template Library (STL) C++
Unordered Set
Ordered Set
Unordered Map
Ordered Map
Stack
Queue
Priority queueChapter Notes
Exercises
Sorting AlgorithmsBubble Sort
Selection Sort
Insertion Sort
Quick Sort
Counting Sort
Merge Sort
Heap Sort
Sorting With the algorithm Library
Overloading operator <
Adding a function to sortChapter Notes
Exercises
Divide and ConquerBinary Search
Binary Exponentiation
Closest Pair of Points
Polynomial Multiplication (FFT)
Range Minimum Query (RMQ)
Chapter Notes
Exercises
Dynamic ProgrammingLongest Increasing Sub-sequence (LIS)
Longest Increasing Subsequence with Binary SearchLongest Common Sub-sequence (LCS)
Levenshtein Distance (Edit Distance)
Knapsack Problem
Maximum Sum in a Sequence
Rectangle of Maximum Sum
Optimal Matrix Multiplication
Coin Change Problem
Chapter Notes
Exercises
Graph TheoryGraph Representation
Adjacency Matrix
Adjacency List
Graph Traversal
DFS
BFS
Topological SortDisjoint Sets
Union-FindShortest Paths
Dijkstra’s Algorithm
Bellman-Ford
Floyd-Warshall
A*Strongly Connected Components
Tarjan’s Algorithm
Kosaraju’s AlgorithmMinimum Spanning Tree
Kruskal’s Algorithm
Prim’s AlgorithmMaximum Bipartite Matching
Flow Network
Ford-Fulkerson AlgorithmChapter Notes
Exercises
GeometryPoint Inside a Polygon
Point in Convex Polygon
Point in Polygon
Point Inside a Triangle
Area of a Polygon
Line Intersection
Horner’s Rule
Centroid of a Convex Polygon
Convex Hull
Andrew’s Monotone Convex Hull Algorithm
Graham’s ScanChapter Notes
Exercises
Number Theory and CombinatoricsPrime Numbers
Sieve of Eratosthenes
Prime Number GeneratorEuler’s Totient Function
Sum of Divisors
Number of DivisorsModular Arithmetic
Euclidean Algorithm
Extended Euclidean AlgorithmBase Conversion
Sum of Number of Divisors
Combinations
Pascal’s TriangleCatalan Numbers
Josephus
Pigeon-Hole Principle
Chapter Notes
Exercises
String ManipulationNext Permutation
Knuth-Morris-Pratt Algorithm (KMP)
Manacher’s Algorithm
Chapter Notes
Exercises
Solution to ExercisesFundamentals
Data Structures
Sorting Algorithms
Divide and Conquer
Dynamic Programming
Graph Theory
Geometry
Number Theory and Combinatorics
String Manipulation
A Recursion and BitwiseThe 8-Queen problem
Vacations
B Data Structures ProblemsFind two numbers whose sum is k
Shunting-yard Algorithm
Find the median
C Sorting ProblemsMarching in the school
How Many Swaps?
Closest K Points to the Origin?
D Divide and Conquer ProblemsPolynomial Product
Wi-Fi Connection
E Graph Theory ProblemsMinmax and Maxmin
Credit Card (minmax)
LufeMart (maxmin)Money Exchange Problem
F Number Theory ProblemsSum of Consecutive Numbers
Two Queens in Attacking Positions
Example. Sum of GCD’s
Find B if LCM(A,B) = C
Last Non Zero Digit in n!