edited by
1,768 views
1 1 vote

What is the time complexity of the following function foo()
 

void foo() {
  int i, j;
  for(i = 1; i <= n ; i++)
    for(j = i; j <= log(i); j++)
      printf(“gate”);
}


 

  • what is the time complexity?

  • the answer given is nlogn.
  • but I think it should be O(n)

1 Answer

0 0 votes
It should be nlogn only.

When the value of i is 1-----runs for log1 times

When I is 2 log 2 times

When i is 3 log 3 times

.

.

 

When i is  n log n times

So total times it runs is log1 + log 2+log 3+...logn =logn!= nLogn
Position:
Show:

Related questions

0 0 votes
0 0 answers
1.8k
1.8k views
Naveen Kumar 3 asked Nov 3, 2018
1,781 views
Suppose, we have an array of n elements. find the time complexity to search two elements x, y such that:-a) x+y < 100b) x+y 1000Also, state the algorithm/approach for th...
2 2 votes
1 answers 1 answer
2.0k
2.0k views
Akriti sood asked Jan 23, 2017
1,951 views
please tell the time complexity?i was getting O(2n)
1 1 vote
0 0 answers
1.4k
1.4k views
Akriti sood asked Dec 22, 2016
1,386 views
Consider the following functionint foo(int n) { int count1=0, count2=0; if (n < 0) n = -n; if (n == 0) return 1; If (n == 1) return 0; while (n) { if (n & 1) count1++; n ...
1 1 vote
2 answers 2 answers
1.9k
1.9k views
sh!va asked Dec 4, 2016
1,854 views
for (int i = 1; i <=m; i += c){ -do something -}for (int i = 1; i <=n; i += c){ -do something - }What will the the tiem complexity of given code pseudococde?A. O (m...