Introduction to Big O
Best Time Complexity
The best complexity possible is O(1), called "constant time" or "constant space". It means that the algorithm ALWAYS uses the same amount of resources, regardless of the input.
Space-complexity (of an algorithm)
The space complexity of an algorithm is the amount of memory allocated by the algorithm when run relative to the input size.(How much memory an algorithm needs).
The most common variable you'll see is n in Big O notation
usually denotes the length of an input array or string.
When talking about complexity of an algorithm, there are normally three cases:
- Best case scenario - Average case scenario - Worst case scenario **In most algorithms, all three of these will be equal, but some algorithms will have them differ. If you have to choose only one to represent the algorithm's time or space complexity, never choose the best case scenario. It is most correct to use the worst case scenario, but you should be able to talk about the difference between the cases.**
complexities
- O(n) - O(n2) - O(2n) - O(log n) - O(n⋅m) (In the context of interviews, there are some common assumptions that we make. For example: when dealing with integers, the larger the value, the more time operations like addition, multiplication, or printing will take. While this is relevant in theory, we typically ignore this fact because the difference is practically very small. If you are given an array of integers as an input, the only variable you would use is n to denote the length of the array. )
Logarithmic Time
A logarithm is the inverse operation to exponents. The time complexity O(logn) is called logarithmic time and is extremely fast. A common time complexity is O(n⋅logn), which is reasonably fast for most problems and also the time complexity of efficient sorting algorithms. Typically, the base of the logarithm will be 2. This means that if your input is size n, then the algorithm will perform x operations, where 2x=n. However, the base of the logarithm doesn't actually matter for big O, since all logarithms are related by a constant factor.
What is the time complexity?: // Given an integer array "arr" with length n, for (int num: arr) { print(num) }
It's O(n). In each for loop iteration, we are performing a print, which costs O(1). The for loop iterates n times, which gives a time complexity of O(1⋅n) = O(n).
Big O Notation
a notation used to describe the computational complexity of an algorithm. The computational complexity of an algorithm is split into two parts: time complexity and space complexity.
Time-complexity (of an algorithm)
The time complexity of an algorithm is the amount of time the algorithm needs to run relative to the input size.
What is the time complexity?: // Given an integer array "arr" with length n, for (int num: arr) { for (int i = 0; i < 500,000; i++) { print(num) } }
This algorithm has a time complexity of O(n). In each inner for loop iteration, we are performing a print, which costs O(1). This for loop iterates 500,000 times, which means each outer for loop iteration costs O(500000)=O(1). The outer for loop iterates n times, which gives a time complexity of O(n).
What is the time complexity?: // Given integer arrays "arr" with length n and "arr2" with length m, for (int num: arr) { print(num) } for (int num: arr) { print(num) } for (int num: arr2) { print(num) }
This algorithm has a time complexity of O(n+m). The first two for loops both cost O(n), whereas the final for loop costs O(m). This gives a time complexity of O(2n+m) = O(n+m).
What is the time complexity? // Given an integer array "arr" with length n, for (int num: arr) { for (int num2: arr) { print(num * num2) } }
This algorithm has a time complexity of O(n2). In each inner for loop iteration, we are performing a multiplication and print, which both cost O(1). The inner for loop runs n times, which means each outer for loop iteration costs O(n). The outer for loop runs O(n) times, which gives a time complexity of O(n⋅n)=O(n2).
Analyzing Space Complexity
When you initialize variables like arrays or strings, your algorithm is allocating memory. We never count the space used by the input (it is bad practice to modify the input), and usually don't count the space used by the output (the answer) unless an interviewer asks us to.