Explanation

560.Count Iterations of a Nested Loop

Easy
For the loops for (i = 0; i < n; i++) and, inside it, for (j = i + 1; j < n; j++), return how many times the inner body runs. Nested loopsNested LoopsA loop placed inside another loop; the inner loop runs completely for every single pass of the outer one. multiply: the inner loop runs completely for every pass of the outer one. Here the inner loop gets shorter each time, but the total — n(n − 1)/2 — still grows with n², so this loop shape is O(n²). It is the shape of every "check all pairs" algorithm.

Example 1:

Input: n = 4

Output: 6

Example 2:

Input: n = 1

Output: 0

+ 5 hidden test cases run on Submit.

Constraints:

  • ●0 ≤ n ≤ 2000

n =

4