sajad torkamani

In a nutshell

Consider this function:

function countUpAndDown(n: number) {
  console.log("Going up!");
  for (let i = 0; i < n; i++) {
    console.log(i);
  }
  console.log("At the top!\nGoing down...");
  for (let j = n - 1; j >= 0; j--) {
    console.log(j);
  }
  console.log("Back down. Bye!");
}

countUpAndDown(10)

Even though technically the Big O here is O(2n) (we loop n times twice), we simplify the Big O to just O(n). We drop the constant before n.

Here are some examples of dropping constants:

  • O(2n) -> O(n)
  • O(100) -> O(1)
  • O(14n²) -> O(n²)

Leave a comment

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