OlveraeUNSW
Back to problems
COMP2521

Complexity: Count a Triangular Double Loop

Complexity: count how many steps this double loop runs

A core skill of complexity analysis is counting exactly how many times a piece of code runs. This classic "triangular" double loop:

for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
        ops++;

Read n and print the total number of times ops++ runs.

Complete countOps: count it with two loops, or use the formula.

Key derivation: for a fixed outer i, the inner loop runs n-1-i times. Summing over i=0..n-1 gives (n-1)+(n-2)+…+1+0 = n(n-1)/2. That is why such loops are O(n²) (the leading term of n(n-1)/2 is n²).

Input

  • One line: integer n (0 ≤ n ≤ 100000).

Output

  • One line: the total number of ops++ executions.

Example: input 5 -> 10 (4+3+2+1+0).

Samples

stdin
5
expected
10
stdin
3
expected
3
c-dcc
14