retagged by
388 views

1 Answer

Best answer
2 votes
2 votes

Answer : D

Here observe that inner loop runs independent of value i, rather it depends on value of n.

So let's see first its complexity.

for (j=1;j<=n;j++){

        k++;

}

Here k incremented by 1 in each step, and is added to j , after 1st iteration k is 2, then after 2nd step  k is 3, so in general after m interations j will have value as sum of first m nums. So breaking condition is m(m+1)/2 > n  ==> m = O(√n) 

Now outer loop : 

while(i>0){

      //inner loop code here

     i = i/2;

}

say this loop runs m times and as i divides by 2 and checked i>0 so least value is 1.

so after i = n/2^m and and i=1 for which it will iterate last i.e m => logn hence in total it takes logn+1 iterations 1 to break the condition.

So total complexity is outer loop * inner loop ==> √n (logn+1) ==> O(√nlogn).

selected by
Answer:

Related questions

1 votes
1 votes
2 answers
1
amit166 asked Jan 5, 2023
491 views
In a host size for PDU of network layer is 17076 bytes, MTU size for that network is 200 bytes and IPv4 header size is 20 bytes .find number of IP fragments
3 votes
3 votes
2 answers
2
0 votes
0 votes
0 answers
3
Neelu Lalchandani asked Nov 2, 2022
345 views
Time Complexity in C will be O(n) right? and big omega (n) is also big omega (n^2), then why is c incorrect?
0 votes
0 votes
1 answer
4