What Time Complexity Means
Time complexity describes how the number of operations grows as input size increases. It does not measure seconds directly, but how the algorithm scales.
Why a Loop Can Be O(n)
If a loop visits every element once, the number of operations grows linearly with the input size. That is why it is called O(n).
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}Why Binary Search Is O(log n)
Binary search repeatedly divides the search space by two. Because the input is reduced by half each step, the complexity becomes O(log n).
Space Complexity
Space complexity measures additional memory used by an algorithm. Recursion, arrays, maps, and dynamic programming tables can increase space usage.
