Computer Science EngineeringVery hard

What is the Theta bound of: for(i=1;i<=n;i++) for(j=i;j<=n;j+=i) count++;

  1. A. Theta(n)
  2. B. Theta(n^2)
  3. C. Theta(log n)
  4. D. Theta(n log n)

Correct answer: D. Theta(n log n)

The inner loop runs about n/i times, and the sum of n/i over i is n times the harmonic number, i.e. Theta(n log n).

More Algorithms, Data Structures & Programming (ISRO CS) questions

Exams that ask this topic