sajad torkamani

In a nutshell

An algorithm is considered O(n) when its complexity (i.,e., number of operations) is directly proportional to the input size.

export function printNumbersUpTo(n: number): void {
  for (let i = 1; i <= n; i++) {
    console.log(i);
  }
}

printNumbersUpTo(10)
printNumbersUpTo(100)

Here we have a single for loop that will run n times. If you increase n by 1, you increase the number of loop operations by 1. If you increase it by 10, you increase the loop operations by 10. The number of operations is roughly proportionate to n.

Leave a comment

Your email address will not be published. Required fields are marked *