module is based off this
Intro and examples
More in-depth. In particular, 5.2 gives a formal definition of Big O.

In programming contests, your program needs to finish running within a certain timeframe in order to receive credit. For USACO, this limit is 22 seconds for C++ submissions, and 44 seconds for Java/Python submissions. A conservative estimate for the number of

operations
the grading server can handle per second is 10810^8, but it could be closer to 51085 \cdot 10^8 given good constant factors
If you don't know what constant factors are, don't worry -- we'll explain them below.
.

Complexity Calculations

We want a method to calculate how many operations it takes to run each algorithm, in terms of the input size nn. Fortunately, this can be done relatively easily using Big O Notation, which expresses worst-case time complexity as a function of nn as nn gets arbitrarily large. Complexity is an upper bound for the number of steps an algorithm requires as a function of the input size. In Big O notation, we denote the complexity of a function as O(f(n))O(f(n)), where constant factors and lower-order terms are generally omitted from f(n)f(n). We'll see some examples of how this works, as follows.

O(1) examples

The following code is O(1)O(1), because it executes a constant number of operations.

int a = 5;
int b = 7;
int c = 4;
int d = a + b + c + 153;
int a = 5;
int b = 7;
int c = 4;
int d = a + b + c + 153;
a = 5
b = 7
c = 4
d = a + b + c + 153

Input and output operations are also assumed to be O(1)O(1).

Loops

In the following examples, we assume that the code inside the loops is O(1)O(1).

The time complexity of loops is the number of iterations that the loop runs. For example, the following code examples are both O(n)O(n).

for (int i = 1; i <= n; i++) {
    // constant time code here
}
int i = 0;
while (i < n) {
    // constant time node here
    i++;
}
for (int i = 1; i <= n; i++) {
    // constant time code here
}
int i = 0;
while (i < n) {
    // constant time node here
    i++;
}
for i in range(1, n+1):
    # constant time code here
i = 0
while (i < n):
    # constant time node here
    i += 1

More Loops

Because we ignore constant factors and lower order terms, the following examples are also O(n)O(n):

for (int i = 1; i <= 5*n + 17; i++) {
    // constant time code here
}
for (int i = 1; i <= n + 457737; i++) {
    // constant time code here
}
for (int i = 1; i <= 5*n + 17; i++) {
    // constant time code here
}
for (int i = 1; i <= n + 457737; i++) {
    // constant time code here
}
for i in range(5*n + 17):
    # constant time code here

for i in range(n + 457737):
    # constant time code here

Nested Loops

We can find the time complexity of multiple loops by multiplying together the time complexities of each loop. This example is O(nm)O(nm), because the outer loop runs O(n)O(n) iterations and the inner loop O(m)O(m).

for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
        // constant time code here
    }
}
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
        // constant time code here
    }
}
for i in range(n):
    for j in range(m):
        # constant time code here

More Nested Loops

In this example, the outer loop runs O(n)O(n) iterations, and the inner loop runs anywhere between 11 and nn iterations (which is a maximum of nn). Since Big O notation calculates worst-case time complexity, we treat the inner loop as a factor of nn.

We can also do some math to calculate exactly how many times the code runs: 1+2+...+n = n*(n+1)/2 = (n^2 + n)/2 = O(n^2)
Thus, this code is O(n2)O(n^2).

for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
        // constant time code here
    }
}
for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
        // constant time code here
    }
}
for i in range(n):
    for j in range(i, n):
        # constant time code here

Multipart Algorithms

If an algorithm contains multiple blocks, then its time complexity is the worst time complexity out of any block. For example, the following code is O(n2)O(n^2).

for (int i = 1; i <= n; i++) {
    for(int j = 1; j <= n; j++) {
        // constant time code here
    }
}
for (int i = 1; i <= n + 58834; i++) {
    // more constant time code here
}
for (int i = 1; i <= n; i++) {
    for(int j = 1; j <= n; j++) {
        // constant time code here
    }
}
for (int i = 1; i <= n + 58834; i++) {
    // more constant time code here
}
for i in range(n):
    for j in range(n):
        # constant time code here

for i in range(n + 58834):
    # more constant time code here

Multipart Algorithms

The following code is O(n2+m)O(n^2 + m), because it consists of two blocks of complexity O(n2)O(n^2) and O(m)O(m), and neither of them is a lower order function with respect to the other.

for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
        // constant time code here
    }
}
for (int i = 1; i <= m; i++) {
    // more constant time code here
}
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
        // constant time code here
    }
}
for (int i = i; j <= m; i++) {
    // more constant time code here
}
for i in range(n):
    for j in range(n):
        # constant time code here

for i in range(m):
    # more constant time code here

Common Complexities and Constraints

Complexity factors that come from some common algorithms and data structures are as follows:

Don't worry if you don't recognize most of these! They will all be introduced later.

  • Mathematical formulas that just calculate an answer: O(1)O(1)
  • Binary search: O(logn)O(\log n)
  • Ordered set/map or priority queue: O(logn)O(\log n) per operation
  • Prime factorization of an integer, or checking primality or compositeness of an integer naively: O(n)O(\sqrt{n})
  • Reading in nn items of input: O(n)O(n)
  • Iterating through an array or a list of nn elements: O(n)O(n)
  • Sorting: usually O(nlogn)O(n \log n) for default sorting algorithms (mergesort, Collections.sort, Arrays.sort)
  • Java Quicksort Arrays.sort function on primitives: O(n2)O(n^2)
  • Iterating through all subsets of size kk of the input elements: O(nk)O(n^k). For example, iterating through all triplets is O(n3)O(n^3).
  • Iterating through all subsets: O(2n)O(2^n)
  • Iterating through all permutations: O(n!)O(n!)

Bounds

Here are conservative upper bounds on the value of nn for each time complexity. You might get away with more than this, but this should allow you to quickly check whether an algorithm is viable.

nnPossible complexities
n10n \le 10O(n!)O(n!), O(n7)O(n^7), O(n6)O(n^6)
n20n \le 20O(2nn)O(2^n \cdot n), O(n5)O(n^5)
n80n \le 80O(n4)O(n^4)
n400n \le 400O(n3)O(n^3)
n7500n \le 7500O(n2)O(n^2)
n7104n \le 7 \cdot 10^4O(nn)O(n \sqrt n)
n5105n \le 5 \cdot 10^5O(nlogn)O(n \log n)
n5106n \le 5 \cdot 10^6O(n)O(n)
n1018n \le 10^{18}O(log2n)O(\log^2 n), O(logn)O(\log n), O(1)O(1)

Note about Bronze

A significant portion of Bronze problems will have n100n\le 100. This doesn't give much of a hint regarding the intended time complexity. The intended solution could still be O(n)O(n)!

Constant Factor

Constant factor refers to the idea that different operations with the same complexity take slightly different amounts of time to run. For example, three addition operations take a bit longer than a single addition operation. Another example is that although binary search and set insertion are both O(logn)O(\log n), binary searching is noticeably faster.

Constant factor is entirely ignored in big-O notation. This is fine most of the time, but if the time limit is particularly tight, you may TLE with the intended complexity. When this happens, it is important to keep the constant factor in mind. One example is, a piece of code that iterates through all ordered triplets runs in O(n3)O(n^3) time, but could be sped up by a factor of 66 by iterating through unordered triplets.

For now, don't worry about optimizing constant factors -- just be aware of them.