Model answer
If the outer loop uses i=0…n−1 and the inner loop executes i+1 times, total work is Σ(i+1)=n(n+1)/2. The dominant term is n²/2, so the complexity is Θ(n²).
Use the key terms, then explain the reasoning.
Rules for loops, branches, sequential blocks, and recurrences let us derive a program's overall asymptotic complexity.
Sequential blocks add costs, nested blocks multiply costs, and mutually exclusive branches contribute the cost of the selected branch—usually bounded by the maximum branch for worst-case analysis.
A loop's syntax can be misleading. Incrementing by one is linear, multiplying the counter is logarithmic, and an inner loop whose limit depends on the outer index often produces an arithmetic series.
Sequential costs add and nested costs multiply. O(n) + O(n²) simplifies to O(n²).
Open a prompt when you are ready to check your answer.