Junior — Middle
Can you explain what the time complexity of an algorithm equal to O(n) means?
sobes.tech AI
Answer from AI
The algorithm's time complexity of O(n) means that the execution time grows linearly with the increase in input data size n. If the number of elements doubles, the execution time roughly doubles.
For example, if you have a loop that goes through all elements of an array of n elements, this is a typical case of O(n):
void PrintElements(int[] arr) {
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i]);
}
}
Here, the execution time directly depends on the number of elements in the array.