Level 1 Training
Build your algorithmic toolkit with two pointers, binary search, recursion, bitmasks, counting, and number theory. Trace the idea, check the conditions, and practice explaining why it works.
Two pointers and binary search
Use sorted order, moving windows, and monotone decisions to eliminate repeated work. Prove each boundary movement before using a template.
Eliminate candidates with two pointers
Use the two ends of a sorted array to find a pair without checking every combination.
Find lower and upper bounds
Return a boundary first; decide separately whether it is an exact match.
Reuse progress across sorted sequences
Merge, sweep offline queries, and filter with forward-moving indices.
Slide a fixed-length window
Maintain a sum by removing one contribution and adding one contribution.
Grow and repair a variable window
Distinguish longest-under-budget from shortest-reaching-target windows.
Count windows and maintain frequencies
Count all valid starts, or store occurrences when a sum is not the right summary.
Binary-search a monotone answer
Turn optimization into a decision problem with a proved truth direction.
Prove the checker before searching
Connect greedy scans, capped arithmetic, and two-pointer counts to answer search.
Recursion and backtracking
Describe one call, prove progress, and explore choices while restoring shared state. Separate correctness, call count, and stack depth.
Give one recursive call a contract
Separate the meaning of a call, its base case, and its progress toward termination.
Shrink an index, a number, or a range
Choose parameters that describe remaining work without copying it.
Count calls separately from stack depth
Distinguish a chain, a branching tree, and divide-and-conquer work.
Choose, explore, and undo
Generate decisions without letting one branch contaminate the next.
Match stopping and pruning to the question
Existence, one witness, counts, and enumeration need different return rules.
Generate combinations and permutations
Encode whether order matters, and avoid equivalent duplicate-value branches.
Reject impossible prefixes early
Use local validity rules for balanced parentheses and nonattacking queens.
Trace paths and recognize reusable states
Distinguish current-path visitation from permanent visitation and memoized answers.
Bit manipulation and bitmasking
Give every bit a meaning. Use unsigned operations, XOR identities, compact sets, and subset enumeration with explicit width limits.
Read binary and choose a width
Separate numeric value, written representation, and the width of the type.
Check, set, clear, and toggle
Choose a bitwise operation from its one-bit truth table.
Count and isolate set bits
Derive low-bit operations and use C++ bit utilities with valid unsigned inputs.
Cancel equal values with XOR
Use occurrence promises to find one or two odd-frequency values.
Answer range XOR queries
Reuse prefix cancellation and the four-case XOR pattern for consecutive integers.
Represent small sets with masks
Model presence, union, intersection, and coverage without confusing frequency with membership.
Enumerate subsets and optimize a choice
Give every position one take-or-skip bit and evaluate each mask exactly once.
Visit submasks and larger bitsets
Choose between a machine-word mask, a fixed bitset, and an exponential search.
Counting principles
Define one outcome, decide whether order and reuse matter, and count it exactly once. Derive formulas and protect intermediate arithmetic.
Add cases, multiply stages, subtract bad outcomes
Choose the counting operation from the structure of an outcome.
Arrange distinct objects and ordered roles
Derive factorials and falling products from choices without replacement.
Count unordered groups
Explain why dividing ordered selections by r! removes exactly the repeated orders.
Compute combinations with Pascal’s identity
Build exact or modular answers using addition instead of unsafe factorial division.
Choose groups, positions, and route steps
Translate different stories into precise combinations and independent stages.
Count contributions and correct overlap
Group equal pairs and derive inclusion-exclusion by tracking how often one object is counted.
Enumerate overlapping conditions
Use subset parity and LCM to count a union of divisibility conditions.
Handle repeated objects and arithmetic safely
Extend the model to repeated letters, circles, distributions, and exact calculations.
Number theory - Part 1
Understand divisor pairs and prime exponents, then choose a sieve or factor table that matches the number and size of your queries.
Find divisors in pairs
Replace a full scan with an integer-safe square-root boundary.
Prove a primality test
Use the smallest-factor argument and respect the input bounds.
Extract prime factors and exponents
Shrink the remaining number while preserving the original when needed.
Count and sum divisors from exponents
View each divisor as an independent choice of exponents.
Prepare a sieve of prime flags
Share composite-marking work across a bounded range.
Store the smallest prime factor
Prepare useful witnesses for many bounded factorization queries.
Choose tables from the workload
Combine prime prefixes, exponent summaries, and divisor tables.
Extend factorization carefully
Recognize segmented sieves, factorial exponents, and square completion.
Number theory - Part 2
Connect common factors, modular arithmetic, fast powers, and inverses. Check the conditions that make division and shortcuts valid.
Preserve common divisors with Euclid
Replace a pair with a smaller equivalent pair until the remainder is zero.
Combine periods with a safe LCM
Divide first and guard the product that remains.
Keep residues and normalize them
Preserve remainders under addition and multiplication, and treat division separately.
Build powers by repeated squaring
Reuse binary exponent contributions for logarithmic work.
Check that an inverse exists
Use coprimality before extended Euclid or the prime-modulus shortcut.
Prepare modular combination tables
Use factorial inverses only under their prime and range conditions.
Handle several solutions and GCD summaries
Solve linear congruences and combine prefixes with suffixes.
Keep the arithmetic contract explicit
Connect totients, huge exponents, congruences, and division-free sums.