Newlearner826
Newlearner826

Reputation: 39

Time complexity of for loop with number of iterations equal array spesific index

I'm new to calculating complexity so this is confusing me. If my code get an unknown length (n) array of integers, suppose that the array (arr) is sorted already and I want for loop like this:

for (int k=0; k<arr[n-1]; k++);

Does this code complexity is O(1) or O(n)?

I'm used to calculate complexity depend on itaration as function of length but here it depend on the data inside the array.

Upvotes: 1

Views: 81

Answers (1)

Ali Haider
Ali Haider

Reputation: 388

The time complexity of your code only depends on the value of the element at index n-1. Suppose that element in your array has the value m. Then you can consider the time complexity of your code to be O(m).

Upvotes: 1

Related Questions