Skip to main contentSkip to solution

What will be the running time complexity of an algorithm that does not have any loop?

Solution

✅ Correct Option: 1

An algorithm with no loop (and no recursion) executes each statement a fixed number of times, whatever the input size nn. Its running time therefore does not grow with nn, so it is constant, written O(1)O(1).


Options 2, 3 and 4: nn, n2n^2 and n3n^3 come from one loop, two nested loops and three nested loops respectively.

Recursion can also make an algorithm take more than constant time without any explicit loop, so the answer O(1)O(1) holds only when there is neither a loop nor recursion.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question