Please Explain Compute the worst case time complexity of the
Please Explain.
Compute the worst case time complexity of the following algorithm. for i = n to 2n do for j = i - n to n do print (i, j).Solution
for i=n to 2n do
for j=i-n to n do
print (i, j)
Here we have two loops:
1. outer for loop: runs for i=n to 2n : n times
2. inner for loop:
for each value of i, it run :
0 to n
1 to n
2 to n
.......
.......
n-1 to n
n to n
So, T(n): n + (n-1) + (n-2)+ .......+1
= n(n+1)/2
Big-O : O(n^2)
