• edited by
3,735 views
0 0 votes

 

 

Consider the following piece of code
```
for(i=1; i {
for(j=n; j>0; j=2)
{
for(k=j; k {
sum+=(i+j*k);
}
}
}
```

The time complexity of above code $\qquad$
a) $O(n \log n)$
b) $\mathrm{O}\left(\mathrm{n}(\log )^{2}\right)$
c) O(nloglogn)
d) O(loglogn)

2 Answers

0 0 votes
For purpose of solving I am assuming that n=2$^{c}$ where c>1

1) outer loop or i$^{th}$ loop will execute (log n) times

2) middle loop or j$^{th}$ loop will execute (log n)+1 times

3) now for last loop or K$^{th}$ loop will run for :

$\sum_{k=1}^{log n+1}  \left ( \frac{n}{2}+1-k \right )$

 

Eg:

for n=8, 1$^{st}$ loop will execute 3 times

now 2$^{nd}$ will  execute  (log n +1) i.e 4 times

and 3$^{rd}$ loop will execute 10 times as per formula

 

we can simplify $\sum_{k=1}^{log n+1}  \left ( \frac{n}{2}+1-k \right )$ as = $\frac{n}{2} - \left ( 1+2+3...log n \right )$

since -(1+2+3...log n )  is negligible we will omit in result

so final answer is (log n)*((log n)+1)*($\frac{n}{2}$)= O(n* (log n)$^{2}$)

 Answer is B
Position:
Show:

Related questions

0 0 votes
1 1 answer
1.6k
1.6k views
GateAspirant999 asked Sep 16, 2018
1,576 views
Consider the following sorting algorithmSorting (A, low, high)Iif (low == high) return;if (low $+1==$ high)Iif $(\mathrm{A}[$ low $]>\mathrm{A}[$ high $])$swap (A[low], A...
0 0 votes
3 3 answers
2.7k
2.7k views
Deepalitrapti asked Sep 12, 2018
2,695 views
14Computer Science \& ITAlgorithm, DataQ. 78 Given a sorted array of n-elements where other than one element $x$ every other element repeat two times. Then how much time ...
1 1 vote
2 answers 2 answers
2.4k
2.4k views
srestha asked Aug 17, 2018
2,359 views
What will be TC here?Ans given $O(n^{2})$ , while I am getting $O(n)$What is the time complexity of function ()?void function (int $n$ ) {int sum = 0;for (int i=0;i0;j ...
1 1 vote
2 2 answers
3.9k
3.9k views
HeadShot asked Aug 8, 2018
3,920 views
19. Find the complexity of the following code fragment:\[\begin{array}{l}\text { int } \mathrm{i}, \mathrm{j}, \mathrm{k} \text {, count } 0 ; \\\text { for }(i=n ; i>=1 ...