1

I have a loop that runs n-2 times, what would be the time complexity in this case.

for(int m=1; m<arr.length-1; m++) {

}

I am not convinced for it to be O(n) because it will never run n times, not even in worst case scenarios.

derek
  • 59
  • 2
  • 10

1 Answers1

1

O(n) just means "on the order of n". Specifically, the definition is that some function f(n) is O(g(n)) if there exist some k and c such that for all n > k, f(n) < c * g(n). In this case, set f(n) = n - 2 and g(n) = n, you can see that for k = 10 and c = 2, n < 2 * (n - 2) for all n > 10, so n - 2 is indeed O(n).

The Zach Man
  • 738
  • 5
  • 15