Skip to content

Complexity Analysis

Course: Arrays - chapter 2

Generally there can be multiple ways to solve a problem. For example, in our last class we learn’t about finding intersections for multiple intervals / ranges. But we need a way to compare which one is better than the other. We can do this comparison using Complexity Analysis.

Generally, we compare 2 things:

  1. Time Complexity
  2. Space Complexity

Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input

t(n) = n^2 + 2n + 6

Here t(n), is a function that returns the time taken by the algorithm for an input of length n.

💡 For simplicity, we have used a single parameter. It can be a function of multiple parameters as well.

Space complexity of an algorithm quantifies the amount of space or memory taken by an algorithm to run as a function of the length of the input.

t(n) = n^2

Here t(n), is a function that returns the additional space used by the algorithm for an input of length n.

Note: Generally by space complexity, we mean additional space. The input can be an array of length n, but generally we do not consider that while computing space complexity