edited by
24,783 views
50 votes
50 votes

Consider the following C function

int fun(int n) {
    int i, j;
    for(i=1; i<=n; i++) {
        for (j=1; j<n; j+=i) {
            printf("%d %d", i, j);
        }
    }
}

Time complexity of $fun$ in terms of $\Theta$ notation is

  1. $\Theta(n \sqrt{n})$
  2. $\Theta(n^2)$
  3. $\Theta(n \: \log n)$
  4. $\Theta(n^2 \log n)$
edited by

7 Answers

1 votes
1 votes

Consider for inner for loop : 

If the loop would have been:

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

Then the loop would have iterated n/2 times

So now in place of '2' we have 'i' : So my loop will iterate n/i times.

So this will vary with value of and we will get a series of : n + n/2 + n/3 + n/4........

=> n( 1 + 1/2 + 1/3.....)

=> nlogn

0 votes
0 votes
b

2 for....j varies 1...n
0 votes
0 votes
The first loop is independent, so it takes only n times, but the second loop is dependent on outer loop.so it takes o(logN)

so it takes o(NlogN).

opt c is correct
Answer:

Related questions

25 votes
25 votes
7 answers
1
khushtak asked Feb 14, 2017
6,891 views
Match the algorithms with their time complexities:$$\begin{array}{|l|l|}\hline \textbf{Algorithms} & \textbf{Time Complexity} \\\hline \text{P. Tower of Hanoi with $n$...
47 votes
47 votes
6 answers
2
Madhav asked Feb 14, 2017
17,905 views
Consider the recurrence function$$T(n) = \begin{cases} 2T(\sqrt{n})+1, & n>2 \\ 2, & 0 < n \leq 2 \end{cases}$$Then $T(n)$ in terms of $\Theta$ notation is$\Theta(\log \l...
41 votes
41 votes
4 answers
3
Arjun asked Feb 14, 2017
21,178 views
A message is made up entirely of characters from the set $X=\{P, Q, R, S, T\}$. The table of probabilities for each of the characters is shown below:$$\begin{array}{|c|c|...
29 votes
29 votes
5 answers
4
Madhav asked Feb 14, 2017
8,028 views
The Breadth First Search (BFS) algorithm has been implemented using the queue data structure. Which one of the following is a possible order of visiting the nodes in the ...