SlideShare a Scribd company logo
1 of 19
Time Complexity
Srivaths P
Goal:
• Understand time complexity
• Understand Big-O notation for time complexity.
• Evaluate time complexity of an algorithm.
• Evaluate expected time complexity based on the given
constraints of a problem.
• Evaluating space complexity of a program.
• Common verdicts of submissions.
What is an Elementary Operation?
An operation that takes constant time is called
elementary operation.
Example:
• Arithmetic operations
• Comparison of primitive types
• Input and output of primitive types
108 operations ≈ 1 second
Quiz 1
1. Is the following an elementary operation?
2. Is the following an elementary operation?
What is Time Complexity?
Time complexity is a function to describe the
approximate amount of operations an algorithm
requires for the given input.
We can calculate approximate execution time of
code using time complexity and constraints.
Big-O notation
Big-O of an algorithm is a function to calculate the
worst case time complexity of the algorithm.
It is written as O(worst case time complexity)
Big-O is used to calculate the approximate upper
bound of the algorithm. It expresses how the run
time of the algorithm grows relative to the input.
More convenient and useful than other notations.
Rules for Big-O notation
• Should not have constants.
• Should not have constant factors.
• Only include the fastest growing function
for each variable.
• Can never be 0. Has to be atleast O(1)
Example function: 2(N2) + 4N + 4(M3 + 5) + 10
Quiz 2
1. (N+M) / K
2. N(N+1)/2
3. N2 + M(N2) + M2(N) + NM
4. N3/64 + 20N + (32NM)2
Calculate Time Complexity of
an Algorithm
Time complexity usually depends on:
• Loops
• Recursion
Time complexity of recursive algorithms will not
be covered.
Note: Usage of STL counts for time complexity
Calculate Time Complexity of
an Algorithm
If there are nested loops, multiply the expected
number of iterations of the loops
Example:
Quiz 3
Find the time complexity of the following code
snippets in Big-O notation:
1.
2.
Quiz 3
3.
4.
Time Complexity based on Constraints
Feasible Big-O
Function
Maximum N Example Algorithms
O(𝑁!) 10 All permutations of a list
O(𝑁3
) 400 Multiplication of two matrices
O(𝑁2) 5000
Square grid, bubble sort,
insertion sort
O(𝑁 𝑁) 105 Usually related to factoring
O(𝑁𝑙𝑜𝑔𝑁) 106 Merge sort, binary search for N times
O(𝑁) 107 Linear search, reversing an array,
string comparison
O( 𝑁) 1012 Factors of a number
O(𝑙𝑜𝑔𝑁), O(1) 1018 Binary search,
Constant time formulas
Space Complexity
Space complexity is similar to time complexity,
except it measures the amount of memory.
Any datatype that has constant memory takes
O(1) space.
Example: int, char, long long int, double, etc.
Space Complexity
Most problems have a memory limit of
256MB or ~2e8 bytes.
Datatype No. of bytes
char 1
int 4
float 4
long long int 8
double 8
long double 16
Verdict of a solution
• AC: Accepted
• WA: Wrong Answer
• TLE: Time Limit Exceeded
• MLE: Memory Limit Exceeded
• RE: Runtime Error
– NZEC: Non Zero Exit Code
– SIGSEGV: Usually due to out of bounds
– SIGFPE: Usually division or modulo by 0
– SIGABRT: Due to assert statements
Points to note:
• Identify the variables that contribute to time
complexity.
• Just because constraints allow slower solutions,
doesn’t mean there’s not a fast solution.
For example, if N <= 1000, then both O(N2) and O(N)
can pass.
• Testcases matter, unless there’s a limit explicitly
imposed in the constraints.
• The constants and constant factors removed when
calculating Big-O still matter.
Problems to test understanding
• https://codeforces.com/contest/1647/problem/A
• https://codeforces.com/problemset/problem/1538/C
• https://www.codechef.com/MARCH221D/problems/DISCUS
• https://www.codechef.com/MARCH221D/problems/WORDLE
• https://www.codechef.com/MARCH221D/problems/CHFDBT
• https://codeforces.com/contest/1651/problem/B
• https://codeforces.com/contest/1651/problem/A
• https://codeforces.com/problemset/problem/919/B
For more practice, try to figure out the time
complexity for any random problem.
Further Reading:
• https://towardsdatascience.com/essential-programming-
time-complexity-a95bb2608cac
• https://www.youtube.com/watch?v=9TlHvipP5yA
https://www.youtube.com/watch?v=9SgLBjXqwd4
https://www.youtube.com/watch?v=I0DTkS1LJ2k
• https://adrianmejia.com/most-popular-algorithms-time-
complexity-every-programmer-should-know-free-online-
tutorial-course/ (advanced)

More Related Content

Similar to Time_Complexity.pptx

Ch1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdfCh1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdfzoric99
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...AntareepMajumder
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsiqbalphy1
 
Analysis of algorithn class 2
Analysis of algorithn class 2Analysis of algorithn class 2
Analysis of algorithn class 2Kumar
 
Module 1 notes of data warehousing and data
Module 1 notes of data warehousing and dataModule 1 notes of data warehousing and data
Module 1 notes of data warehousing and datavijipersonal2012
 
Analysis and Algorithms: basic Introduction
Analysis and Algorithms: basic IntroductionAnalysis and Algorithms: basic Introduction
Analysis and Algorithms: basic Introductionssuseraf8b2f
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptxNishaS88
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsAsen Bozhilov
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity CalculationAkhil Kaushik
 
2. Asymptotic Notations and Complexity Analysis.pptx
2. Asymptotic Notations and Complexity Analysis.pptx2. Asymptotic Notations and Complexity Analysis.pptx
2. Asymptotic Notations and Complexity Analysis.pptxRams715121
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptxrajesshs31r
 

Similar to Time_Complexity.pptx (20)

Ch1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdfCh1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdf
 
Cs 331 Data Structures
Cs 331 Data StructuresCs 331 Data Structures
Cs 331 Data Structures
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Analysis of algorithn class 2
Analysis of algorithn class 2Analysis of algorithn class 2
Analysis of algorithn class 2
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Module 1 notes of data warehousing and data
Module 1 notes of data warehousing and dataModule 1 notes of data warehousing and data
Module 1 notes of data warehousing and data
 
Analysis and Algorithms: basic Introduction
Analysis and Algorithms: basic IntroductionAnalysis and Algorithms: basic Introduction
Analysis and Algorithms: basic Introduction
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
 
2. Asymptotic Notations and Complexity Analysis.pptx
2. Asymptotic Notations and Complexity Analysis.pptx2. Asymptotic Notations and Complexity Analysis.pptx
2. Asymptotic Notations and Complexity Analysis.pptx
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
 

Recently uploaded

(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 

Recently uploaded (20)

(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 

Time_Complexity.pptx

  • 2. Goal: • Understand time complexity • Understand Big-O notation for time complexity. • Evaluate time complexity of an algorithm. • Evaluate expected time complexity based on the given constraints of a problem. • Evaluating space complexity of a program. • Common verdicts of submissions.
  • 3. What is an Elementary Operation? An operation that takes constant time is called elementary operation. Example: • Arithmetic operations • Comparison of primitive types • Input and output of primitive types 108 operations ≈ 1 second
  • 4. Quiz 1 1. Is the following an elementary operation? 2. Is the following an elementary operation?
  • 5. What is Time Complexity? Time complexity is a function to describe the approximate amount of operations an algorithm requires for the given input. We can calculate approximate execution time of code using time complexity and constraints.
  • 6. Big-O notation Big-O of an algorithm is a function to calculate the worst case time complexity of the algorithm. It is written as O(worst case time complexity) Big-O is used to calculate the approximate upper bound of the algorithm. It expresses how the run time of the algorithm grows relative to the input. More convenient and useful than other notations.
  • 7. Rules for Big-O notation • Should not have constants. • Should not have constant factors. • Only include the fastest growing function for each variable. • Can never be 0. Has to be atleast O(1) Example function: 2(N2) + 4N + 4(M3 + 5) + 10
  • 8. Quiz 2 1. (N+M) / K 2. N(N+1)/2 3. N2 + M(N2) + M2(N) + NM 4. N3/64 + 20N + (32NM)2
  • 9. Calculate Time Complexity of an Algorithm Time complexity usually depends on: • Loops • Recursion Time complexity of recursive algorithms will not be covered. Note: Usage of STL counts for time complexity
  • 10. Calculate Time Complexity of an Algorithm If there are nested loops, multiply the expected number of iterations of the loops Example:
  • 11. Quiz 3 Find the time complexity of the following code snippets in Big-O notation: 1. 2.
  • 13. Time Complexity based on Constraints Feasible Big-O Function Maximum N Example Algorithms O(𝑁!) 10 All permutations of a list O(𝑁3 ) 400 Multiplication of two matrices O(𝑁2) 5000 Square grid, bubble sort, insertion sort O(𝑁 𝑁) 105 Usually related to factoring O(𝑁𝑙𝑜𝑔𝑁) 106 Merge sort, binary search for N times O(𝑁) 107 Linear search, reversing an array, string comparison O( 𝑁) 1012 Factors of a number O(𝑙𝑜𝑔𝑁), O(1) 1018 Binary search, Constant time formulas
  • 14. Space Complexity Space complexity is similar to time complexity, except it measures the amount of memory. Any datatype that has constant memory takes O(1) space. Example: int, char, long long int, double, etc.
  • 15. Space Complexity Most problems have a memory limit of 256MB or ~2e8 bytes. Datatype No. of bytes char 1 int 4 float 4 long long int 8 double 8 long double 16
  • 16. Verdict of a solution • AC: Accepted • WA: Wrong Answer • TLE: Time Limit Exceeded • MLE: Memory Limit Exceeded • RE: Runtime Error – NZEC: Non Zero Exit Code – SIGSEGV: Usually due to out of bounds – SIGFPE: Usually division or modulo by 0 – SIGABRT: Due to assert statements
  • 17. Points to note: • Identify the variables that contribute to time complexity. • Just because constraints allow slower solutions, doesn’t mean there’s not a fast solution. For example, if N <= 1000, then both O(N2) and O(N) can pass. • Testcases matter, unless there’s a limit explicitly imposed in the constraints. • The constants and constant factors removed when calculating Big-O still matter.
  • 18. Problems to test understanding • https://codeforces.com/contest/1647/problem/A • https://codeforces.com/problemset/problem/1538/C • https://www.codechef.com/MARCH221D/problems/DISCUS • https://www.codechef.com/MARCH221D/problems/WORDLE • https://www.codechef.com/MARCH221D/problems/CHFDBT • https://codeforces.com/contest/1651/problem/B • https://codeforces.com/contest/1651/problem/A • https://codeforces.com/problemset/problem/919/B For more practice, try to figure out the time complexity for any random problem.
  • 19. Further Reading: • https://towardsdatascience.com/essential-programming- time-complexity-a95bb2608cac • https://www.youtube.com/watch?v=9TlHvipP5yA https://www.youtube.com/watch?v=9SgLBjXqwd4 https://www.youtube.com/watch?v=I0DTkS1LJ2k • https://adrianmejia.com/most-popular-algorithms-time- complexity-every-programmer-should-know-free-online- tutorial-course/ (advanced)