Code Snippets
Math
Below are common math patterns and helper snippets that often appear in LeetCode-style problems. Each section groups related tools into one Python block you can copy and adapt.
Number Theory
import math# Greatest Common Divisor (GCD) - Euclidean algorithmdef gcd(a: int, b: int) -> int:while b:a, b = b, a % breturn a# Using math.gcdg1 = gcd(48, 18) # 6g2 = math.gcd(48, 18) # 6# Least Common Multiple (LCM) via GCDdef lcm(a: int, b: int) -> int:return a // gcd(a, b) * b# Simple prime check (good for n up to about 1e7 in worst case)def is_prime(n: int) -> bool:if n < 2:return Falseif n % 2 == 0:return n == 2d = 3while d * d <= n:if n % d == 0:return Falsed += 2return True# Sieve of Eratosthenes - primes up to ndef sieve(n: int) -> list[bool]:is_prime = [True] * (n + 1)is_prime[0:2] = [False, False]p = 2while p * p <= n:if is_prime[p]:for m in range(p * p, n + 1, p):is_prime[m] = Falsep += 1return is_prime# Prime factorization (returns list of (prime, exponent))def factorize(n: int) -> list[tuple[int, int]]:factors = []d = 2while d * d <= n:if n % d == 0:cnt = 0while n % d == 0:n //= dcnt += 1factors.append((d, cnt))d += 1if n > 1:factors.append((n, 1))return factors# Euler's Totient Function φ(n)def phi(n: int) -> int:result = nfor p, _ in factorize(n):result -= result // preturn result# Modular arithmetic basicsMOD = 10**9 + 7a, b = 5, 3add = (a + b) % MODsub = (a - b) % MODmul = (a * b) % MOD# Modular inverse using Fermat's Little Theorem (MOD must be prime)inv_a = pow(a, MOD - 2, MOD) # a^{-1} mod MOD# Number of divisors and sum of divisors from factorizationdef divisor_count_and_sum(n: int) -> tuple[int, int]:factors = factorize(n)cnt = 1total_sum = 1for p, e in factors:cnt *= (e + 1)# geometric series: 1 + p + ... + p^etotal_sum *= (p**(e + 1) - 1) // (p - 1)return cnt, total_sum# Coprime checkdef are_coprime(x: int, y: int) -> bool:return gcd(x, y) == 1
Combinatorics
import math# Factorial helperdef fact(n: int) -> int:res = 1for i in range(2, n + 1):res *= ireturn res# nCr (combinations) using factorialdef nCr(n: int, r: int) -> int:if r < 0 or r > n:return 0return fact(n) // (fact(r) * fact(n - r))# nPr (permutations)def nPr(n: int, r: int) -> int:if r < 0 or r > n:return 0res = 1for i in range(n, n - r, -1):res *= ireturn res# Python 3.8+ / 3.11+ built-ins (if available)try:c_5_2 = math.comb(5, 2) # 10p_5_2 = math.perm(5, 2) # 20except AttributeError:c_5_2 = nCr(5, 2)p_5_2 = nPr(5, 2)# Build first k rows of Pascal's Triangledef pascal(k: int) -> list[list[int]]:tri = []for i in range(k):row = [1] * (i + 1)for j in range(1, i):row[j] = tri[i - 1][j - 1] + tri[i - 1][j]tri.append(row)return tri# Number of subsets of an n-element setdef subset_count(n: int) -> int:return 1 << n # 2**n# Inclusion–exclusion for |A ∪ B|def union_two(a: int, b: int, both: int) -> int:return a + b - both# Simple Catalan number (small n)def catalan(n: int) -> int:return nCr(2 * n, n) // (n + 1)# Ways to choose unordered pairs from n itemsdef pairs_from_n(n: int) -> int:return n * (n - 1) // 2# Stars and bars: number of non-negative integer solutions to x1+...+xk = ndef stars_and_bars(n: int, k: int) -> int:return nCr(n + k - 1, k - 1)
Probability & Expectation
import random# Basic probability from countsdef probability(successes: int, total: int) -> float:return successes / total if total > 0 else 0.0# Conditional probability P(A|B) = P(A∩B)/P(B)def conditional(joint: float, p_b: float) -> float:return joint / p_b if p_b > 0 else 0.0# Expected value of discrete variabledef expected_value(values: list[float], probs: list[float]) -> float:return sum(v * p for v, p in zip(values, probs))# Expected number of trials until first success in geometric(p)def geometric_expectation(p: float) -> float:return 1.0 / p# Linearity of expectation: sum of dicedef expected_sum_of_dice(n: int) -> float:# Each die: (1+2+3+4+5+6)/6 = 3.5return n * 3.5# Monte Carlo estimate of probabilitydef monte_carlo_estimate(trials: int = 10000) -> float:hits = 0for _ in range(trials):x, y = random.random(), random.random()if x * x + y * y <= 1:hits += 1return hits / trials # ~π/4# Single step of 1D random walkdef random_walk_step(pos: int) -> int:step = random.choice([-1, 1])return pos + step# Normalize weights into probabilitiesdef normalize(weights: list[float]) -> list[float]:s = sum(weights)return [w / s for w in weights] if s > 0 else [0.0] * len(weights)# Build prefix sums for weighted random choicedef build_prefix(probs: list[float]) -> list[float]:prefix = []s = 0.0for p in probs:s += pprefix.append(s)return prefixdef pick_index(prefix: list[float]) -> int:r = random.random() * prefix[-1]lo, hi = 0, len(prefix) - 1while lo < hi:mid = (lo + hi) // 2if prefix[mid] >= r:hi = midelse:lo = mid + 1return lo
Algebra & Series
import math# Quadratic formula: ax^2 + bx + c = 0def solve_quadratic(a: float, b: float, c: float) -> tuple[complex, complex]:if a == 0:# Linear: bx + c = 0return (-c / b, )disc = b * b - 4 * a * cif disc >= 0:root_disc = math.sqrt(disc)else:root_disc = complex(0, math.sqrt(-disc))return ((-b - root_disc) / (2 * a), (-b + root_disc) / (2 * a))# Sum of first n integers: 1 + ... + ndef sum_first_n(n: int) -> int:return n * (n + 1) // 2# Sum of squares 1^2 + ... + n^2def sum_squares(n: int) -> int:return n * (n + 1) * (2 * n + 1) // 6# Sum of cubes 1^3 + ... + n^3def sum_cubes(n: int) -> int:s = sum_first_n(n)return s * s# Arithmetic progression sum: a, a+d, ..., a+(n-1)ddef ap_sum(a: int, d: int, n: int) -> int:return n * (2 * a + (n - 1) * d) // 2# Geometric progression sum: a + ar + ... + ar^{n-1}, r != 1def gp_sum(a: float, r: float, n: int) -> float:if r == 1:return a * nreturn a * (1 - r**n) / (1 - r)# Clamp a value to [lo, hi]def clamp(x: float, lo: float, hi: float) -> float:return max(lo, min(hi, x))# Floor and ceil division patternsdef floor_div(a: int, b: int) -> int:return a // bdef ceil_div(a: int, b: int) -> int:return (a + b - 1) // b# Safe mid for binary searchdef safe_mid(lo: int, hi: int) -> int:return lo + (hi - lo) // 2# Normalize angle to [0, 2π)def normalize_angle(theta: float) -> float:two_pi = 2 * math.pireturn theta % two_pi
Geometry & Coordinates
import mathfrom typing import TuplePoint = Tuple[float, float]# Euclidean distance between two pointsdef dist(p1: Point, p2: Point) -> float:return math.hypot(p1[0] - p2[0], p1[1] - p2[1])# Manhattan distancedef manhattan(p1: Point, p2: Point) -> float:return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])# Cross product of vectors AB x ACdef cross(ax: float, ay: float, bx: float, by: float) -> float:return ax * by - ay * bxdef cross_points(a: Point, b: Point, c: Point) -> float:return cross(b[0] - a[0], b[1] - a[1], c[0] - a[0], c[1] - a[1])# Orientation: >0 counterclockwise, <0 clockwise, 0 collineardef orientation(a: Point, b: Point, c: Point) -> float:return cross_points(a, b, c)# Area of triangle (absolute value)def triangle_area(a: Point, b: Point, c: Point) -> float:return abs(cross_points(a, b, c)) / 2.0# Polygon area by Shoelace formuladef polygon_area(poly: list[Point]) -> float:area = 0.0n = len(poly)for i in range(n):x1, y1 = poly[i]x2, y2 = poly[(i + 1) % n]area += x1 * y2 - x2 * y1return abs(area) / 2.0# Check if point p lies on segment abdef on_segment(a: Point, b: Point, p: Point) -> bool:if abs(cross_points(a, b, p)) > 1e-9:return Falsereturn (min(a[0], b[0]) <= p[0] <= max(a[0], b[0]) andmin(a[1], b[1]) <= p[1] <= max(a[1], b[1]))# Axis-aligned rectangle intersectiondef rects_intersect(a: Point, a2: Point, b: Point, b2: Point) -> bool:# rectangles: a->a2 and b->b2 as opposite cornersax1, ay1 = min(a[0], a2[0]), min(a[1], a2[1])ax2, ay2 = max(a[0], a2[0]), max(a[1], a2[1])bx1, by1 = min(b[0], b2[0]), min(b[1], b2[1])bx2, by2 = max(b[0], b2[0]), max(b[1], b2[1])return not (ax2 < bx1 or bx2 < ax1 or ay2 < by1 or by2 < ay1)# Degrees ↔ radiansdef deg_to_rad(deg: float) -> float:return math.radians(deg)def rad_to_deg(rad: float) -> float:return math.degrees(rad)
Matrix & Grid
from typing import List, TupleMatrix = List[List[int]]# Matrix multiplication (A: n x m, B: m x p)def mat_mul(A: Matrix, B: Matrix) -> Matrix:n, m, p = len(A), len(A[0]), len(B[0])C = [[0] * p for _ in range(n)]for i in range(n):for k in range(m):for j in range(p):C[i][j] += A[i][k] * B[k][j]return C# Transpose of matrixdef transpose(A: Matrix) -> Matrix:return [list(row) for row in zip(*A)]# Rotate square matrix 90 degrees clockwise (in-place)def rotate_90(A: Matrix) -> None:n = len(A)# transposefor i in range(n):for j in range(i + 1, n):A[i][j], A[j][i] = A[j][i], A[i][j]# reverse each rowfor i in range(n):A[i].reverse()# Directions for 4-neighbor grid BFS/DFSD4: list[Tuple[int, int]] = [(1, 0), (-1, 0), (0, 1), (0, -1)]D8: list[Tuple[int, int]] = D4 + [(1, 1), (1, -1), (-1, 1), (-1, -1)]def in_bounds(r: int, c: int, rows: int, cols: int) -> bool:return 0 <= r < rows and 0 <= c < cols# Build 2D prefix sumdef build_prefix_2d(grid: Matrix) -> Matrix:rows, cols = len(grid), len(grid[0])pref = [[0] * (cols + 1) for _ in range(rows + 1)]for r in range(rows):for c in range(cols):pref[r + 1][c + 1] = (grid[r][c] + pref[r][c + 1] +pref[r + 1][c] - pref[r][c])return pref# Query sum of submatrix [r1, r2] x [c1, c2] inclusive using prefixdef query_prefix_2d(pref: Matrix, r1: int, c1: int, r2: int, c2: int) -> int:return (pref[r2 + 1][c2 + 1] - pref[r1][c2 + 1] -pref[r2 + 1][c1] + pref[r1][c1])# Diagonal traversal (top-left to bottom-right)def diagonals(grid: Matrix) -> list[list[int]]:rows, cols = len(grid), len(grid[0])ans: list[list[int]] = []for s in range(rows + cols - 1):cur = []for r in range(rows):c = s - rif 0 <= c < cols:cur.append(grid[r][c])ans.append(cur)return ans# Spiral order traversal skeletondef spiral_order(grid: Matrix) -> list[int]:if not grid or not grid[0]:return []res = []top, left = 0, 0bottom, right = len(grid) - 1, len(grid[0]) - 1while top <= bottom and left <= right:for c in range(left, right + 1):res.append(grid[top][c])top += 1for r in range(top, bottom + 1):res.append(grid[r][right])right -= 1if top <= bottom:for c in range(right, left - 1, -1):res.append(grid[bottom][c])bottom -= 1if left <= right:for r in range(bottom, top - 1, -1):res.append(grid[r][left])left += 1return res
Powers, Logs & Roots
import math# Binary exponentiation (fast pow)def fast_pow(base: int, exp: int) -> int:res = 1b = basee = expwhile e > 0:if e & 1:res *= bb *= be >>= 1return res# Modular exponentiationdef mod_pow(base: int, exp: int, mod: int) -> int:return pow(base, exp, mod)# Check if n is power of twodef is_power_of_two(n: int) -> bool:return n > 0 and (n & (n - 1)) == 0# Integer square rootdef int_sqrt(n: int) -> int:return math.isqrt(n)# nth root via binary search (integer root)def int_nth_root(n: int, k: int) -> int:lo, hi = 0, nwhile lo <= hi:mid = (lo + hi) // 2p = mid**kif p == n:return midif p < n:lo = mid + 1else:hi = mid - 1return hi # floor root# Logarithm base change: log_b(x)def log_base(x: float, b: float) -> float:return math.log(x) / math.log(b)# Highest power of 2 <= ndef highest_power_of_two(n: int) -> int:if n <= 0:return 0return 1 << (n.bit_length() - 1)# Count bits using bit_lengthdef count_bits(n: int) -> int:return n.bit_length()# Example: pow with negative exponent (floating result)negative_exponent = pow(2, -3) # 0.125
Fractions & Modular Arithmetic
from fractions import Fraction# Exact rational using Fractionf1 = Fraction(1, 3)f2 = Fraction(2, 5)f_sum = f1 + f2 # 11/15# Reduce a fraction via GCDfrom math import gcddef reduce_fraction(num: int, den: int) -> tuple[int, int]:g = gcd(num, den)return num // g, den // g# Fraction to decimal (no repeat detection)def fraction_to_decimal(num: int, den: int) -> float:return num / denMOD = 10**9 + 7# Modular addition, subtraction, multiplicationdef mod_add(a: int, b: int, m: int = MOD) -> int:return (a + b) % mdef mod_sub(a: int, b: int, m: int = MOD) -> int:return (a - b) % mdef mod_mul(a: int, b: int, m: int = MOD) -> int:return a * b % m# Normalize negative modulo resultdef normalize_mod(x: int, m: int = MOD) -> int:return (x % m + m) % m# Extended GCD for modular inverse when mod not primedef extended_gcd(a: int, b: int) -> tuple[int, int, int]:if b == 0:return a, 1, 0g, x1, y1 = extended_gcd(b, a % b)return g, y1, x1 - (a // b) * y1def mod_inv(a: int, m: int) -> int | None:g, x, _ = extended_gcd(a, m)if g != 1:return Nonereturn x % m# Modular division a / b mod mdef mod_div(a: int, b: int, m: int = MOD) -> int | None:inv_b = mod_inv(b, m)if inv_b is None:return Nonereturn a * inv_b % m
Digit & Representation Tricks
# Sum of digitsdef digit_sum(n: int) -> int:s = 0x = abs(n)while x:s += x % 10x //= 10return s# Digital root (repeated digit sum)def digital_root(n: int) -> int:if n == 0:return 0return 1 + (n - 1) % 9# Reverse integerdef reverse_int(n: int) -> int:sign = -1 if n < 0 else 1x = abs(n)rev = 0while x:rev = rev * 10 + x % 10x //= 10return sign * rev# Palindrome number checkdef is_palindrome_number(n: int) -> bool:return n >= 0 and n == reverse_int(n)# Count digits in base 10def count_digits(n: int) -> int:if n == 0:return 1c = 0x = abs(n)while x:c += 1x //= 10return c# Convert to binary / hex stringdef to_binary(n: int) -> str:return bin(n)[2:]def to_hex(n: int) -> str:return hex(n)[2:]# Extract k-th digit from right (0-based)def kth_digit(n: int, k: int) -> int:return abs(n) // (10**k) % 10# Count set bits using bindef count_set_bits(n: int) -> int:return bin(n).count(\"1\")# Build number from digits listdef build_number(digits: list[int]) -> int:x = 0for d in digits:x = x * 10 + dreturn x# Generate all digit masks for n-digit number (0..(1<<n)-1)def digit_masks(n: int) -> list[int]:return list(range(1 << n))