Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Algorithms | Analysis of Algorithms | Question 18

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Consider the following function,

 int unknown(int n) {
    int i, j, k = 0;
    for (i  = n/2; i <= n; i++)
        for (j = 2; j <= n; j = j * 2)
            k = k + n/2;
    return k;
 }

What is the time complexity of the function? (GATE CS 2013)

(A)

n^2

(B)

n logn

(C)

n^3

(D)

n^3 logn 


Answer: (B)

Explanation:

In the below explanation, \’^\’ is used to represent exponent: The outer loop runs n/2 or Theta(n) times. The inner loop runs (Logn) times (Note that j is multiplied by 2 in every iteration). So the overall time complexity is nLogn.


Quiz of this Question
Please comment below if you find anything wrong in the above post

My Personal Notes arrow_drop_up
Last Updated : 29 Jul, 2021
Like Article
Save Article
Similar Reads