Reauditing Algorithm Complexity in Competitive Programming

    Official solutions do not always achieve the best possible asymptotic complexity. This article presents a practical workflow for revisiting competitive programming problems: formalize constraints, use large language models to generate candidate approaches, test their assumptions, and validate improvements through proofs, independent implementations, and adversarial testing. It also explains how to document genuine complexity improvements without confusing them with constant-factor gains or unsupported claims.

    Once an official solution is published for a competitive programming problem, the community often assumes that its time complexity is close to the problem’s lower bound. Experience in recent years suggests that this assumption does not always hold. Some older problems still admit overlooked improvements when their constraints allow offline processing, incremental answer maintenance, or state merging.

    A reusable approach is to treat a large language model as a candidate idea generator, while leaving proofs, implementation, and stress testing to humans. This article focuses on that technical workflow rather than any particular model or platform.

    What a Complexity Reaudit Should Establish

    The goal is not simply to make a program run a little faster. A reaudit should answer three separate questions:

    1. Does the official solution fully exploit the problem’s constraints?
    2. Is there an asymptotically better algorithm that is correct for every valid input?
    3. Is the claimed improvement actually a constant-factor optimization, an implementation trick, or the result of a flawed proof?

    These questions must remain separate. Constant-factor improvements can help in practice, but they do not change the recorded asymptotic complexity. An approach that is faster on random inputs but degrades on constructed inputs is not an asymptotic improvement either.

    Problems worth reviewing first often have one or more of these characteristics:

    • Counting or expectation calculations whose transitions appear to require enumerating every state.
    • Batch queries over intervals, trees, or the plane, where the official solution uses an online data structure even though the statement does not require online processing.
    • Dynamic maintenance of extrema, connectivity, or contributions, where updates and queries can be reordered.
    • Constraints that specify upper bounds while the operations also exhibit monotonicity, reversibility, or small incremental changes.

    Expectations should be lower when the problem requires online processing, limits the number of interactions, or has a lower bound established through information theory, the comparison model, or a reduction from a known hard problem. In those cases, the more useful task may be to establish why the complexity cannot be reduced, rather than search blindly for a new algorithm.

    First, Reduce the Statement to a Computational Specification

    Before exploring alternatives, set aside the problem’s narrative and express it as four components: input, output, constraints, and permitted error.

    For the input, distinguish between static arrays, dynamic updates, mandatory online queries, and static graph edge sets. For the output, distinguish between exact integers, modular results, and floating-point answers with absolute or relative error tolerances.

    List n, m, the value range, the modulus, and the time limit separately. Complexity improvements usually come from exploiting a dimension that the original solution did not use effectively.

    Also prepare a resource ledger for the official solution:

    • Preprocessing cost.
    • Cost per update.
    • Cost per query.
    • Total running time.
    • Additional space.
    • Essential assumptions.

    For example, “recompute everything after each update” and “updates can be rolled back and queries can be processed offline” describe very different opportunities. Without a clear ledger, there is no reliable baseline for an optimization claim.

    Fix the correctness criteria before proceeding. Integer problems require exact agreement across all valid inputs. For modular problems, check whether the modulus is prime and whether multiple moduli are allowed. For floating-point problems, construct boundary tests using the stated error definition rather than the number of decimal places shown in the samples.

    A model-generated formula that omits the conditions for a modular inverse to exist, or ignores integer overflow, is incomplete—not almost ready to use.

    Use Models to Generate Candidates, Not Conclusions

    Give an AI coding tool the problem statement, constraints, original complexity, and a request to propose strictly better asymptotic complexity while identifying failure conditions. It may suggest several directions:

    • A data structure better matched to the operations.
    • Offline processing instead of online processing.
    • Contribution decomposition instead of element-by-element simulation.
    • Number-theoretic or generating-function techniques to compress states.
    • Divide and conquer or parallel binary search to reduce repeated work.

    These suggestions are valuable because they broaden the search space, not because they can be accepted without verification.

    Apply four filters to each candidate immediately:

    1. State its claimed total complexity. Compare it with the original solution using the same notation and analysis model. Do not compare an amortized bound on one side with a worst-case bound on the other.
    2. Identify new assumptions. These may include offline access, coordinate compression, the existence of inverses, graph sparsity, or rollback support.
    3. Try to break it with small counterexamples. Test empty intervals, single elements, identical values, extreme values, modulus boundaries, and interleaved updates that must be answered online.
    4. Proceed to a proof or reference implementation only if those attempts fail. If the model cannot justify where a new assumption comes from, retain the proposal as inspiration rather than labeling it an established improvement.

    When reviewing problems in batches, avoid leaking a known target complexity into the prompt. Specifying the desired bound after the fact can encourage the model to force an argument while omitting correctness conditions.

    A safer question is:

    Under these constraints, which algorithm families have strictly better complexity than the current resource ledger and remain correct for every valid input? What assumptions does each require?

    Structures That Commonly Enable Lower Complexity

    Offline Processing

    Offline processing is one of the most common opportunities. If queries do not depend on earlier answers, they may be sorted by a key and processed using a sweep line, two pointers, or divide and conquer. Multiple independent queries can then become a single ordered traversal.

    The prerequisite is that the statement allows all queries to be read in advance. If the next update is provided only after the previous query has been answered, the offline argument no longer applies.

    Incremental Maintenance

    Incremental maintenance works when contributions can be added, removed, or rolled back. Instead of recomputing a statistic from scratch, maintain sufficient statistics for the current window and prove how an insertion or deletion changes the expectation or extremum being tracked.

    The maintained statistics must actually be sufficient. Keeping only the current answer is often inadequate; frequencies, prefix sums, convex hulls, or connected-component information may also be needed.

    Rollback must follow a supported discipline, such as last-in, first-out reversal or an invertible operation. It does not imply that arbitrary deletion is inexpensive.

    State Compression

    State compression often arises in counting problems and dynamic programming. If several state dimensions interact only through a sum, difference, parity, or residue, transitions may be consolidated through prefix-sum optimization, monotone optimal decisions, or matrix and polynomial representations.

    A common mistake is to turn “fewer transitions on this input” into “states can be merged on every input.” The argument must establish the relevant inclusion relationship between decision ranges or the associative semigroup structure that makes merging valid. Otherwise, the method is only heuristic pruning.

    Data Structure Replacement

    A replacement data structure must match the entire operation set. Point updates and range sums can be handled by a Fenwick tree or segment tree. Adding historical versions, range increments, and persistence changes the complexity ledger.

    “Can support the operations” is not the same as “is asymptotically better.” Compare the total number of operations multiplied by their individual costs, and include construction and coordinate compression.

    Awareness of Lower Bounds

    Comparison sorting, element uniqueness, some online dynamic problems, and known hard counting problems cannot be made arbitrarily cheaper merely by rewriting the solution.

    A reaudit report should allow the conclusion:

    No better algorithm was found, and the following obstacles remain.

    That is more useful than an unproved claim of lower complexity.

    Close the Loop Between Proof, Implementation, and Regression Testing

    A candidate that survives initial filtering needs at least three parts of an argument:

    • Correctness: Explain the invariants preserved by each operation.
    • Complexity: Distinguish worst-case, amortized, and expected bounds. For randomized algorithms, specify whether the randomness comes from the algorithm itself.
    • Boundaries: Identify input families that would invalidate the approach.

    A proof can be semiformal, but its key lemmas cannot rest solely on “obviously.”

    Implement the candidate against the same input and output specification as the original solution. Prepare four categories of tests:

    1. Statement samples and hand-built small inputs: Use these for differential testing.
    2. Random inputs: Use these to uncover implementation errors.
    3. Adversarial inputs based on the candidate’s failure modes: Use these to check that its cost does not fall back to the original complexity or become worse.
    4. Inputs near the constraint limits: Use these to check whether the implementation finishes within reasonable resources.

    Passing large tests on one machine is not a proof of asymptotic complexity.

    During differential testing, treat both the original and the new implementation as potentially incorrect. When they disagree, minimize the input first. Then determine whether the cause is a counterexample to the new algorithm, a flaw in the original solution, or a difference in formatting, modular arithmetic, or overflow behavior.

    Use integer types with explicitly sufficient width. Normalize modular values consistently after additions and subtractions. If the complexity claim is amortized, count the total cost of an operation sequence rather than reporting the runtime of individual calls.

    How to Document a New Complexity Result

    A credible record should include:

    • A neutral restatement of the problem.
    • The original complexity ledger.
    • The new complexity ledger.
    • Any additional assumptions.
    • The main correctness arguments.
    • The counterexamples attempted.
    • The scope of differential testing.

    A conversation transcript or a model’s wording is not evidence.

    If the new method is better only in expectation, label that explicitly. If it uses randomization without analyzing failure probability, state that limitation separately. Such results should not be presented as equivalent to deterministic worst-case guarantees.

    Problem setters and solution maintainers can adopt a repeatable review process:

    1. Each year, select a subset of frequently referenced older problems whose solutions may leave some constraint underused.
    2. Have humans identify relevant lower-bound barriers first.
    3. Ask models only to generate candidate algorithm families.
    4. Have independent implementers carry out differential testing.

    When publishing the results, include failed approaches as well. Explain why seemingly faster methods do not work under mandatory online processing or extreme value ranges. This uses the exploratory strength of generative tools while keeping complexity claims verifiable.

    The same method is useful for post-contest review. First write a resource ledger for your own solution. Then compare it with better candidates and work through the missing invariants, rather than simply memorizing the final result.

    The foundations of complexity optimization remain constraints and invariants. The tool’s role is to propose hypotheses that can be disproved.