Ki_Ki
Ki_Ki

Reputation: 21

confusion regarding the time of complexity of constat array size

I researched why the following code is still O(N), even if we put the "const" keyword before the input array size.

const int array_size = 10 
for (int i= 0 ; i < array_size; i++)
{
  ...
}

Why the complexity time is still O(N)?

Upvotes: 0

Views: 71

Answers (1)

David G
David G

Reputation: 96800

What time complexity refers to is the order of growth of a function as the input size increases. It has nothing to do with language-specific features like the const keyword.

In your example the time complexity is constant, or O(1), because the number of iterations is not dependent on the size of some input. The const keyword is a keyword in the C++ language that means that the variable itself can't be changed, but time complexity is a general computer science concept. Whether or not array_size is const, the number of iterations is the same.

Upvotes: 3

Related Questions