edited by
3,883 views

1 Answer

Best answer
3 votes
3 votes

two for loops are dependent on each other.

so execute for some value of i and calculate

initially i=1 --------- j=1

i=2 then j=1,2

i=4 then j=1,2,4

i=8 then j=1,2,4,8 times executed.

calculate no of iterations of outer loop=1,2,4,8,...............$2^{k}$

$2^{k}$=n(equate to for loop condition)

k=logn.

and inner for loop execute one more than outer loop

so inner for loop no of iterations=logn+1.

Hence time complexity=logn*(logn+1)=$O((logn)^{2})$

                             

selected by

Related questions

0 votes
0 votes
0 answers
2
0 votes
0 votes
1 answer
3
NeelParekh asked Jul 27, 2023
283 views
If an array is split in the form of increasing and decreasing order then what is TC to find minimum element in the array?
2 votes
2 votes
1 answer
4
h4kr asked Dec 30, 2022
477 views
What is the correct time complexity in $\theta()$ ?