what is the time complexity T(n) of the following potion of the code? for simplicity you may assume that n is power of 2. that is n=2^k . for some positive integer k.
for(i=1;;I<=n;i++)
for(j=n/2;j<n;j++)
print x;
choose the correct answer:
- T(n)=n+1
- T(n)=n^2+2n
- T(n)=n
- T(n)=n^3
- T(n)=n(n/2 +1)
So lets write some iterations:
Now lets remember
1st
andnth
and calculate sum of ranges(end - start + 1
) fromi = 2
tilli = n-1
....
...
...
...
...
Last is arithmetic progression which is calculated by formula:
where:
So we end up with:
...
...
Now lets add 2 values that we remembered:
...
...
...
I am getting none of the above. I have checked and rechecked but can not find the error.