458 views

2 Answers

2 votes
2 votes

Answer is O(nlogn) while loop depends on which is reduced by 2 so while loop run logn times . For loop run approx n times for each i value. so total = n $\times$ logn= O(nlogn)

1 votes
1 votes

it can be rewritten as 

for(i=n;i>0;i/=2)

{

k=1;

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

k++;

}

---------------------------------------------------

outer for loop execution

1st iteration i=n

2nd iteration i=n/2

3rd itearation i=n/4

.

.

kth iteration i=n/2^k;

so n/2^k>0  equate to for loop condition)

n/2^k=1 then k=log(n).

coming to inner for loop.

for j=1,2,3,....n .it executes 'n' times.

so total time complexity is o(nlog(n))

Related questions

0 votes
0 votes
1 answer
1
2 votes
2 votes
1 answer
2
gauravkc asked Apr 5, 2018
906 views
What is the time complexity of this code?
3 votes
3 votes
1 answer
3
0 votes
0 votes
3 answers
4
pranab ray asked Jan 13, 2018
422 views
i am getting t.c as O(n^5) but given answer as O(n^4) what should be the answer