The distinction that traps everyone
Many konkur items ask for the order of the printed value of an accumulator (k, j, s, ...), not the running time. The two are different: a doubly nested loop runs in time, but the number it accumulates can be , , or anything else, depending on what is added or multiplied each step. Always re-read the question: "order of the output" means analyze the final value of the variable, not the loop count.
The recipe
- Express the accumulator as a sum or product over the loop indices. A variable updated by
+=becomes a ; one updated by*=becomes a . - Pin down each index range by tracing the loop guards (mind whether a counter is updated before or after it is used).
- Collapse the closed form with the standard identities below.
- Keep only the dominant term to get the -order (for a printed value, report the order, e.g. ; for an exact value, simplify fully).
Power-sum identities (the additive toolbox)
For a sum, only the highest power survives in : because dominates .
Exponent-sum identity (the multiplicative toolbox)
When the accumulator is a product of powers of a fixed base, the exponents add: The most common pattern is consecutive exponents, where the exponent sum is again triangular: A product grows exponentially in the loop count — never confuse it with the polynomial growth of a sum.
Mini-example (additive)
s = 0
for(i = 1, i <= n, i++)
for(j = 1, j <= i, j++)
s = s + j
Inner loop adds . Then The running time is only , but the printed value is order .
Mini-example (multiplicative)
If is multiplied by on pass for , then — exponential in .
Pitfall: "Order of the output" "time complexity." A counter incremented inside the inner loop measures steps ( here), but the value built by adding can be a full degree higher (). Decide first whether you are counting iterations or summing what each iteration contributes.