closed by
3,463 views
0 votes
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)

closed by

1 Answer

1 votes
1 votes
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

Related questions

1 votes
1 votes
1 answer
3
thor asked Nov 23, 2016
599 views
0 votes
0 votes
1 answer
4
rsansiya111 asked Dec 18, 2021
387 views