Count the steps of a triangle loop
Standard · Write code · about 6 min
Two nested loops run: i goes from 0 to n - 1, and for each i, j goes from 0 to i. Return how many times the loop body runs for input n.
Example 1
Input:
[4]
Output:
10
1 + 2 + 3 + 4 = 10.
Example 2
Input:
[0]
Output:
0
Constraints
- 0 ≤ n ≤ 2,000
Learn the idea first: Big-O, Big-Θ and Big-Ω, a visual lesson.
More Big-O, Big-Θ and Big-Ω problems
- Where n² overtakes a line Standard
- Guess the growth from a doubling test Standard