Search⌘ K

Non Trivial Runtime

Explore the concept of non trivial runtime in algorithms by analyzing a C++ nested loop example that forms a geometric progression. Understand how the runtime complexity simplifies to linear O(N) despite logarithmic outer loops, helping you grasp practical complexity analysis for competitive programming.

We'll cover the following...

Sum of powers

Take the code sample below:

for (int i = 1; i <= N; i *= 2)
    for (int j = 1; j <= i; j++)
        x++;

As discussed earlier, the outer loop runs (logN+1)(logN + 1) times. The number of times the inner loop runs is equal to the first loop variable ii.

  • Iteration 11: Inner loop runs for 11 time
  • Iteration 22: Inner loop runs for 22 times
  • Iteration 3
...