edited by
2,047 views

2 Answers

Best answer
2 votes
2 votes

values after execution of while loop

1st time=i=1

2nd time=i=1+2

3rd       i=1+2+3

4th       i=1+2+3+4

let for ith time i=(1+2+3+......i)<n

                      =i(i+1)/2  <n

                     =i^2<n

                     i=sqrt(n)

therefore time complexity=O(sqrt  n)

edited by
0 votes
0 votes
Lets assume while loop runs x times. then time complexity is O(x) (because code inside while is O(1))

In the last iteration i = 0+1+2+3+4 ......... + x;

                              i = $\frac{x(x+1)}{2}$

           now i < n and so $x^{2} + x < 2n$ time complexity is $O(\sqrt{n})$

Related questions

1.6k
views
2 answers
0 votes
radha gogia asked Jul 7, 2018
1,641 views
foo(int n) { for(int i=0 ; i<n ;i++) for(int j=i ; j<=i*i ;j++) if(j%i==0) { for(int k=0;k<j;k++) printf("hii"); } } How to proceed here for analyzing the time complexity ?
840
views
2 answers
2 votes
radha gogia asked Jun 28, 2018
840 views
For the below code : foo() { for(i=1;i<=n;i++) { for(j=0;j<i;j++) { for(k=0;k<j;k++) c++; } } } Here k is executing j-1 ... this is getting too large, I checked with an example, it is not matching,So please correct where I went wrong.
1.2k
views
1 answers
3 votes
Sanjay Sharma asked Feb 20, 2018
1,227 views
An array A is of length n has log( n) distinct numbers.What is the time complexity of sorting A by best comparison based algorithm ?
1.5k
views
1 answers
1 votes
Akriti sood asked Dec 22, 2016
1,525 views
What is the worst case time complexity of the following recurrence relation?T(n)=T(n/2)+T(n/4)+T(n/8)+n Θ(nlogn) Θ(n2) Θ(n)--------------- ... in this approach..??by tree method,i will get O(n).but whats wrong with this?please clear ths.