Reputation: 90
What will be the time complexity (Big O only) of the following code?
int n = 0;
for(int i = 0; i < 5; i++)
n+=1;
Will it be O(1) since the statements in it are executed a fixed number of times?
Upvotes: 1
Views: 217
Reputation: 14660
Will it be O(1) since the statements in it are executed a fixed number of times?
Big O notation is used to describe a relationship between a size of the input and the number of operations necessary to compute a result from that input. In your case, it's not clear what your input is, and hence it's not clear in relation to what input you compute the result.
If we assume that your input is n
(first line in your code), then it's O(1) because the number computed in the for
loop doesn't depend on the input.
Upvotes: 1