Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
B.Tech S3 (S) Examinations May 2026 (2024 Scheme)QP 06PCCST303112504Part A · Q13 marksCO1Module 1asked in 2 papers

Question 1

Compute the time complexity of the following code fragment. for(i=1; i<=n; i++) for(j=1; j<=n; j=j*2) printf("KTU");

Topics: Performance analysis: time and space complexity

Solution

Assumptions:
  • n ≥ 1.

Outer loop: n iterations. Inner loop: j = 1, 2, 4, …, 2ᵏ ≤ n → ⌊log₂ n⌋ + 1 iterations.

T(n) = n · (⌊log₂ n⌋ + 1) ⇒ T(n) = Θ(n log n)

How to write it in the exam

“KTU” is printed n(⌊log₂ n⌋ + 1) times → O(n log n).

How this answer was verified
  • tests/complexity.test.ts

Similar questions