Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler Events New
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER
Events
New

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.

## 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

Problem Score Companies Time Status
Collatz Conjecture 200 23:08
Palindromic Time 200 40:08
Pangram Check 100 27:15
Climbing Stairs 150 29:08
Integers in Strings 100 20:49
Word Count 150 23:13
Extracting Numbers 100 24:50
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *