71 71 votes Consider the following C function int fun(int n) { int i, j; for(i=1; i<=n; i++) { for (j=1; j<n; j+=i) { printf("%d %d", i, j); } } } Time complexity of $fun$ in terms of $\Theta$ notation is $\Theta(n \sqrt{n})$ $\Theta(n^2)$ $\Theta(n \: \log n)$ $\Theta(n^2 \log n)$ Algorithms gatecse-2017-set2 algorithms time-complexity + – Madhav 41.1k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
Best answer 75 75 votes Inner for loop is dependent on $i$, so for each $i$ we have to check no of times inner loop operating.. It ll be something like $\frac{n-1}{1}+\frac{n-1}{2}+\frac{n-1}{3}+...........+\frac{n-1}{n-1}+1$ $\frac{n}{1}+\frac{n}{2}+\frac{n}{3}+......+\frac{n}{n-1}-\log(n-1)$ $n\{{\frac{1}{1}+\frac{1}{2}+\frac{1}{3}+.....+\frac{1}{n-1}}\} - \log(n-1)$ $n\log(n-1)-\log(n-1)$ $n\log(n-1)$ $n\log n$ Correct Answer: $C$ 2018 answered Feb 14, 2017 • edited Apr 29, 2019 by Naveen Kumar 3 2018 comment Share Follow See all 5 Comments 5 5 Comments reply Show 2 previous comments Amjad. commented Apr 21, 2025 reply Follow flag anyone please explain how : 1/1+1/2+1/3+......+1/n becomes logn thank you! 1 1 replyShare Rohit Gupta 62 commented Nov 10, 2025 reply Follow flag Integration of 1/x is logx .... 4 4 replyShare cprdereddyy commented Jan 29 reply Follow flag I dont think Integral works for discrete things ........ 0 0 replyShare Please log in or register to add a comment.
158 158 votes $i=1 \rightarrow j : 1 ..2..3..4..5..6..7..8..9..10......approx (n times)$ $i=2 \rightarrow j : 1 ..3..5..7..9..11..13.................approx (n/2 times)$ $i=3 \rightarrow j : 1 ..4..7..10..13..14..17..............approx (n/3 times)$ $T(n)= n + n/2 + (n/3) + (n/4)+ (n/5) + (n/6)......... =\Theta (nlogn)$ {best option} erh answered Feb 14, 2017 erh comment Share Follow See all 11 Comments 11 11 Comments reply Puja Mishra commented Nov 12, 2017 reply Follow flag nice answer ... 2 2 replyShare Ajit J commented Apr 2, 2018 reply Follow flag why can't the answer be D? I'll first find the complexity of the inner loop as nlogn, and then multiply it with the complexity of the outer loop i.e n, and the answer turns out to be D? 2 2 replyShare Shamim Ahmed commented Nov 21, 2018 reply Follow flag I think as we are calculating the complexity of the inner loop using the values of i, we are already taking outer loop under consideration. This is the reason the answer is nlogn but not n^2lgn. 3 3 replyShare divyan commented Dec 9, 2018 reply Follow flag How 1 + 1/2 + 1/3 + 1/4 +1/5 + .................. + 1/n being logn?? can anybody explain please? 0 0 replyShare Shamim Ahmed commented Dec 10, 2018 reply Follow flag We can do this using integration :- 16 16 replyShare divyan commented Dec 10, 2018 reply Follow flag Integration is for continuous function. When you are integrating it from 1 to n then you are taking every point between 1 and n e.g. 1.01,1.02 and infinite points. But here we have only 1,2,3,4,......... n 4 4 replyShare Shamim Ahmed commented Dec 10, 2018 reply Follow flag Its a summation of n term harmonic series :- Refer:- https://en.wikipedia.org/wiki/Harmonic_number 0 0 replyShare `JEET commented Dec 17, 2019 reply Follow flag The outer loop has no use here except calculating the value of the inner loop. 1 1 replyShare dr_Jackal commented Jan 7, 2020 reply Follow flag how this summation become log n, source : https://www.quora.com/What-is-the-sum-of-the-series-1+-1-2-+-1-3-+-1-4-+-1-5-up-to-infinity-How-can-it-be-calculated Edit : solved : its not approaching infinite rather going to n 0 0 replyShare Abhineet Singh commented Nov 13, 2020 reply Follow flag here inner loop increment condition depends on the outer loop, so you can’t calculate them individually as you do when they are independent. Here you have to unravel the loop to find the time complexity. 0 0 replyShare Akashsr3 commented May 21 reply Follow flag @Divyan this is harmonic GP sum which is logn 0 0 replyShare Please log in or register to add a comment.
19 19 votes Ans) c First time inner loop run -----n times 2nd time '' '' ''----n/2 times Then,n/3,n/4....till n/n=1 time Total=n+n/2+n/3+n/4+n/5+......+n/n=n(1+1/2+1/3+...+1/n)=⊝(nlogn) (approx) jatin saini answered Feb 14, 2017 jatin saini comment Share Follow 0 reply Please log in or register to add a comment.
12 12 votes for i=1;j will run=1to n=n tym i=2;j will run =1ton=n/2 tyms i=3;j will run 1to n=n/3 tyms i=4;j will run 1 to n=n/4 tyms ...so on i=n; j will run 1to n=1 tym; therefore j will run in total=(n+n/2+n/3+n/4+..........1)=n(1+1/2+1/3+1/4.......)=nθ(log n)=θ(nlogn) option c Shashank Kumar Mishr answered Mar 7, 2017 Shashank Kumar Mishr comment Share Follow See 1 comment 1 1 comment reply Puja Mishra commented Jan 11, 2018 reply Follow flag U hav described same as other answers !!!! 2 2 replyShare Please log in or register to add a comment.
4 4 votes Consider for inner for loop : If the loop would have been: for( j=1; j<n ; j= j+2) Then the loop would have iterated n/2 times So now in place of '2' we have 'i' : So my loop will iterate n/i times. So this will vary with value of and we will get a series of : n + n/2 + n/3 + n/4........ => n( 1 + 1/2 + 1/3.....) => nlogn TheAnteamatter answered Jun 17, 2020 TheAnteamatter comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote The first loop is independent, so it takes only n times, but the second loop is dependent on outer loop.so it takes o(logN) so it takes o(NlogN). opt c is correct keshore muralidharan answered Aug 21, 2020 keshore muralidharan comment Share Follow 0 reply Please log in or register to add a comment.