Skip to content

Builtins

This page lists the builtins you can call directly from algo source. For larger structures such as graphs and segment trees, see Stdlib.

BuiltinPurpose
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 functionMethod 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.

BuiltinPurpose
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(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.

mex(a)
argsort(a)
inversion_count(a)
lis(a)
lcs(s, t)
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.

bsearch(lo, hi, pred)
bsearch_last(lo, hi, pred)
bsearch_float(lo, hi, eps, pred)
z_func(s)
kmp_fail(s)
kmp_search(s, t)
suffix_array(s)
println x
print a, b, c
output arr
output arr, sep=" "
newline
NameMeaning
INFlarge positive sentinel
NINFlarge negative sentinel
MODcurrent modulus for modular helpers