1 votes 1 votes What will be TC here? Ans given $O(n^{2})$ , while I am getting $O(n)$ Algorithms time-complexity algorithms test-series + – srestha asked Aug 17, 2018 • retagged Jul 18, 2022 by makhdoom ghaya srestha 1.1k views answer comment Share Follow See all 13 Comments See all 13 13 Comments reply Show 10 previous comments Rishav Kumar Singh commented Aug 18, 2018 reply Follow Share Shubhgupta yes corrected ,thank you 1 votes 1 votes srestha commented Aug 18, 2018 reply Follow Share ok, but 1st case j is running less time right? that means in 1st case actual running time will be $1+2+3+....+n=\text{almost equal to}\frac{n(n+1)}{2}=O(n^{2})$ And in 2nd case actual running time will be $n+n+.....+n=n(1+1+1+1....+1)=n^{2}=\text{exact equal to}O(n^{2})$ right now? 1 votes 1 votes Rishav Kumar Singh commented Aug 18, 2018 reply Follow Share thats true, srestha mam $x^{2} > \frac{x^{2} + x}{2}$ 2 votes 2 votes Please log in or register to add a comment.
Best answer 3 votes 3 votes yes it will be o(n2) when i=0 j=0 so zero times when i=1 j=1,0 but only execute for j=1 so one time when i=2 j=2,1,0 but j will run for j=2,1 so 2 times .........................................similarly when i=n-1 j will execute n-1 times so total =0+1+2+3+...................n-1 =O(n2) eyeamgj answered Aug 18, 2018 • selected Aug 18, 2018 by srestha eyeamgj comment Share Follow See all 0 reply Please log in or register to add a comment.
2 votes 2 votes in this problem find how many times for loop execute i=1, j=1 i=2,j=2 ,,,,,,,, i=n-1, j=n-1 Total Complexity = 0 + 1 + 2............. + n-1 = n(n-1)/2 = O(n^2) is correct answer Hradesh patel answered Aug 18, 2018 Hradesh patel comment Share Follow See all 0 reply Please log in or register to add a comment.