Big O: Constants don’t matter
23 April 2023 (Updated 23 April 2023)
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²)
Tagged:
Big O Notation
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment