• closed by
4,561 views
0 0 votes
closed as a duplicate of: TIME COMPLEXITY

What is time complexity of fun()?

int fun(int n)

{

  int count = 0;

  for (int i = n; i > 0; i /= 2)

     for (int j = 0; j < i; j++)

        count += 1;

  return count;

}

(A) O(n^2)
(B) O(nLogn)
(C) O(n)
(D) O(nLognLogn)

1 Answer

1 1 vote
in this problem ...how many times for loop will execute:

       first time loop:  n times

      second time :   n/2 times

        similarly  form series   =    n +n/2 +n/4 + .....+1

                 its decreasing GP series

      TC =   O(n)

Hence option C is correct
Position:
Show:

Related questions

4 4 votes
3 3 answers
4.6k
4.6k views
Hirak asked Jun 9, 2019
4,597 views
If Kruskal’s algorithm is used for finding a minimum spanning tree of a weighted graph G with n vertices and m edges and edge weights are already given in a sorted list, ...
0 0 votes
1 1 answer
752
752 views
0 0 votes
1 1 answer
3.9k
3.9k views
Pankaj Joshi asked Jan 14, 2017
3,920 views
Which of the following statements is correct about context sensitive grammar?I) In a context sensitive grammar, ε can’t be the right hand side of any productionII) In a c...
0 0 votes
2 2 answers
786
786 views
Sarvottam Patel asked Jan 13, 2017
786 views
A and B are two sets. If |A| = 5 , |B| = 3 , then, the number of onto functions from A to B are ___ ?(A) 35(B) 150(C) 29(D) 27