14 14 votes What is the complexity of the following code? sum=0; for(i=1;i<=n;i*=2) for(j=1;j<=n;j++) sum++; Which of the following is not a valid string? $O(n^2)$ $O(n\log\ n)$ $O(n)$ $O(n\log\ n\log\ n)$ Algorithms isro-2020 algorithms time-complexity normal + – Satbir 19.5k views answer comment Share Follow Print See all 9 Comments 9 9 Comments reply Show 6 previous comments swettt871 commented Jan 19, 2021 reply Follow flag Option B is right here because outer loop run logn times(1,2,4,8,…...)so logn and inner loop runs n times corresponding to each value of i so TC:nlogn 1 1 replyShare Vijay Devagonda commented Dec 26, 2023 reply Follow flag Atleast read question please 🙏 1 1 replyShare EagerLearner commented 5 days ago reply Follow flag Could have formed it like "Which of these is not a valid time complexity/upper bound for this code" 0 0 replyShare Please log in or register to add a comment.
14 14 votes $\underline{\textbf{Answer:}\Rightarrow}\;\mathbf{c.}$ Outer loop runs $\mathbf{\log n}$ times and inner loop runs $\mathbf{n}$ times. $\therefore $ The time complexity will be: $\mathbf{O(n\log n)}$ They asked for the invalid string. So, it will be $\mathbf{O(n)}$ $\underline{\underline{\color{red}{\textbf{Note:}}}}$ Question was $\color{green}{\textbf{excluded}}$ from the evaluation due to ambiguity. `JEET answered Jan 13, 2020 • edited Sep 28, 2022 by `JEET `JEET comment Share Follow See all 5 Comments 5 5 Comments reply Show 2 previous comments `JEET commented Jan 13, 2020 reply Follow flag Yes $\mathbf{O(n)}$ is correct. 0 0 replyShare smsubham commented Jan 18, 2020 reply Follow flag What is meaning of invalid string here? 3 3 replyShare Ram Swaroop commented Feb 10, 2020 reply Follow flag @jeet what is the meaning of invalid string here I am not getting 1 1 replyShare Please log in or register to add a comment.
2 2 votes The first loop runs for "log" time. Second loop runs for "nahi" time. Overall running complexity (as loop is inside the loop ,so their run time complexity gets multiplied )= O(nlogn). So, the desired complexity is O(nlogn) , therefore the run time complexity can be greater than this but can never be lesser than this. But according to question, Options A,B and D are correct and C is wrong . (we've to find out the wrong option ). Hence correct answer is C ShubhamKumar7352 answered Dec 20, 2022 ShubhamKumar7352 comment Share Follow See 1 comment 1 1 comment reply Anurag_Gautam 1 commented Oct 30, 2025 reply Follow flag yes this is the actual way to getting odd one out option 0 0 replyShare Please log in or register to add a comment.
1 1 vote Option C is correct here checking all the options heisenberggg answered Apr 6, 2021 heisenberggg comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Outer Loop : i=1*2 :2 i=2*2 : 2^2 i=2^2*2 : 2^3 i=2^k and outer loop terminates if i>n substituting i=2^k 2^k>n k=logn and inner loop runs for n times giving overall timecomplexity as O(nlogn) Rohit ._. answered Oct 31, 2025 Rohit ._. comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Correct answer =>C Time complexity Tight bound = Θ(nlogn) option A) nlogn <= c.n^2 (valid for large n) option B ) nlogn <= c.nlogn (valid tight bound equal) option C) nlogn <=c.n (invalid as nlogn >n) option D) nlogn ,<= nlognlogn (valid for large n) Ashwani 1 answered May 30 Ashwani 1 comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes the correct option was c O(n) we get the time complexity was nlogn and we have to compare with the remaining option ,if we take the frst option was n^2 nlogn<=n^2 nlogn <= n*n // if cancled the one n from the right side logn <=n we know that n was greater when compare to the logn in the same way we have to compare to the option c nlogn <=n nlogn<=c*n // c is the constant if we cancled the n from the right side we having the constant we are know that "logn" is greater than the c but in the c option they was given the O(n) so that option c was invalid Ramsetti_suvek_Suvek answered Jul 17 Ramsetti_suvek_Suvek comment Share Follow 0 reply Please log in or register to add a comment.