Have a look at this question and answers Still sort of confused about Big O notation it may help you sort out some of the confusion between worst case, best case, Big O, Big Omega, etc.
As to your specific problem, you are asked to find some (asymptotical) lower bound of the time complexity of the algorithm (you should also clarify whether you mean Big Omega or small omega).
Otherwise you are right, this problem is quite simple. You just have to think about how many times print (k)
will be executed for a given n
.
You can see that the first loop goes n-times. The second one also n-times.
For the third one, you see that if i = 1, then j = n-1, thus k is 1 to n-1-1 = n-2, which makes me think whether your example is correct and if there should be i-j
instead.
In any case, the third loop will execute at least n/2 times. It is n/2
because you subtract j-i
and j
decreases while i
increases so when they are n/2
the result will be 0
and then it will be negative at which point the innermost loop will not execute anymore.
Therefore print (k)
will execute n*n*n/2
-times which is Omega(n^3)
which you can easily verify from definition.
Just beware, as @Yves points out in his answer, that this is all assuming that print(k)
is done in constant time which is probably what was meant in your exercise.
In the real world, it wouldn't be true because printing the number also takes time and the time increases with log(n)
if print(k)
prints in base 2 or base 10 (it would be n
for base 1).
Otherwise, in this case the best case is the same as the worst case. There is just one input of size n
so you cannot find "some best instance" of size n
and "worst instance" of size n
. There is just instance of size n
.