1,406 views
–1 –1 vote
 void find (int n) {
    if (n < 2) return;
    else {
        sum = 0;
        for (i = 1; i <= 4; i++)
            find(n / 2);
        for (i = 1; i <= n*n; i++)
            sum = sum +1;
    }  
     
 }

assume that the division operation takes constant time and sum is global variable. what is the time complexity of find(n)?

3 Answers

1 1 vote
Recurrence equation:

T(n)=4T(n/2)+n^2

solving this we get

T(n)=O(n^2 logn )(master theorem case 2)
1 1 vote

You can do this question with your intuition also.

See the first for loop. We know that we are calling the function by function(n/2). Reducing the function exponentially. That is it will run for a time complexity of O(log n).

The second loop will obviously run for n*n.

So the total time complexity will be O(n2 log n) . 

Well if you don't get it, go ahead with the conventional way. I just told with respect of exam point of view. 

0 0 votes

Since first for loop run 4logn(4T(n/2)) times and second for loop run n^2 times.

Recurrence equation:

T(n)=4T(n/2)+n^2

solving this we get

T(n)=O(n^2 logn )(master theorem case 2)

Position:
Show:

Related questions

1 1 vote
1 1 answer
120
120 views
GO Classes asked Aug 31
120 views
The algorithm $\text{ALGSORT}$ sorts an array of distinct integers using comparisons.The function $\text{MININDEX(V,i,j)}$ returns the position of the smallest element in...
1 1 vote
1 1 answer
93
93 views
GO Classes asked Aug 29
93 views
Suppose Huffman coding is implemented as follows.Initially, the $n$ symbols are stored in a min priority queue according to their frequencies.The algorithm repeatedly per...
0 0 votes
1 1 answer
67
67 views
GO Classes asked Aug 26
67 views
Consider,f1(N): x = 0 for i = 0 to N - 1: x++ return xand,f2(N, R): x = 0 for i = 0 to N - 1: for j = 1; j <= R; j = j + j: x = x + f1(j) return xWhat is the order of gro...
2 2 votes
1 1 answer
375
375 views
KrishnaVardhan asked Oct 7, 2024
375 views
Even though there are two for loops some times the Time complexity will be the m+n and some times it will be m*n assuming loops run till m and n respectively.How do we di...