retagged by
1,054 views
1 votes
1 votes

What will be TC here?

Ans given $O(n^{2})$ , while I am getting $O(n)$

retagged by

2 Answers

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)

 

selected by
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

Related questions

0 votes
0 votes
2 answers
1
Gate Fever asked Sep 27, 2018
1,987 views
0 votes
0 votes
1 answer
2
0 votes
0 votes
3 answers
3
1 votes
1 votes
2 answers
4
HeadShot asked Aug 8, 2018
1,570 views