Guess the growth from a doubling test
Standard · Write code · about 8 min
An algorithm took t1 steps on size n and t2 steps on size 2n. Compute r = round(log2(t2 / t1)). Return "constant", "linear", "quadratic" or "cubic" for r = 0, 1, 2 or 3 (use "cubic" for anything above 3 and "constant" for anything below 0).
Example 1
Input:
[50,100]
Output:
linear
Ratio 2, so r = 1.
Example 2
Input:
[10,40]
Output:
quadratic
Ratio 4, so r = 2.
Constraints
- 1 ≤ t1 ≤ t2 ≤ 1,000,000,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
- Count the steps of a triangle loop Standard