text stringlengths 183 150k | path stringlengths 15 112 | bucket stringclasses 4
values | source stringclasses 14
values | group stringclasses 121
values | spdx stringclasses 11
values | repo_license stringclasses 11
values | commit stringclasses 13
values | lang stringclasses 4
values | def_heavy bool 1
class | comment_ratio float64 0 1 | restricted bool 2
classes | fresh bool 1
class | n_bytes int64 183 150k | n_tokens int64 -1 -1 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---
tags:
- Translated
e_maxx_link: string_hashes
---
# String Hashing
Hashing algorithms are helpful in solving a lot of problems.
We want to solve the problem of comparing strings efficiently.
The brute force way of doing so is just to compare the letters of both strings, which has a time complexity of $O(\min(n... | cp-algorithms/src/string/string-hashing.md | algorithms | cp-algorithms | cp-algorithms/src/string | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.185 | false | false | 12,488 | -1 |
---
tags:
- Translated
e_maxx_link: assignment_hungary
---
# Hungarian algorithm for solving the assignment problem
## Statement of the assignment problem
There are several standard formulations of the assignment problem (all of which are essentially equivalent). Here are some of them:
- There are $n$ jobs and $n... | cp-algorithms/src/graph/hungarian-algorithm.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.112 | false | false | 28,359 | -1 |
---
tags:
- Translated
e_maxx_link: extended_euclid_algorithm
---
# Extended Euclidean Algorithm
While the [Euclidean algorithm](euclid-algorithm.md) calculates only the greatest common divisor (GCD) of two non-negative integers $a$ and $b$, the extended version also finds a way to represent GCD in terms of $a$ and... | cp-algorithms/src/algebra/extended-euclid-algorithm.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.114 | false | false | 5,259 | -1 |
---
tags:
- Translated
e_maxx_link: balanced_ternary
---
# Balanced Ternary

This is a non-standard but still positional **numeral system**. Its feature is that digits can have one of the values `-1... | cp-algorithms/src/algebra/balanced-ternary.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.138 | false | false | 3,142 | -1 |
---
title: Checking a graph for acyclicity and finding a cycle in O(M)
tags:
- Translated
e_maxx_link: finding_cycle
---
# Checking a graph for acyclicity and finding a cycle in $O(M)$
Consider a directed or undirected graph without loops and multiple edges. We have to check whether it is acyclic, and if it is not, ... | cp-algorithms/src/graph/finding-cycle.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.053 | false | false | 4,235 | -1 |
---
tags:
- Translated
e_maxx_link: dfs
---
# Depth First Search
Depth First Search is one of the main graph algorithms.
Depth First Search finds the lexicographical first path in the graph from a source vertex $u$ to each vertex.
Depth First Search will also find the shortest paths in a tree (because there only e... | cp-algorithms/src/graph/depth-first-search.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.504 | false | false | 10,290 | -1 |
---
tags:
- Original
---
# Convex hull trick and Li Chao tree
Consider the following problem. There are $n$ cities. You want to travel from city $1$ to city $n$ by car. To do this you have to buy some gasoline. It is known that a liter of gasoline costs $cost_k$ in the $k^{th}$ city. Initially your fuel tank is emp... | cp-algorithms/src/geometry/convex_hull_trick.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.137 | false | false | 9,059 | -1 |
---
tags:
- Translated
e_maxx_link: dijkstra_sparse
---
# Dijkstra on sparse graphs
For the statement of the problem, the algorithm with implementation and proof can be found on the article [Dijkstra's algorithm](dijkstra.md).
## Algorithm
We recall in the derivation of the complexity of Dijkstra's algorithm we u... | cp-algorithms/src/graph/dijkstra_sparse.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.067 | false | false | 7,419 | -1 |
---
tags:
- Original
---
# Half-plane intersection
In this article we will discuss the problem of computing the intersection of a set of half-planes. Such an intersection can be conveniently represented as a convex region/polygon, where every point inside of it is also inside all of the half-planes, and it is this ... | cp-algorithms/src/geometry/halfplane-intersection.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.341 | false | false | 25,091 | -1 |
---
tags:
- Translated
e_maxx_link: prime_sieve_linear
---
# Linear Sieve
Given a number $n$, find all prime numbers in a segment $[2;n]$.
The standard way of solving a task is to use [the sieve of Eratosthenes](sieve-of-eratosthenes.md). This algorithm is very simple, but it has runtime $O(n \log \log n)$.
Altho... | cp-algorithms/src/algebra/prime-sieve-linear.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.133 | false | false | 4,876 | -1 |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#previewBtn").click(function(){
var markdown = $("#markdownInput").val();
var URL = "https://us-central1-cp-algorithms.cloudfunctions.net/convert-ma... | cp-algorithms/src/preview.md | algorithms | cp-algorithms | cp-algorithms | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.038 | false | false | 1,548 | -1 |
---
tags:
- Original
title: MEX (minimal excluded) of a sequence
---
# MEX (minimal excluded) of a sequence
Given an array $A$ of size $N$. You have to find the minimal non-negative element that is not present in the array. That number is commonly called the **MEX** (minimal excluded).
$$
\begin{align}
\text{mex}... | cp-algorithms/src/sequences/mex.md | algorithms | cp-algorithms | cp-algorithms/src/sequences | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.092 | false | false | 4,544 | -1 |
---
tags:
- Translated
e_maxx_link: dsu
---
# Disjoint Set Union
This article discusses the data structure **Disjoint Set Union** or **DSU**.
Often it is also called **Union Find** because of its two main operations.
This data structure provides the following capabilities.
We are given several elements, each of wh... | cp-algorithms/src/data_structures/disjoint_set_union.md | algorithms | cp-algorithms | cp-algorithms/src/data_structures | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.069 | false | false | 29,307 | -1 |
---
tags:
- Translated
e_maxx_link: rabin_karp
---
# Rabin-Karp Algorithm for string matching
This algorithm is based on the concept of hashing, so if you are not familiar with string hashing, refer to the [string hashing](string-hashing.md) article.
This algorithm was authored by Rabin and Karp in 1987.
Problem... | cp-algorithms/src/string/rabin-karp.md | algorithms | cp-algorithms | cp-algorithms/src/string | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.205 | false | false | 2,188 | -1 |
---
tags:
- Translated
e_maxx_link: stacks_for_minima
---
# Minimum stack / Minimum queue
In this article we will consider three problems:
first we will modify a stack in a way that allows us to find the smallest element of the stack in $O(1)$, then we will do the same thing with a queue, and finally we will use t... | cp-algorithms/src/data_structures/stack_queue_modification.md | algorithms | cp-algorithms | cp-algorithms/src/data_structures | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.163 | false | false | 6,997 | -1 |
---
tags:
- Translated
e_maxx_link: bfs
---
# Breadth-first search
Breadth first search is one of the basic and essential searching algorithms on graphs.
As a result of how the algorithm works, the path found by breadth first search to any node is the shortest path to that node, i.e the path that contains the smal... | cp-algorithms/src/graph/breadth-first-search.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.26 | false | false | 10,120 | -1 |
---
title: Finding bridges in a graph in O(N+M)
tags:
- Translated
e_maxx_link: bridge_searching
---
# Finding bridges in a graph in $O(N+M)$
We are given an undirected graph. A bridge is defined as an edge which, when removed, makes the graph disconnected (or more precisely, increases the number of connected compon... | cp-algorithms/src/graph/bridge-searching.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.167 | false | false | 6,512 | -1 |
---
tags:
- Translated
e_maxx_link: inclusion_exclusion_principle
---
# The Inclusion-Exclusion Principle
The inclusion-exclusion principle is an important combinatorial way to compute the size of a set or the probability of complex events. It relates the sizes of individual sets with their union.
## Statement
##... | cp-algorithms/src/combinatorics/inclusion-exclusion.md | algorithms | cp-algorithms | cp-algorithms/src/combinatorics | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.203 | false | false | 29,206 | -1 |
---
tags:
- Translated
e_maxx_link: mst_kruskal_with_dsu
---
# Minimum spanning tree - Kruskal with Disjoint Set Union
For an explanation of the MST problem and the Kruskal algorithm, first see the [main article on Kruskal's algorithm](mst_kruskal.md).
In this article we will consider the data structure ["Disjoint... | cp-algorithms/src/graph/mst_kruskal_with_dsu.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.094 | false | false | 2,372 | -1 |
---
tags:
- Translated
e_maxx_link: mst_prim
---
# Minimum spanning tree - Prim's algorithm
Given a weighted, undirected graph $G$ with $n$ vertices and $m$ edges.
You want to find a spanning tree of this graph which connects all vertices and has the least weight (i.e. the sum of weights of edges is minimal).
A spa... | cp-algorithms/src/graph/mst_prim.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.062 | false | false | 10,740 | -1 |
---
title: Finding area of simple polygon in O(N)
tags:
- Translated
e_maxx_link: polygon_area
---
# Finding area of simple polygon in $O(N)$
Let a simple polygon (i.e. without self intersection, not necessarily convex) be given. It is required to calculate its area given its vertices.
## Method 1
This is easy to ... | cp-algorithms/src/geometry/area-of-simple-polygon.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.192 | false | false | 1,213 | -1 |
---
tags:
- Translated
e_maxx_link: linear_systems_gauss
---
# Gauss method for solving system of linear equations
Given a system of $n$ linear algebraic equations (SLAE) with $m$ unknowns. You are asked to solve the system: to determine if it has no solution, exactly one solution or infinite number of solutions. A... | cp-algorithms/src/linear_algebra/linear-system-gauss.md | algorithms | cp-algorithms | cp-algorithms/src/linear_algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.221 | false | false | 12,367 | -1 |
---
tags:
- Translated
e_maxx_link: matrix_rank
---
# Finding the rank of a matrix
**The rank of a matrix** is the largest number of linearly independent rows/columns of the matrix. The rank is not only defined for square matrices.
The rank of a matrix can also be defined as the largest order of any non-zero mino... | cp-algorithms/src/linear_algebra/rank-matrix.md | algorithms | cp-algorithms | cp-algorithms/src/linear_algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.191 | false | false | 2,176 | -1 |
---
tags:
- Translated
e_maxx_link: prufer_code_cayley_formula
---
# Prüfer code
In this article we will look at the so-called **Prüfer code** (or Prüfer sequence), which is a way of encoding a labeled tree into a sequence of numbers in a unique way.
With the help of the Prüfer code we will prove **Cayley's formul... | cp-algorithms/src/graph/pruefer_code.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.058 | false | false | 14,736 | -1 |
---
tags:
- Translated
e_maxx_link: heavy_light
---
# Heavy-light decomposition
**Heavy-light decomposition** is a fairly general technique that allows us to effectively solve many problems that come down to **queries on a tree** .
## Description
Let there be a tree $G$ of $n$ vertices, with an arbitrary root.
... | cp-algorithms/src/graph/hld.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.126 | false | false | 12,089 | -1 |
---
title: Finding articulation points in a graph in O(N+M)
tags:
- Translated
e_maxx_link: cutpoints
---
# Finding articulation points in a graph in $O(N+M)$
We are given an undirected graph. An articulation point (or cut vertex) is defined as a vertex which, when removed along with associated edges, makes the grap... | cp-algorithms/src/graph/cutpoints.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.087 | false | false | 4,665 | -1 |
---
tags:
- Translated
e_maxx_link: randomized_heap
---
# Randomized Heap
A randomized heap is a heap that, through using randomization, allows to perform all operations in expected logarithmic time.
A **min heap** is a binary tree in which the value of each vertex is less than or equal to the values of its childr... | cp-algorithms/src/data_structures/randomized_heap.md | algorithms | cp-algorithms | cp-algorithms/src/data_structures | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.097 | false | false | 5,728 | -1 |
---
tags:
- Translated
e_maxx_link: triangles_union
---
# Vertical decomposition
## Overview
Vertical decomposition is a powerful technique used in various geometry problems. The general idea is to cut the plane into several vertical stripes
with some "good" properties and solve the problem for these stripes indepe... | cp-algorithms/src/geometry/vertical_decomposition.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.056 | false | false | 11,012 | -1 |
---
tags:
- Translated
e_maxx_link: kuhn_matching
---
# Kuhn's Algorithm for Maximum Bipartite Matching
## Problem
You are given a bipartite graph $G$ containing $n$ vertices and $m$ edges. Find the maximum matching, i.e., select as many edges as possible so
that no selected edge shares a vertex with any other sel... | cp-algorithms/src/graph/kuhn_maximum_bipartite_matching.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.206 | false | false | 15,323 | -1 |
---
tags:
- Translated
e_maxx_link:
- rib_connectivity
- vertex_connectivity
---
# Edge connectivity / Vertex connectivity
## Definition
Given an undirected graph $G$ with $n$ vertices and $m$ edges.
Both the edge connectivity and the vertex connectivity are characteristics describing the graph.
### Edge conn... | cp-algorithms/src/graph/edge_vertex_connectivity.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.217 | false | false | 5,562 | -1 |
---
tags:
- Original
---
# Lattice points inside non-lattice polygon
For lattice polygons there is Pick's formula to enumerate the lattice points inside the polygon.
What about polygons with arbitrary vertices?
Let's process each of the polygon's edges individually, and after that we may sum up the amounts of latt... | cp-algorithms/src/geometry/lattice-points.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.069 | false | false | 5,175 | -1 |
---
tags:
- Translated
e_maxx_link: ford_bellman
---
# Bellman-Ford Algorithm
**Single source shortest path with negative weight edges**
Suppose that we are given a weighted directed graph $G$ with $n$ vertices and $m$ edges, and some specified vertex $v$. You want to find the length of shortest paths from vertex ... | cp-algorithms/src/graph/bellman_ford.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.122 | false | false | 15,716 | -1 |
---
tags:
- Translated
e_maxx_link: binary_pow
---
# Binary Exponentiation
Binary exponentiation (also known as exponentiation by squaring) is a trick which allows to calculate $a^n$, where $n$ is a non-negative integer, using only $O(\log n)$ multiplications (instead of $O(n)$ multiplications required by the naive... | cp-algorithms/src/algebra/binary-exp.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.247 | false | false | 13,928 | -1 |
---
tags:
- Translated
e_maxx_link: lca_linear_offline
---
# Lowest Common Ancestor - Tarjan's off-line algorithm
We have a tree $G$ with $n$ nodes and we have $m$ queries of the form $(u, v)$.
For each query $(u, v)$ we want to find the lowest common ancestor of the vertices $u$ and $v$, i.e. the node that is an a... | cp-algorithms/src/graph/lca_tarjan.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.132 | false | false | 4,567 | -1 |
---
tags:
- Translated
e_maxx_link: segments_intersection_checking
---
# Check if two segments intersect
You are given two segments $(a, b)$ and $(c, d)$.
You have to check if they intersect.
Of course, you may find their intersection and check if it isn't empty, but this can't be done in integers for segments with... | cp-algorithms/src/geometry/check-segments-intersection.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.163 | false | false | 1,907 | -1 |
---
tags:
- Translated
e_maxx_link: fibonacci_numbers
---
# Fibonacci Numbers
The Fibonacci sequence is defined as follows:
$$F_0 = 0, F_1 = 1, F_n = F_{n-1} + F_{n-2}$$
The first elements of the sequence ([OEIS A000045](http://oeis.org/A000045)) are:
$$0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...$$
## Properti... | cp-algorithms/src/algebra/fibonacci-numbers.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.151 | false | false | 9,722 | -1 |
---
tags:
- Original
---
# Centroid Decomposition
Prerequisite knowledge: [Depth First Search (DFS)](./depth-first-search.md), [Divide and Conquer](https://en.wikipedia.org/wiki/Divide-and-conquer_algorithm), [Trees](<https://en.wikipedia.org/wiki/Tree_(graph_theory)>).
## Introduction
**Centroid Decomposition** ... | cp-algorithms/src/graph/centroid_decomposition.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.128 | false | false | 11,735 | -1 |
---
tags:
- Translated
e_maxx_link: gray_code
---
# Gray code
Gray code is a binary numeral system where two successive values differ in only one bit.
Denote by $G(n)$ the representation of the number $n$ using the Gray code. The sequence of Gray codes for 3-bit numbers is: 000, 001, 011, 010, 110, 111, 101, 100,... | cp-algorithms/src/algebra/gray-code.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.236 | false | false | 3,453 | -1 |
---
tags:
- Original
---
# Knapsack Problem
Prerequisite knowledge: [Introduction to Dynamic Programming](https://cp-algorithms.com/dynamic_programming/intro-to-dp.html)
## Introduction
Consider the following example:
### [[USACO07 Dec] Charm Bracelet](https://www.acmicpc.net/problem/6144)
There are $n$ distinct ... | cp-algorithms/src/dynamic_programming/knapsack.md | algorithms | cp-algorithms | cp-algorithms/src/dynamic_programming | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.15 | false | false | 9,590 | -1 |
---
tags:
- Translated
e_maxx_link: convex_hull_graham
---
# Convex Hull construction
In this article we will discuss the problem of constructing a convex hull from a set of points.
Consider $N$ points given on a plane, and the objective is to generate a convex hull, i.e. the smallest
convex polygon that contains ... | cp-algorithms/src/geometry/convex-hull.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.085 | false | false | 9,124 | -1 |
---
tags:
- Original
---
# Simulated Annealing
**Simulated Annealing (SA)** is a randomized algorithm, which approximates the global optimum of a function. It's called a randomized algorithm, because it employs a certain amount of randomness in its search and thus its output can vary for the same input.
## The p... | cp-algorithms/src/num_methods/simulated_annealing.md | algorithms | cp-algorithms | cp-algorithms/src/num_methods | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.137 | false | false | 10,006 | -1 |
---
tags:
- Translated
e_maxx_link: rmq
---
# Range Minimum Query
You are given an array $A[1..N]$.
You have to answer incoming queries of the form $(L, R)$, which ask to find the minimum element in array $A$ between positions $L$ and $R$ inclusive.
RMQ can appear in problems directly or can be applied in some oth... | cp-algorithms/src/sequences/rmq.md | algorithms | cp-algorithms | cp-algorithms/src/sequences | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.167 | false | false | 2,679 | -1 |
---
tags:
- Translated
e_maxx_link: oriented_area
---
# Oriented area of a triangle
Given three points $p_1$, $p_2$ and $p_3$, calculate an oriented (signed) area of a triangle formed by them. The sign of the area is determined in the following way: imagine you are standing in the plane at point $p_1$ and are facin... | cp-algorithms/src/geometry/oriented-triangle-area.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.226 | false | false | 1,978 | -1 |
---
tags:
- Translated
e_maxx_link: tree_painting
---
# Paint the edges of the tree
This is a fairly common task. Given a tree $G$ with $N$ vertices. There are two types of queries: the first one is to paint an edge, the second one is to query the number of colored edges between two vertices.
Here we will describe... | cp-algorithms/src/graph/tree_painting.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.074 | false | false | 8,021 | -1 |
---
tags:
- Translated
e_maxx_link: stern_brocot_farey
---
# The Stern-Brocot tree and Farey sequences
## Stern-Brocot tree
The Stern-Brocot tree is an elegant construction to represent the set of all positive fractions. It was independently discovered by German mathematician Moritz Stern in 1858 and by French wat... | cp-algorithms/src/others/stern_brocot_tree_farey_sequences.md | algorithms | cp-algorithms | cp-algorithms/src/others | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.089 | false | false | 12,489 | -1 |
---
title: Manacher's Algorithm - Finding all sub-palindromes in O(N)
tags:
- Translated
e_maxx_link: palindromes_count
---
# Manacher's Algorithm - Finding all sub-palindromes in $O(N)$
## Statement
Given string $s$ with length $n$. Find all the pairs $(i, j)$ such that substring $s[i\dots j]$ is a palindrome. Str... | cp-algorithms/src/string/manacher.md | algorithms | cp-algorithms | cp-algorithms/src/string | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.097 | false | false | 11,231 | -1 |
---
tags:
- Translated
e_maxx_link: fenwick_tree
---
# Fenwick Tree
Let $f$ be some group operation (a binary associative function over a set with an identity element and inverse elements) and $A$ be an array of integers of length $N$.
Denote $f$'s infix notation as $*$; that is, $f(x,y) = x*y$ for arbitrary intege... | cp-algorithms/src/data_structures/fenwick.md | algorithms | cp-algorithms | cp-algorithms/src/data_structures | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.164 | false | false | 19,073 | -1 |
---
tags:
- Translated
e_maxx_link: min_cost_flow
---
# Minimum-cost flow - Successive shortest path algorithm
Given a network $G$ consisting of $n$ vertices and $m$ edges.
For each edge (generally speaking, oriented edges, but see below), the capacity (a non-negative integer) and the cost per unit of flow along th... | cp-algorithms/src/graph/min_cost_flow.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.107 | false | false | 8,197 | -1 |
---
tags:
- Translated
e_maxx_link: voronoi_diagram_2d_n4
---
# Delaunay triangulation and Voronoi diagram
Consider a set $\{p_i\}$ of points on the plane.
A **Voronoi diagram** $V(\{p_i\})$ of $\{p_i\}$ is a partition of the plane into $n$ regions $V_i$, where $V_i = \{p\in\mathbb{R}^2;\ \rho(p, p_i) = \min\ \rho(... | cp-algorithms/src/geometry/delaunay.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.065 | false | false | 12,830 | -1 |
---
tags:
- Translated
e_maxx_link: determinant_gauss
---
# Calculating the determinant of a matrix by Gauss
Problem: Given a matrix $A$ of size $N \times N$. Compute its determinant.
## Algorithm
We use the ideas of [Gauss method for solving systems of linear equations](linear-system-gauss.md)
We will perform t... | cp-algorithms/src/linear_algebra/determinant-gauss.md | algorithms | cp-algorithms | cp-algorithms/src/linear_algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.167 | false | false | 1,794 | -1 |
---
tags:
- Translated
e_maxx_link: lines_intersection
---
# Intersection Point of Lines
You are given two lines, described via the equations $a_1 x + b_1 y + c_1 = 0$ and $a_2 x + b_2 y + c_2 = 0$.
We have to find the intersection point of the lines, or determine that the lines are parallel.
## Solution
If two ... | cp-algorithms/src/geometry/lines-intersection.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.094 | false | false | 2,513 | -1 |
---
tags:
- Translated
e_maxx_link: discrete_root
---
# Discrete Root
The problem of finding a discrete root is defined as follows. Given a prime $n$ and two integers $a$ and $k$, find all $x$ for which:
$x^k \equiv a \pmod n$
## The algorithm
We will solve this problem by reducing it to the [discrete logarithm ... | cp-algorithms/src/algebra/discrete-root.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.107 | false | false | 4,386 | -1 |
---
tags:
- Translated
e_maxx_link: negative_cycle
---
# Finding a negative cycle in the graph
You are given a directed weighted graph $G$ with $N$ vertices and $M$ edges. Find any cycle of negative weight in it, if such a cycle exists.
In another formulation of the problem you have to find all pairs of vertices w... | cp-algorithms/src/graph/finding-negative-cycle-in-graph.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.095 | false | false | 4,304 | -1 |
---
title: Lowest Common Ancestor - O(sqrt(N)) and O(log N) with O(N) preprocessing
tags:
- Translated
e_maxx_link: lca
---
# Lowest Common Ancestor - $O(\sqrt{N})$ and $O(\log N)$ with $O(N)$ preprocessing
Given a tree $G$. Given queries of the form $(v_1, v_2)$, for each query you need to find the lowest common an... | cp-algorithms/src/graph/lca.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.161 | false | false | 7,550 | -1 |
---
tags:
- Translated
e_maxx_link: assignment_mincostflow
---
# Solving assignment problem using min-cost-flow
The **assignment problem** has two equivalent statements:
- Given a square matrix $A[1..N, 1..N]$, you need to select $N$ elements in it so that exactly one element is selected in each row and column,... | cp-algorithms/src/graph/Assignment-problem-min-flow.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.051 | false | false | 5,144 | -1 |
---
tags:
- Original
---
# Floyd's Linked List Cycle Finding Algorithm
Given a linked list where the starting point of that linked list is denoted by **head**, and there may or may not be a cycle present. For instance:
<div style="text-align: center;">
<img src="tortoise_hare_algo.png" alt=""Linked list with cyc... | cp-algorithms/src/others/tortoise_and_hare.md | algorithms | cp-algorithms | cp-algorithms/src/others | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.129 | false | false | 5,243 | -1 |
---
tags:
- Translated
e_maxx_link: simpson_integrating
---
# Integration by Simpson's formula
We are going to calculate the value of a definite integral
$$\int_a ^ b f (x) dx$$
The solution described here was published in one of the dissertations of **Thomas Simpson** in 1743.
## Simpson's formula
Let $n$ be s... | cp-algorithms/src/num_methods/simpson-integration.md | algorithms | cp-algorithms | cp-algorithms/src/num_methods | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.186 | false | false | 2,852 | -1 |
---
tags:
- Translated
e_maxx_link: 15_puzzle
---
# 15 Puzzle Game: Existence Of The Solution
This game is played on a $4 \times 4$ board. On this board there are $15$ playing tiles numbered from 1 to 15. One cell is left empty (denoted by 0). You need to get the board to the position presented below by repeatedly ... | cp-algorithms/src/others/15-puzzle.md | algorithms | cp-algorithms | cp-algorithms/src/others | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.186 | false | false | 2,382 | -1 |
---
tags:
- Translated
e_maxx_link: ukkonen
---
# Suffix Tree. Ukkonen's Algorithm
*This article is a stub and doesn't contain any descriptions. For a description of the algorithm, refer to other sources, such as [Algorithms on Strings, Trees, and Sequences](https://www.cs.cmu.edu/afs/cs/project/pscico-guyb/realwor... | cp-algorithms/src/string/suffix-tree-ukkonen.md | algorithms | cp-algorithms | cp-algorithms/src/string | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.118 | false | false | 6,679 | -1 |
---
tags:
- Translated
e_maxx_link: expressions_parsing
---
# Expression parsing
A string containing a mathematical expression containing numbers and various operators is given.
We have to compute the value of it in $O(n)$, where $n$ is the length of the string.
The algorithm discussed here translates an expressio... | cp-algorithms/src/string/expression_parsing.md | algorithms | cp-algorithms | cp-algorithms/src/string | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.042 | false | false | 9,482 | -1 |
---
tags:
- Translated
e_maxx_link: games_on_graphs
---
# Games on arbitrary graphs
Let a game be played by two players on an arbitrary graph $G$.
I.e. the current state of the game is a certain vertex.
The players perform moves by turns, and move from the current vertex to an adjacent vertex using a connecting edg... | cp-algorithms/src/game_theory/games_on_graphs.md | algorithms | cp-algorithms | cp-algorithms/src/game_theory | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.032 | false | false | 9,824 | -1 |
---
title: Calculating the determinant using Kraut method
tags:
- Original
---
# Calculating the determinant using Kraut method in $O(N^3)$
In this article, we'll describe how to find the determinant of the matrix using Kraut method, which works in $O(N^3)$.
The Kraut algorithm finds decomposition of matrix $A$ as ... | cp-algorithms/src/linear_algebra/determinant-kraut.md | algorithms | cp-algorithms | cp-algorithms/src/linear_algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.045 | false | false | 3,243 | -1 |
---
tags:
- Translated
e_maxx_link: sprague_grundy
---
# Sprague-Grundy theorem. Nim
## Introduction
This theorem describes the so-called **impartial** two-player game,
i.e. those in which the available moves and winning/losing depends only on the state of the game.
In other words, the only difference between the ... | cp-algorithms/src/game_theory/sprague-grundy-nim.md | algorithms | cp-algorithms | cp-algorithms/src/game_theory | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.197 | false | false | 12,374 | -1 |
---
tags:
- Original
---
# Stars and bars
Stars and bars is a mathematical technique for solving certain combinatorial problems.
It occurs whenever you want to count the number of ways to group identical objects.
## Theorem
The number of ways to put $n$ identical objects into $k$ labeled boxes is
$$\binom{n + k ... | cp-algorithms/src/combinatorics/stars_and_bars.md | algorithms | cp-algorithms | cp-algorithms/src/combinatorics | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.275 | false | false | 3,438 | -1 |
---
tags:
- Translated
e_maxx_link: dijkstra
---
# Dijkstra Algorithm
You are given a directed or undirected weighted graph with $n$ vertices and $m$ edges. The weights of all edges are non-negative. You are also given a starting vertex $s$. This article discusses finding the lengths of the shortest paths from a st... | cp-algorithms/src/graph/dijkstra.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.382 | false | false | 12,954 | -1 |
---
tags:
- Translated
e_maxx_link: aho_corasick
---
# Aho-Corasick algorithm
The Aho-Corasick algorithm allows us to quickly search for multiple patterns in a text.
The set of pattern strings is also called a _dictionary_.
We will denote the total length of its constituent strings by $m$ and the size of the alphab... | cp-algorithms/src/string/aho_corasick.md | algorithms | cp-algorithms | cp-algorithms/src/string | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.06 | false | false | 16,404 | -1 |
---
tags:
- Original
---
# Minimum Enclosing Circle
Consider the following problem:
!!! example "[Library Checker - Minimum Enclosing Circle](https://judge.yosupo.jp/problem/minimum_enclosing_circle)"
You're given $n \leq 10^5$ points $p_i=(x_i, y_i)$.
For each $p_i$, find whether it lies on the circumfe... | cp-algorithms/src/geometry/enclosing-circle.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.095 | false | false | 11,952 | -1 |
---
tags:
- Translated
e_maxx_link: 2_sat
---
# 2-SAT
SAT (Boolean satisfiability problem) is the problem of assigning Boolean values to variables to satisfy a given Boolean formula.
The Boolean formula will usually be given in CNF (conjunctive normal form), which is a conjunction of multiple clauses, where each c... | cp-algorithms/src/graph/2SAT.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.097 | false | false | 10,150 | -1 |
---
tags:
- Translated
e_maxx_link: circles_intersection
---
# Circle-Circle Intersection
You are given two circles on a 2D plane, each one described as coordinates of its center and its radius. Find the points of their intersection (possible cases: one or two points, no intersection or circles coincide).
## Solut... | cp-algorithms/src/geometry/circle-circle-intersection.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.156 | false | false | 2,269 | -1 |
---
tags:
- Translated
e_maxx_link: stoer_wagner_mincut
---
# Minimum cut - Stoer-Wagner algorithm
## Problem statement
Given an undirected weighted graph $G$ with $n$ vertices and $m$ edges. A cut $C$ is a non-empty proper subset of the vertices (effectively, a cut is a partition of the vertices into two non-empt... | cp-algorithms/src/graph/stoer_wagner_mincut.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.085 | false | false | 10,657 | -1 |
---
title: Finding faces of a planar graph
tags:
- Translated
e_maxx_link: facets
---
# Finding faces of a planar graph
Consider a graph $G$ with $n$ vertices and $m$ edges, which can be drawn on a plane in such a way that two edges intersect only at a common vertex (if it exists).
Such graphs are called **planar**.... | cp-algorithms/src/geometry/planar.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.067 | false | false | 13,865 | -1 |
---
tags:
- Translated
e_maxx_link: chinese_theorem
---
# Chinese Remainder Theorem
The Chinese Remainder Theorem (which will be referred to as CRT in the rest of this article) was discovered by Chinese mathematician Sun Zi.
## Formulation
Let $m = m_1 \cdot m_2 \cdots m_k$, where $m_i$ are pairwise coprime. In a... | cp-algorithms/src/algebra/chinese-remainder-theorem.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.1 | false | false | 9,244 | -1 |
---
tags:
- Translated
e_maxx_link: length_of_segments_union
---
# Length of the union of segments
Given $n$ segments on a line, each described by a pair of coordinates $(a_{i1}, a_{i2})$.
We have to find the length of their union.
The following algorithm was proposed by Klee in 1977.
It works in $O(n\log n)$ and ... | cp-algorithms/src/geometry/length-of-segments-union.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.128 | false | false | 1,460 | -1 |
---
tags:
- Original
---
<!--?title Continued fractions -->
# Continued fractions
**Continued fraction** is a representation of a real number as a specific convergent sequence of rational numbers. They are useful in competitive programming because they are easy to compute and can be efficiently used to find the bes... | cp-algorithms/src/algebra/continued-fractions.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.073 | false | false | 59,608 | -1 |
---
tags:
- Translated
e_maxx_link: bipartite_checking
---
# Check whether a graph is bipartite
A bipartite graph is a graph whose vertices can be divided into two disjoint sets so that every edge connects two vertices from different sets (i.e. there are no edges which connect vertices from the same set). These set... | cp-algorithms/src/graph/bipartite-check.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.133 | false | false | 2,554 | -1 |
---
tags:
- Translated
e_maxx_link: intersecting_segments
---
# Search for a pair of intersecting segments
Given $n$ line segments on the plane. It is required to check whether at least two of them intersect with each other.
If the answer is yes, then print this pair of intersecting segments; it is enough to choose... | cp-algorithms/src/geometry/intersecting_segments.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.057 | false | false | 9,218 | -1 |
---
tags:
- Translated
e_maxx_link: roots_newton
---
# Newton's method for finding roots
This is an iterative method invented by Isaac Newton around 1664. However, this method is also sometimes called the Raphson method, since Raphson invented the same algorithm a few years after Newton, but his article was publis... | cp-algorithms/src/num_methods/roots_newton.md | algorithms | cp-algorithms | cp-algorithms/src/num_methods | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.075 | false | false | 4,546 | -1 |
---
tags:
- Original
---
# Sqrt Tree
Given an array $a$ that contains $n$ elements and the operation $\circ$ that satisfies associative property: $(x \circ y) \circ z = x \circ (y \circ z)$ is true for any $x$, $y$, $z$.
So, such operations as $\gcd$, $\min$, $\max$, $+$, $\text{and}$, $\text{or}$, $\text{xor}$, e... | cp-algorithms/src/data_structures/sqrt-tree.md | algorithms | cp-algorithms | cp-algorithms/src/data_structures | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.126 | false | false | 16,958 | -1 |
---
tags:
- Translated
e_maxx_link: sqrt_decomposition
---
# Sqrt Decomposition
Sqrt Decomposition is a method (or a data structure) that allows you to perform some common operations (finding sum of the elements of the sub-array, finding the minimal/maximal element, etc.) in $O(\sqrt n)$ operations, which is much f... | cp-algorithms/src/data_structures/sqrt_decomposition.md | algorithms | cp-algorithms | cp-algorithms/src/data_structures | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.182 | false | false | 14,437 | -1 |
---
tags:
- Translated
e_maxx_link: euler_function
---
# Euler's totient function
Euler's totient function, also known as **phi-function** $\phi (n)$, counts the number of integers between 1 and $n$ inclusive, which are coprime to $n$. Two numbers are coprime if their greatest common divisor equals $1$ ($1$ is cons... | cp-algorithms/src/algebra/phi-function.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.203 | false | false | 13,028 | -1 |
---
tags:
- Original
---
# Sparse Table
Sparse Table is a data structure, that allows answering range queries.
It can answer most range queries in $O(\log n)$, but its true power is answering range minimum queries (or equivalent range maximum queries).
For those queries it can compute the answer in $O(1)$ time.
Th... | cp-algorithms/src/data_structures/sparse-table.md | algorithms | cp-algorithms | cp-algorithms/src/data_structures | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.244 | false | false | 8,363 | -1 |
---
tags:
- Translated
e_maxx_link: pick_grid_theorem
---
# Pick's Theorem
A polygon without self-intersections is called a lattice polygon if all its vertices have integer coordinates in some 2D grid. Pick's theorem provides a way to compute the area of this polygon through the number of lattice points that are ly... | cp-algorithms/src/geometry/picks-theorem.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.219 | false | false | 3,239 | -1 |
---
tags:
- Translated
e_maxx_link: primitive_root
---
# Primitive Root
## Definition
In modular arithmetic, a number $g$ is called a `primitive root modulo n` if every number coprime to $n$ is congruent to a power of $g$ modulo $n$. Mathematically, $g$ is a `primitive root modulo n` if and only if for any integer... | cp-algorithms/src/algebra/primitive-root.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.227 | false | false | 4,547 | -1 |
---
tags:
- Translated
e_maxx_link: catalan_numbers
---
# Catalan Numbers
Catalan numbers form a number sequence which is useful in a number of combinatorial problems, often involving recursively-defined objects.
This sequence was named after the Belgian mathematician [Catalan](https://en.wikipedia.org/wiki/Eug%C3%... | cp-algorithms/src/combinatorics/catalan-numbers.md | algorithms | cp-algorithms | cp-algorithms/src/combinatorics | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.182 | false | false | 5,856 | -1 |
---
tags:
- Translated
e_maxx_link: string_tandems
---
# Finding repetitions
Given a string $s$ of length $n$.
A **repetition** is two occurrences of a string in a row.
In other words a repetition can be described by a pair of indices $i < j$ such that the substring $s[i \dots j]$ consists of two identical strings... | cp-algorithms/src/string/main_lorentz.md | algorithms | cp-algorithms | cp-algorithms/src/string | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.053 | false | false | 11,247 | -1 |
---
tags:
- Original
---
# Manhattan Distance
## Definition
For points $p$ and $q$ on a plane, we can define the distance between them as the sum of the differences between their $x$ and $y$ coordinates:
$$d(p,q) = |x_p - x_q| + |y_p - y_q|$$
Defined this way, the distance corresponds to the so-called [Manhatta... | cp-algorithms/src/geometry/manhattan-distance.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.157 | false | false | 12,327 | -1 |
---
tags:
- Translated
e_maxx_link: bracket_sequences
---
# Balanced bracket sequences
A **balanced bracket sequence** is a string consisting of only brackets, such that this sequence, when inserted certain numbers and mathematical operations, gives a valid mathematical expression.
Formally you can define balanced ... | cp-algorithms/src/combinatorics/bracket_sequences.md | algorithms | cp-algorithms | cp-algorithms/src/combinatorics | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.071 | false | false | 13,016 | -1 |
---
tags:
- Original
---
# Number of divisors / sum of divisors
In this article we discuss how to compute the number of divisors $d(n)$ and the sum of divisors $\sigma(n)$ of a given number $n$.
## Number of divisors
The prime factors of a divisor $d$, including their multiplicities, have to form a submultiset of... | cp-algorithms/src/algebra/divisors.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.14 | false | false | 4,564 | -1 |
---
tags:
- Translated
e_maxx_link: profile_dynamics
---
# Dynamic Programming on Broken Profile. Problem "Parquet"
Common problems solved using DP on broken profile include:
- finding number of ways to fully fill an area (e.g. chessboard/grid) with some figures (e.g. dominoes)
- finding a way to fill an area with... | cp-algorithms/src/dynamic_programming/profile-dynamics.md | algorithms | cp-algorithms | cp-algorithms/src/dynamic_programming | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.116 | false | false | 3,373 | -1 |
---
tags:
- Translated
e_maxx_link: suffix_array
---
# Suffix Array
## Definition
Let $s$ be a string of length $n$. The $i$-th suffix of $s$ is the substring $s[i \ldots n - 1]$.
A **suffix array** will contain integers that represent the **starting indexes** of the all the suffixes of a given string, after the ... | cp-algorithms/src/string/suffix-array.md | algorithms | cp-algorithms | cp-algorithms/src/string | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.144 | false | false | 23,210 | -1 |
---
tags:
- Translated
e_maxx_link: suffix_automata
---
# Suffix Automaton
A **suffix automaton** is a powerful data structure that allows solving many string-related problems.
For example, you can search for all occurrences of one string in another, or count the amount of different substrings of a given string.
... | cp-algorithms/src/string/suffix-automaton.md | algorithms | cp-algorithms | cp-algorithms/src/string | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.066 | false | false | 44,837 | -1 |
---
tags:
- Translated
e_maxx_link: joseph_problem
---
# Josephus Problem
## Statement
We are given the natural numbers $n$ and $k$.
All natural numbers from $1$ to $n$ are written in a circle.
First, count the $k$-th number starting from the first one and delete it.
Then $k$ numbers are counted starting from the... | cp-algorithms/src/others/josephus_problem.md | algorithms | cp-algorithms | cp-algorithms/src/others | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.104 | false | false | 6,420 | -1 |
---
tags:
- Translated
e_maxx_link: maximum_zero_submatrix
---
# Finding the largest zero submatrix
You are given a matrix with `n` rows and `m` columns. Find the largest submatrix consisting of only zeros (a submatrix is a rectangular area of the matrix).
## Algorithm
Elements of the matrix will be `a[i][j]`, wh... | cp-algorithms/src/dynamic_programming/zero_matrix.md | algorithms | cp-algorithms | cp-algorithms/src/dynamic_programming | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.101 | false | false | 5,437 | -1 |
---
tags:
- Translated
e_maxx_link: eratosthenes_sieve
---
# Sieve of Eratosthenes
Sieve of Eratosthenes is an algorithm for finding all the prime numbers in a segment $[1;n]$ using $O(n \log \log n)$ operations.
The algorithm is very simple:
at the beginning we write down all numbers between 2 and $n$.
We mark al... | cp-algorithms/src/algebra/sieve-of-eratosthenes.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.155 | false | false | 15,432 | -1 |
---
title: Factorial modulo p
tags:
- Translated
e_maxx_link: modular_factorial
---
# Factorial modulo $p$
In some cases it is necessary to consider complex formulas modulo some prime $p$, containing factorials in both numerator and denominator, like such that you encounter in the formula for Binomial coefficients.... | cp-algorithms/src/algebra/factorial-modulo.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.07 | false | false | 6,426 | -1 |
---
tags:
- Translated
e_maxx_link: flow_with_limits
---
# Flows with demands
In a normal flow network the flow of an edge is only limited by the capacity $c(e)$ from above and by 0 from below.
In this article we will discuss flow networks, where we additionally require the flow of each edge to have a certain amoun... | cp-algorithms/src/graph/flow_with_demands.md | algorithms | cp-algorithms | cp-algorithms/src/graph | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.111 | false | false | 4,474 | -1 |
---
tags:
- Translated
e_maxx_link: ternary_search
---
# Ternary Search
We are given a function $f(x)$ which is unimodal on an interval $[l, r]$. By unimodal function, we mean one of two behaviors of the function:
1. The function strictly increases first, reaches a maximum (at a single point or over an interval)... | cp-algorithms/src/num_methods/ternary_search.md | algorithms | cp-algorithms | cp-algorithms/src/num_methods | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.295 | false | false | 8,666 | -1 |
---
tags:
- Translated
e_maxx_link: diofant_1_equation
---
# Linear Congruence Equation
This equation is of the form:
$$a \cdot x \equiv b \pmod n,$$
where $a$, $b$ and $n$ are given integers and $x$ is an unknown integer.
It is required to find the value $x$ from the interval $[0, n-1]$ (clearly, on the entire ... | cp-algorithms/src/algebra/linear_congruence_equation.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.152 | false | false | 2,926 | -1 |
---
tags:
- Original
---
# Basic Geometry
In this article we will consider basic operations on points in Euclidean space which maintains the foundation of the whole analytical geometry.
We will consider for each point $\mathbf r$ the vector $\vec{\mathbf r}$ directed from $\mathbf 0$ to $\mathbf r$.
Later we will n... | cp-algorithms/src/geometry/basic-geometry.md | algorithms | cp-algorithms | cp-algorithms/src/geometry | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.05 | false | false | 16,865 | -1 |
---
tags:
- Original
---
# Integer factorization
In this article we list several algorithms for the factorization of integers, each of which can be either fast or varying levels of slow depending on their input.
Notice, if the number that you want to factorize is actually a prime number, most of the algorithms wil... | cp-algorithms/src/algebra/factorization.md | algorithms | cp-algorithms | cp-algorithms/src/algebra | CC-BY-SA-4.0 | CC-BY-SA-4.0 | bbbbb6f04e5c9a665c8477ec68ee4f28b1367dd7 | doc | false | 0.043 | false | false | 17,230 | -1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.