4 4 votes //n is a prime number here int main() { for(i=1;i<=n;i=2*i) { for(j=1;j<=n;j++) { if(n%i==0) { k=1; while(k<=n) { a=b+c; k=k+1; } } } } } Algorithms time-complexity algorithms + – junaid ahmad 2.2k views answer comment Share Follow Print See all 15 Comments 15 15 Comments reply vivek yadhuvanshi commented Oct 28, 2017 reply Follow flag i think answer should be n^3 0 0 replyShare Hemant Parihar commented Oct 28, 2017 reply Follow flag For i = 1 only, the statement if (n % i = 0) is true, for rest of the value of i, this statement is not true. as n is prime. for i = 1 $\Rightarrow$ $n^{2}$ inner loop execute. for i = 2 $\Rightarrow$ n times only. for i = 4 $\Rightarrow$ n times. for i = 8 $\Rightarrow$ n times. .... .... for i = n $\Rightarrow$ n times. T(n) = $n^{2}$ + n + n + n .... (log n) times n. T(n) = $n^{2}$ + O(n(log n)). T(n) = O($n^{2}$) 5 5 replyShare rahul sharma 5 commented Oct 28, 2017 reply Follow flag First loop:- Logn times Second loop:- n times Third loop will run only 2 times i.e for 1 and n as prime number has only two factors. So nlogn+n should be time complexity 0 0 replyShare Rupendra Choudhary commented Oct 28, 2017 reply Follow flag Case 1 ) n is some odd number in that case if block will be able to execute when i=1 , otherwise it will always fail to execute as odd%even is always not zero value. so in that case we can analyze , TC as Hemant did. and output will be O(n2). case 2 ) n is some even value like suppose n=32 i=1 , j=1->n , k=1->n : n2 times i=2 , j=1->n , k=1->n : n2 times i=4 ,j=1->n , k=1->n : n2 times i=8 ,j=1->n , k=1->n : n2 times i=16 , j=1->n , k=1->n : n2 times i=32 , j=1->n , k=1->n : n2 times so total n2logn times... => O(n2logn) 0 0 replyShare Anu007 commented Oct 28, 2017 reply Follow flag i never be equal to n . since n is prime number . @hemant sir 0 0 replyShare Shubhanshu commented Oct 28, 2017 reply Follow flag TC = O(n2 log n) . 1 1 replyShare joshi_nitish commented Oct 28, 2017 reply Follow flag it will be O(n2) for i=1 and i=n, it is running O(n2) times for i = 2 , 4 , 8, 16, ......2log(n-1) each, it is running O(n) times overall time complexity = O(n2) + O(n2) + O(nlogn) = O(n2) 3 3 replyShare Habibkhan commented Oct 28, 2017 reply Follow flag Change ur comment to answer @Hemant Parihar.. 0 0 replyShare Shivam Chauhan commented Oct 29, 2017 i edited by Shivam Chauhan Nov 15, 2017 reply Follow flag i incements as 1, 2, 4, 8, 16 ... so we can observe that first loop executes till $\left \lceil logn \right \rceil$ (final value) Prime number gets divided by 1 and itself. Variable i ecounters only i = 1 so statement if (n % i == 0) will only be true when i = 1 for (i = 1; i <= n; i = 2*i) This loop will be executed log n times for (j = 1; j <= n; j++) This loop will be executed n * (log n) times if (n % i == 0) This statement will be executed n * (log n) times k = 1; This statement will be executed n times while (k <= n) This statement will be executed n2 times a = b + c; This statement will be executed n2 times k = k + 1; This statement will be executed n2 times Time complexity = O(n2) 0 0 replyShare Rupendra Choudhary commented Oct 29, 2017 reply Follow flag we're talking about the worst case TC about this code segment , then won't the worst case would be when n=2k and then as i'm abe to see TC would be n2logn . see my comment and please tell me am i going wrong somewhere? 0 0 replyShare Shivam Chauhan commented Oct 29, 2017 reply Follow flag n is a prime number. How will it be 2k 0 0 replyShare Rupendra Choudhary commented Oct 29, 2017 reply Follow flag oops ! and i noticed it now. Thanks brother. 0 0 replyShare Nitesh Choudhary commented Oct 30, 2017 reply Follow flag sir i think answer should b O(nlogn) because first for loop run lgn(n) times for second for loop j=1 while loop n times j=2 1 time because n%2==0 is false j=3 1 time because n%2==0 is false so............. j=n n times n%n==0 true so total second loop=n+1+1+1.............+n=n-2+2n and first loop log(n) times so O((3n-2)log(n))=O(nlog(n)) 0 0 replyShare rahul sharma 5 commented Nov 13, 2017 reply Follow flag @Hemant Parihar ,when i =n then it will run n^2 . 0 0 replyShare Hemant Parihar commented Nov 14, 2017 reply Follow flag @rahul n is a prime number here. And i is getting double in each iteration. It never divide n properly when n > 2. 0 0 replyShare Please log in or register to add a comment.