5 Time & Space Complexity 1.1!03!08 2024
5 Time & Space Complexity 1.1!03!08 2024
URL: https://forms.gle/LsD6CnjK5gaVNHg98
QR CODE:
TIME AND SPACE
COMPLEXITY
TOPICS
a function of the input size 'n'. Time complexity is denoted using Big O notation,
which provides an upper bound on the growth rate of the algorithm's running
as a function of the input size 'n'. Space complexity is also denoted using Big O
notation, and it represents the upper bound on the additional memory space
They are 3 asymptotic notations are mostly used to represent the time complexity
of the algorithm.
1.Big oh (O)notation
3.Theta(Θ) notation
Big-O notation
Big-O notation represents the upper bound of the running time of an algorithm.
The above expression can be described as a function f(n) belongs to the set O(g(n)) if there
exists a positive constant c such that it lies between 0 and cg(n), for sufficiently large n.
For any value of n, the running time of an algorithm does not cross the time provided by
O(g(n)).
Big-O notation
Omega notation represents the lower bound of the running time of an algorithm.
The above expression can be described as a function f(n) belongs to the set Ω(g(n)) if
there exists a positive constant c such that it lies above cg(n), for sufficiently large n.
For any value of n, the minimum time required by the algorithm is given by Omega
Ω(g(n)).
Theta Notation (Θ-notation)
Theta notation encloses the function from above and below. Since it represents
the upper and the lower bound of the running time of an algorithm, it is used for
The above expression can be described as a function f(n) belongs to the set Θ(g(n)) if there exist
positive constants c1 and c2 such that it can be sandwiched between c1g(n) and c2g(n), for
sufficiently large n.
Theta Notation (Θ-notation)
If a function f(n) lies anywhere in between c1g(n) and c2g(n) for all n ≥ n0, then f(n) is
Answer: The time complexity of this code is O(n^2) since it uses nested loops to print all
Answer: You can improve the time complexity to O(n) by using a different approach.
Instead of nested loops, you can use a single loop and a HashSet to keep track of visited
elements.
Interview questions
3. Explain the difference between the time complexities of linear search and binary search
algorithms.
Answer: Linear search has a time complexity of O(n) since it checks each element in the
list one by one until the target element is found or the end of the list is reached. Binary
search, on the other hand, has a time complexity of O(log n) as it halves the search space
Answer: Big-O notation represents the upper bound of an algorithm's running time and
gives the worst-case complexity. It helps us understand how the algorithm's performance
Answer: Omega notation represents the lower bound of an algorithm's running time and
provides the best-case complexity. It gives us the minimum time required by the algorithm
for any input size.
THANK
YOU