ProgrammingHardTCS Digital · Infosys SP
What is the time complexity of the following pseudocode, as a function of n?
What is the time complexity of the following pseudocode, as a function of n?
for (i = 1; i * i <= n; i = i + 1)
for (j = 1; j <= n; j = j * 2)
count = count + 1
end for
end for- A. O(n log n)
- B. O(n^(1/2) log n)
- C. O(n^(1/2))
- D. O(n)
Correct answer: B. O(n^(1/2) log n)
The outer loop stops when i² > n, so it runs about √n times; the inner loop doubles j up to n, about log₂ n times. Total O(√n · log n), written O(n^(1/2) log n).
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What is the time complexity of the following pseudocode, as a function of n? Function f(Integer n) if (n <= 1) return 1 end if return f(n…
- What will be the output of the following pseudocode? (OR stops evaluating once the left side is true; ++c adds 1 to c and then gives the…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer nxt[8] = {1, 2, 3, 4, 5, 6, 7, 3} Integer slow =…
- What will be the output of the following pseudocode? (mod is applied to (i j) before adding) Integer x = 0 for (each i from 1 to 5) for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer A[2][3] = {{1, 2, 3}, {4, 5, 6}} Integer Bm[3][2]…
- What will be the output of the following pseudocode? Function J(Integer n, Integer k) if (n == 1) return 0 end if return (J(n - 1, k) + k)…