Describe growth with Big-O
Name the input sizes and separate operation counts, growth bounds, and actual runtime.
First choose what n means
For an array, n can mean its active length. For a string, L is its stored character count. A matrix has independent row and column counts r and c. A divisor test measures the numeric value x, which is different from the number of digits used to write x.
The source uses a fixed-width unit-cost model: ordinary arithmetic, comparison, assignment, and valid array indexing have constant cost. This is a useful model for bounded C++ inputs, not a promise that arbitrary-precision arithmetic or a whole long string operation takes one step.
An upper bound is not a stopwatch
A count such as 3n + 7 grows linearly, so O(n) describes an asymptotic upper bound. O(1) means a bounded amount of work independent of the named size, not literally one instruction. For 2n² + 5n + 8, the quadratic term dominates as n grows.
Θ(n) describes a tight bound, with matching upper and lower growth. Big-O itself does not mean worst case. A linear search has constant best-case work when the first element matches, and linear worst-case work when the target is absent. Average-case claims need a stated input model.
Sequential costs add
One n-element scan followed by another makes 2n body visits, so its growth is still O(n). Scans of independent lengths n and m cost O(n + m). A fixed stride such as i += 3 changes the number of visits by a constant factor, not the linear growth.
For a conditional, count the condition plus the executed branch. A worst-case bound uses the most expensive reachable branch; mutually exclusive branches do not both run. A function called by the condition may itself be expensive.
// Complete C++23 program. Input: 0 <= n,m <= 1000000.
#include <iostream>
int main() {
int n = 0, m = 0;
std::cin >> n >> m;
long long visits = 0;
for (int i = 0; i < n; ++i) ++visits;
for (int j = 0; j < m; ++j) ++visits;
std::cout << visits << '\n'; // n + m body visits
}Growth explorer
Idealized operation counts show growth. Actual runtime also depends on the implementation and machine.
O(1)1O(log n)7O(n)100O(n log n)665O(n²)10,000Bar lengths use a logarithmic scale. Logarithms use base 2.
Guided simulation · Change the inputs to test your understanding.
One loop visits n elements, then another visits 5n elements. What is the total growth?
Turn the idea into code
For 0 ≤ n ≤ 10⁶, count the body visits in for (int i = 0; i < n; i += 3). Give an exact count, a tight growth bound, and auxiliary space. Check n = 0, 1, and 8.
Give me a hint
List 0, 3, 6, … below n. A partial final group still contributes one visit.
Show the explained solution
The exact count is ceil(n/3), or (n + 2) / 3 under these bounds: 0, 1, and 3 for the requested cases. Including setup, time is Θ(n + 1); conventional positive-size shorthand is Θ(n). The counter and index use O(1) auxiliary space.
Your notes
Mark complete when you can explain it yourself.