简体   繁体   中英

Time Complexity of the following Code that uses 3 nested loops

Can someone please explain the proper time Complexity of this code below.

int sum,i,j,k,n;
sum = 0;
cin>>n;
int arr * = new int[n];
for (i=1;i<n;i=i*2){
   cin>>arr[i];
   for (j=0;j<n;++j)
       for (k=1;k<=n;k=k*2)
           sum+=arr[j];
}

The bounds of the three for loops do not appear to have any interdependencies. So, we should be able to figure out the overall running time by just multiplying together the complexities of the three loops.

The loops in i and k are O(lgN) , because they double the loop counter at each iteration. The middle loop in j is O(N) . This yields O(N*lgN*lgN) as the overall complexity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM