Time Complexity

Go to Problems

How to Calculate Time Complexity?

Some General Rules for Calculating Time Complexity

In this section we are going to see some general rules for calculating the time complexity. Sometimes calculating time complexity becomes very complicated , to avoid this, these rules are going to come handy.

A very important thing to keep in mind is that we always assume that the input size is very large.

Rule 1: Drop constant multipliers.

Eg: 

Given T(n) = 3n3 , find worst case running time

Using the above rule, we can drop the constant term.

Worst case running time = O(n3)

Rule 2 : Drop lower order terms.

Eg:

Given T(n) = 3n2 + 2nlogn + 5n+4 , find worst case running time.
We know that 3n2 is the highest order term. Therefore we can drop the rest of the terms .
Worst case running time = O(3n2)

We can further reduce this using rule 1.

∴ Worst case running time = O(n2 )

Rule 3: Running time of a program=  Running time of all fragments

Eg: Calculate the running time of the following program.

for (int i=0;i<n;i++)
{
    int a=5;
    a++;
}


for (int i=0;i<n;i++)
{
   for(int j=0;j<n;j++)
   {
   int b=5;
   b++;
   }
}

First of all we can break the program into 2 fragments. The first fragment is the first loop and the second fragment is the second loop (nested loop). Our final answer would be the sum of both fragments.

As we know running time for simple statements like declaration, assignment, arithmetic/logical operations is O(1).

Therefore the time complexity of the first fragment would be O(n), as the loop would run n times, the time complexity of the statements inside the loop is O(1).

Whereas the time complexity of the second fragment would be O(n2).
Time complexity of the program= O(n) + O(n2)
= O(n2)

Serious about Learning Programming ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum which covers Data Structures & Algorithms in depth.

This topic has only Multiple Choice Questions

Jump to subsequent topics to solve code problems.

Time Complexity Problems

Basic primer
Problem Score Companies Time Status
LOOP_CMPL 20 2:43
NESTED_CMPL 20
1:10
NESTED_CMPL2 30
1:25
CHOOSE4 50
0:57
Math
Problem Score Companies Time Status
WHILE_CMPL 50
1:31
NESTED_CMPL3 80 3:56
LOOP_CMPL2 80
2:43
GCD_CMPL 150
4:13
Compare functions
Problem Score Companies Time Status
CHOOSE3 50
1:39
CHOOSE1 50
1:43
CHOOSE2 80
2:23
Function calling itself
Problem Score Companies Time Status
REC_CMPL1 80 6:58
REC_CMPL2 80
6:25
REC_CMPL3 150
4:39
Amortized complexity
Problem Score Companies Time Status
AMORTIZED1 100
3:03
lock
Topic Bonus
Bonus will be unlocked after solving min. 1 problem from each bucket