Table of contents
1.
Introduction
2.
Practice Questions on Time Complexity Analysis in C++
3.
Frequently Asked Questions
3.1.
What is time complexity analysis with an example?
3.2.
How many types of time complexity are there?
3.3.
Why is it important to know time complexity?
4.
Conclusion
Last Updated: Oct 10, 2024
Easy

Practice Questions on Time Complexity Analysis in C++

Author Malay Gain
6 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Time Complexity analysis in c++

Introduction

Besides solving coding problems, one should be aware of the time complexity of the implemented algorithm so that it can be optimized further if possible.

Time complexity is the running time of an algorithm. Big O-notation is used to express the number of primitive operations executed as a function of input size.

Any algorithm can be classified into some type based on its complexity. See the classes mentioned below.

Here are the special classes of algorithms:

  • Logarithmic: O(logn) 
  • Linear: O(n) 
  • Quadratic: O(n2
  • Polynomial: O(nk), k ≥ 1 
  • Exponential: O(an), a > 1
     

Also, have a look at the blog on Asymptotic Notations for a better understanding of the topic.

Also See, kth largest element in an array and Euclid GCD Algorithm

Practice Questions on Time Complexity Analysis in C++

Let us now practice some questions on time complexity analysis and get more familiar with the topic.

 

Q1: Analyze and find the time complexity of the below function from the code snippet.

for(int i=1;i<=n;i++){
    cout<<i<<endl;
}
for(int j=1;j<=m;j++){
    cout<<j<<endl;
}

 

Note: first, try to solve the problem and then see the below solution approach.

In the above code, the first loop will run times and the second loop will run times So, we can say the time complexity of the above code is O(n+m).

 

Q2: Analyze and find the time complexity of the below function from the code snippet.

int k=1;
while(k<=n){
	cout<<k<<endl;
	k=k*2;
}

 

Note: first, try to solve the problem and then see the below solution approach.

In the above code, the while loop will run logn times as k is multiplied by 2 for every iteration. So, we can say the time complexity of the above code is O(logn).

 

Q3: Analyze and find the time complexity of the below function from the code snippet.

for(int i=n/2;i<=n;i++){
	for(int j=1;j<=n;j=j*2){
   		cout<<i<<j<<endl;
	}
}

 

Note: first, try to solve the problem and then see the below solution approach.

In the above code, the outer loop will run (n/2) times, and for each value of i, the inner loop will run (logn) times. So, the total number of operations is n/2*logn. We can say the time complexity of the above code is O(n*logn ).

 

Q4: Analyze and find the time complexity of the below function from the code snippet.

if(i>j){
	j==0? j++ : j--;
}

 

Note: first, try to solve the problem and then see the solution below.

A Conditional statement takes O(1) time to execute. In the above code, there are only two conditional statements. So, it takes constant time to execute. Then we can conclude the time complexity of the above code is O(1).

 

Q5: Analyze and find the time complexity of the below function from the code snippet.

for (int i = 0; i < n; i++)
        for (int j = i; j > 0; j--)
            cout << i << j;

 

Note: first, try to solve the problem and then see the solution below.

In the above code, the outer loop will run (n) times, and for each value of i, the inner loop will run (i) times. So, the total number of operations is n*i, and the maximum value of i will be n, so the overall time complexity of the problem will be n O(n*n).

Read More - Time Complexity of Sorting Algorithms

Must Read Algorithm Types

Frequently Asked Questions

What is time complexity analysis with an example?

Time complexity is defined as the amount of time it takes an algorithm to run as a function of the input length. It calculates the time required to execute each code statement in an algorithm. It does not look at an algorithm's overall execution time. For example, an algorithm is said to have a linear time complexity when its execution time scales linearly with the input size.

How many types of time complexity are there?

There are several forms of time complexities, below are the most fundamental.
Constant Time Complexity: O (1)
Linear Time Complexity: O (n)
Logarithmic Time Complexity: O (log n)
Quadratic Time Complexity: O(n2
Exponential Time Complexity: O(2^n)

Why is it important to know time complexity?

Time complexity is required to determine the program's efficiency. The lesser the time complexity, the faster the execution.

Conclusion

This article has covered some interesting time complexity questions. We have shown the approach of time complexity analysis of a program.

You can refer to the Time Complexity Analysis video lecture by Ankush Singla, the co-founder of Coding Ninjas, and also visit the articles to elaborate on time complexity as well as space complexity.

Recommended Readings:

To learn more about Data Structures and Algorithms, you can enroll in our DSA in C++ Course.

Thankyou

Happy learning!

Live masterclass