- Related Questions & Answers
- How to format date and time in android?
- HTML DOM console.time() Method
- How to get current date and time from internet in iOS?
- How to request Location permission at run time in Android?
- How to get current Time in Java 8?
- How to write date and time changer Listener in Android?
- What is a Fourier Analysis?
- Amortized Complexity
- How to get file last modified time in Java?
- Asymptotic Complexity
- How to launch activity only once when Android app is opened for the first time?
- How do I display the current date and time in an Android application?
- How to get current date and time from internet in android?
- Print system time in C++ (3 different ways)
- How to display the date and time of a document when it is last modified in JavaScript?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

**Time complexity** of any algorithm is the time taken by the algorithm to complete. It is an important matrix to show the efficiency of the algorithm and for comparative analysis. We tend to reduce the time complexity of algorithm that makes it more effective.

Find the time complexity of the following code snippets

for(i= n ; i > 0; i++){ cout<<i; i++; }

The loop has maximum value n but the i will be incremented twice in the for loop which will make the time take half.

**Time complexity: O(n/2)**

for(i= n ; i > 0; i++){ for(j = 0; j<n;j++){ cout<<i; } }

Here it is evidant that for each iteation of outer loop, inner loop will be executed n times.

**So, Time Complexity: O(n ^{2}) or O(N*N)**

int i = N; while(i){ cout<<i; i = i/2 ; }

Here, after every iteration value of i will be divided by 2. So, **time complexity**: O(log(n))

if(i > j ){ j>23 ? cout<<j : cout<<i; }

There are two conditional statements in the code. Each conditional statement has time complexity = O(1).

Time complexity : O(2)

for(i= n ; i > 0; i++){ for(j = 0; j<n;j*2){ cout<<i; } }

Here, we have nested loops and the inner loop has multiplication as increment operator. So, the complexity of the inner loop will be of the order log(n). And have of outer will be of the order n. So,

**Time complexity:** O(nlog(n))

Advertisements