531 views
0 votes
0 votes
#include<stdio.h>
int main()
{
int sum =0;
 for(limit=1;limit<=n;limit*=2)
 {
     for(i=0;i<limit;i++)
     {
         for(j=0;j<n;j+=2)
         {
             sum+=j;
         }
         for(j=1;j<n;j*=2)
         {
             sum*=j;
         }
     }
   }
}

1 Answer

0 votes
0 votes

The outer loop of limit will run will run for Logn 

It contain one inner loop of ith loop

 ith loop will also run for logn times

 Jth loop runs for n/2 times

And next jth loop will run for logn time

Hence th total complexity for the ith loop becomes

Logn(n/2+logn)=nlogn+(logn)^2

For the outer loop

The total complexoty will become

Logn(nlogn+(logn)^2)

Making the overall complexity=bigoh(n(logn)^2)

#please correct if i m wrong

Related questions

0 votes
0 votes
1 answer
1
Rackson asked Jan 12, 2019
951 views
0 votes
0 votes
0 answers
2
1 votes
1 votes
0 answers
3
srestha asked Jan 17, 2018
470 views
foo(int n, int a[]) { int i=0,j=0; for(i=0;i<=n;i++) while (j<n && A[i]<A[j]) j++; }Time complexity of foo()