Skip to main content

Complexity Quiz: Medium

Editorial

Complexity Quiz: Medium

Fifteen pieces of code that look slower or faster than they are: loops that shrink, loops that jump, library calls that hide a loop, simple recursions, and a sort's best and worst inputs. Every answer was checked by running the code on our judge in C++, Python, Java and JavaScript at three sizes of n, 1,000, 2,000 and 4,000 unless the answer names others: the four counted the same steps, and the growth in the answer fitted those counts best. Where the cost hides inside a library call, every language's times were checked instead.

Complexity Quiz: Medium

1 / 15

Multiple choice

The inner loop jumps by i. What is the time complexity?
long long count = 0;
for (int i = 1; i <= n; i++)
    for (int j = 0; j < n; j += i)
        count++;