Builtins
This page lists the builtins you can call directly from algo source. For larger
structures such as graphs and segment trees, see Stdlib.
Array and collection helpers
Section titled “Array and collection helpers”| Builtin | Purpose |
|---|---|
len(a) | length of an array or string |
filled(n, val) | create an array filled with val |
zeros(n) | array of n zeros |
zeros(n, m) | n x m matrix of zeros |
sort(a) | sort ascending in place |
sort(a, desc) | sort descending in place |
| `sort(a, | x, y |
sort_by(a, key) | sort by a key function |
sorted(a) | sorted copy |
reverse(a) | reverse in place |
fill(a, val) | overwrite each element |
unique(a) | remove consecutive duplicates |
lower(a, x) | lower-bound index in a sorted array |
upper(a, x) | upper-bound index in a sorted array |
Array methods and free-function equivalents
Section titled “Array methods and free-function equivalents”| Free function | Method form |
|---|---|
sum(a) | a.sum() |
min(a) | a.min() |
max(a) | a.max() |
count(a, pred) | a.count(pred) |
count(a, val) | a.count(val) |
any(a, pred) | a.any(pred) |
all(a, pred) | a.all(pred) |
filter(a, pred) | a.filter(pred) |
map(a, f) | a.map(f) |
find(a, pred) | a.find(pred) |
Dot-only array helpers:
a.push(x)a.compress()a.prefix_sum()a.suffix_sum()a.next_greater()a.next_smaller()a.sliding_max(k)a.sliding_min(k)Additional collection helpers:
min_by(a, key = |x| x.cost)max_by(a, key = |x| x.cost)sort_by(a, key = |x| x.cost)zip(a, b)enumerate(a)Positional forms such as sort_by(a, |x| x.cost) still work, but naming the optional
key/comparator is the recommended house style.
| Builtin | Purpose |
|---|---|
gcd(a, b) | greatest common divisor |
lcm(a, b) | least common multiple |
abs(x) | absolute value |
min(a, b) | minimum of two values |
max(a, b) | maximum of two values |
comb(n, k) | binomial coefficient modulo MOD |
modinv(a) | modular inverse |
sieve(n) | prime sieve |
primes(n) | list primes up to n |
factorize(n) | prime factorization |
divisors(n) | sorted divisors |
pow(a, b) | integer exponentiation |
floor_div(a, b) | floor division (same as a // b) |
isqrt(n) | integer square root |
ceil_div(a, b) | ceiling division |
Prefix and suffix helpers
Section titled “Prefix and suffix helpers”prefix(a, op = sum)prefix(a, op = xor)suffix(a, op = max)prefix_2d(mat, op = sum)prefix(a, init = 1, reducer = |acc: int, x: int| acc * x)Builtin reducers are sum, xor, max, min, and gcd.
prefix_2d is sum-only.
For custom folds, use the three-argument forms prefix(a, init, reducer) and
suffix(a, init, reducer), optionally with named init = and reducer = arguments.
CP helpers
Section titled “CP helpers”mex(a)argsort(a)inversion_count(a)lis(a)lcs(s, t)Graph and stdlib constructors
Section titled “Graph and stdlib constructors”graph(n)graph(n, undirected = true)wgraph(n)wgraph(n, undirected = true)grid(mat)grid(mat, dirs = 8)grid_graph(mat)grid_graph(mat, dirs = 8)tree(n)
dsu(n)segtree(n, op = sum)segtree(a, op = sum)lazy_segtree(n, op = sum)lazy_segtree(a, op = max)bit(n)sparse(a, op = min)strhash(s)grid() returns a Grid with bfs(), multi_bfs(), components(), id(), cell(), and neighbors() methods. grid_graph() converts a grid into a Graph for adjacency-based algorithms. tree() creates a tree for LCA and distance queries.
Binary search
Section titled “Binary search”bsearch(lo, hi, pred)bsearch_last(lo, hi, pred)bsearch_float(lo, hi, eps, pred)String helpers
Section titled “String helpers”z_func(s)kmp_fail(s)kmp_search(s, t)suffix_array(s)I/O helpers
Section titled “I/O helpers”println xprint a, b, coutput arroutput arr, sep=" "newlineConstants
Section titled “Constants”| Name | Meaning |
|---|---|
INF | large positive sentinel |
NINF | large negative sentinel |
MOD | current modulus for modular helpers |