Time & Space Complexity - DSA Series by Shradha Ma'am

Apna College www.youtube.com

Brief Summary

This video provides a comprehensive guide to understanding time and space complexity in computer science, crucial for efficient algorithm design and problem-solving. It covers key concepts such as Big O notation, common time complexities, and practical applications in coding interviews and competitive programming. The video also addresses space complexity, recursion, and provides practical tips for optimizing code performance based on problem constraints.

  • Time complexity measures the time taken by an algorithm as a function of input size, focusing on the number of operations performed.
  • Big O notation is used to express the upper bound of an algorithm's time complexity, representing the worst-case scenario.
  • Space complexity refers to the amount of memory space required by an algorithm, including input space and auxiliary space.

Introduction

The video introduces a comprehensive series on Data Structures and Algorithms (DSA), emphasizing time complexity as a fundamental concept. The series aims to provide in-depth knowledge relevant for placements and interviews, with additional resources available on the channel.

Disclaimer

The lecture covers time and space complexity, including sorting algorithms and recursion. Students familiar with sorting and recursion can watch the entire lecture, while beginners can focus on time complexity without getting overwhelmed by advanced algorithms. The practical usage section is essential for all viewers.

Why study these concepts?

Time and space complexity are crucial in programming because they are fundamental to algorithm and data structure selection. These concepts are essential for solving coding problems on various platforms and are frequently assessed in interviews to determine the efficiency of solutions. Understanding time and space complexity helps in developing efficient solutions and is relevant to almost every programming concept.

Time Complexity

Time complexity measures the amount of time taken by code to run as a function of the input size, measured in terms of operations. It is not the actual execution time, which can vary based on the machine. Instead, it focuses on how the number of operations changes with the input size. For example, in linear search, the number of operations increases linearly with the size of the array.

Big O Notation

Big O notation is a symbolic representation of time complexity, indicating the upper bound or worst-case scenario of an algorithm's performance. It helps in comparing the efficiency of different algorithms, focusing on how they scale with large input sizes. Big O notation is used to describe the worst-case scenario, also known as the upper bound, of an algorithm's time complexity. While other notations like Theta (average case) and Omega (best case) exist, Big O is most commonly used in interviews and coding platforms.

Space Complexity

Space complexity measures the amount of memory space required by an algorithm, including input space and auxiliary space. Input space is the space taken by the input data, while auxiliary space is the extra space used by the algorithm. Space complexity is crucial for optimizing memory usage, especially in systems with limited resources.

Common Time Complexities

The video discusses common time complexities such as O(1), O(n), O(n^2), O(log n), O(n log n), O(2^n), and O(n!). These complexities are visualized through graphs, illustrating how the number of operations increases with input size. Understanding these complexities helps in choosing the most efficient algorithm for a given problem.

Understanding O(1)

O(1) or constant time complexity means the time taken by an algorithm does not depend on the input size. Examples include calculating the sum of numbers using a direct formula or accessing the first element of an array. Hash table operations like insertion, deletion, and finding elements also have an average time complexity of O(1).

Understanding O(n)

O(n) or linear time complexity means the time taken by an algorithm increases linearly with the input size. Examples include linear search, calculating the factorial of a number, and dynamic programming algorithms with a single loop.

Understanding O(n^2) & O(n^3)

O(n^2) or quadratic time complexity occurs when an algorithm has nested loops, where the number of operations increases quadratically with the input size. Examples include bubble sort, insertion sort, and selection sort. O(n^3) or cubic time complexity involves three nested loops, often seen in algorithms that process all possible subarrays of an array.

Understanding O(logn)

O(log n) or logarithmic time complexity is highly efficient, where the time taken increases logarithmically with the input size. Binary search is a prime example, where the search space is halved at each step. Logarithmic time complexity is generally used for binary search and operations on binary search trees.

O(nlogn)

O(n log n) time complexity is commonly found in efficient sorting algorithms like merge sort and heap sort. These algorithms combine linear and logarithmic operations to achieve better performance than quadratic algorithms.

O(2^n) & O(n!)

O(2^n) or exponential time complexity occurs in algorithms where the time taken doubles with each addition to the input size, often seen in recursive algorithms. O(n!) or factorial time complexity is even worse, occurring in algorithms that generate all permutations of a set.

Solving for Prime Number

The video demonstrates how to calculate the time complexity of a prime number checking algorithm. The algorithm involves a loop that runs up to the square root of n, resulting in a time complexity of O(√n).

Solving for Selection Sort

The video explains the time complexity of selection sort, which involves nested loops. The outer loop runs n times, and the inner loop also runs approximately n times, resulting in a time complexity of O(n^2).

Recursion (Time Complexity)

The video discusses how to calculate time complexity in recursive functions using recurrence relations and recursion trees. Recurrence relations express the time complexity in terms of smaller subproblems, while recursion trees visualize the calls made by the recursive function.

Recursion (Space Complexity)

Space complexity in recursion includes both the extra variables used and the memory occupied by the call stack. The call stack stores data for each recursive call, and its depth affects the space complexity. The space complexity is calculated by considering the depth of the recursion tree and the memory occupied in each call.

Solving for Recursive Fibonacci

The video analyzes the time complexity of a recursive Fibonacci algorithm. By drawing a recursion tree and balancing the branches, it's shown that the total number of calls is approximately 2^n, resulting in an exponential time complexity of O(2^n).

Solving for Merge Sort

The video provides a detailed analysis of merge sort's time and space complexity. Merge sort involves dividing the array into smaller parts and merging them back together. The time complexity of the merge step is O(n), and the overall time complexity of merge sort is O(n log n). The space complexity is O(n) due to the temporary vectors used in the merge step.

Practical Usage

The video concludes with a discussion on the practical uses of time complexity in coding platforms like LeetCode and Codeforces. It explains how constraints on input size help in deciding which algorithms to use. The video also provides guidelines on expected time complexities based on constraint values, aiding in algorithm selection and optimization.

Summarize any YouTube video in seconds

Get summaries like this one for any video, in your language, with the free Summ app.

Download on the Apple Store
Get it on Google Play

Transcript

Full transcript of “Time & Space Complexity - DSA Series by Shradha Ma'am”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.

0:04 Hi everyone and welcome to the complete DSC The series that we are going to cover today about time Complexion Now We Are Working Really Hard to make this the best DSA series on youtube4 placements now in addition to dsa If you need any other concept inside If you want to read it then watch it on this channel Inside the playlist we will find it available If we want, we can go there and read it too. Now before the lecture starts a small A disclaimer that in today's lecture we have Time and space are complicated which means we can use some such algorithms Like sorting algorithms are done like Reconstruction happened and that too in today's chapter We will discuss those who are in the DSA series we're going to do it later so Students who have already been sorted There is an idea which already has the idea of recor he can watch the whole lecture who are the end students starting the series I'm following them from the fear of getting scared Today we don't need sorting and reconciliation I have definitely seen the time complexity of these

0:50 We will discuss algorithms later in the series. If you are going to cover it then the whole We will get a timeline of the lecture if You have recorded it, that's why we I want to see the recorn part, if we have just Although it is not recorded yet I will tell you I will advise that the rest of the We must see this chapter, especially the Practical usage part which we discussed in the last He has covered all the students watching is compulsory so now let's get it started now let's move on to our Concept of Time Complex Space Complicated Why Is It One of the Most Important concepts in programming Basically, no matter which data structure we use No matter which algorithm we use, Let us read Time and Space with them all Can use complexion sept When we go to any platform and ask questions When we are solving time and space

1:39 We will give the complexity no matter what the question is In 99% of cases our interviewer asks us that Time and space of the solution Complexion assessments are done inside them we have time space complexion should come so that we can quickly Moving in the direction of an efficient solution If possible, then this concept is almost every One with another concept of programming relates and we provide solution devices finding a solution to a problem he helps me a lot and that is Why It Is One of the Most Important Chapters that we cover in our DSA series We are going to end this chapter We will do it with a practical approach, that is From placement and interview point of view we are going to study this concept so that If we get practical knowledge then first of all Let's talk about time complexity

2:25 what is the most important thing time Complexion time took time It is written in a complicated way and how much time does this code take I will run sometimes the time comes 0.05 seconds or whatever time comes like this it is not time May take different time on complex windows It may take different time on Mac and Different time on Le and other websites So the actual execution time would have been The code is also machine dependent it happens when we do different online When we talk about platforms, those servers If it is also dependent then time

3:11 Compilation time taken of the algorithm There is time complexity of the code which is There is complex tea for different machines It doesn't differ according to the calculation, it is always the same if it stays then what is time Complexity is the amount of time taken a function of input size basically time The complexity of ta time taken is here We measure in terms of operation By saying operation, I mean in our code we Whatever statement you are writing, it should be in a way is the operation for example I have an integer I wrote x = 10, this will be an operation, I applied a for loop. Inside this for loop, as many The number of operations performed is the number of times this loop runs How many operations will there be in a code? are performed as a function of input Size means the input size which is n, when If it increases, how many operations will be performed less

3:58 If it happens then how many operations will be performed and less If it happens then how many operations will be performed How can I calculate the input size of my number of operations? That is, it affects the amount of time taken time changes that behavior This is called complexion, it is very simple Let us understand Ares with an example we read the concept of linear inside ares Read about search Linear search is one The Most Simple Algorithms of DS What is linear search? in we have an array with this in we are given a target in this array Inside, kill your targets one by one and then go to each index and search This is a linear search algorithm Now in this algorithm let's suppose our The size of the array is the size of the array is n because what is an array this is an array input

4:44 And its size can vary meaning maybe someday we will be the same single You may get an array with elements tomorrow We get an array with a lot of elements So this way size is our variable now The linear search algorithm is The pseudo code looks something like this: We type i < n go upto i+ p go upto anytime if The value of our array of i becomes equal to our target so in that case we let's return this i as the answer then This way our for loop is if If we do not find anything then in that case we can return -1 so this is our overall This is the concept of linear search It is easy now here let's suppose our which is n i.e. size of my array or say in which case the value of n is one How many operations did I have to undergo overall in that case? You must be looking basically like a number off to us The number of operations it takes will be the same number of times

5:30 The loop will run then if the time required for this loop to run Let me assume an operation so if my n is 1 so my loop will run only once so this Proximally we can estimate that We must be having the same operation now if tomorrow If the size of my array increases to 10 then Now this loop of mine will be running 10 times and Which operation should I do after running it 10 times I am performing it repeatedly inside the loop I am performing an operation to check and What does it mean to say worst case in worst case scenario? so I have an array of size 10 and In the worst case I can do this whole array And my target will take me You will find my worst on the last index A case happens if it is found in the beginning So this is the best case but I got it in the last The worst case is in linear search then the worst In this case, this is my loop 10 times If it works then I will get approximation in this also How many operations will it take 10 operations if tomorrow you grow bigger than my array you must be size becomes 100 then I know my loop

6:17 It will run 100 times and I will get 100 operations and if tomorrow the size of my array increases In that case, if it increases to 1 lakh It's taking me approximately 1 lakh operations because my loop will run 1 lakh times so here I have to calculate the input size Show a behavior of your operations accordingly how do I increase the input size If I am here then how are my operations changing and this change is what we call time Complicated My input size is increasing My number of operations is increasing day by day at what speed are they growing now He is growing at such a speed that he is absolutely To find out, we can plot it in the form of a graph can be plotted in the form of a graph plotting means we'll take one We'll take our y axis and our x axis. This is the drug, this is the drug, this is the drug Above we have summarized the input size

7:03 which is going to be n va axis Now we'll put our number of operations on top of I know when I have approximatively If there is one size then I have one operation, so there is one If he brings it here then I get a point It will come here when there is 10 then there are 10 operations similarly 10 would be placed somewhere here then I will get this value of 100 Let's talk about it, I am not a graph of scale I am making it but let's suppose I brought 100 here I do 100% of it, I bring it here my point will come here i.e. I Combine both the points and plot them I am going to plot 1 lakh rupees tomorrow If you want then let's suppose 1 lakh comes here And if its 1 lakh comes here then my The point is that something will come here in this way I will plot a graph of all the

7:49 for all the points so here we have set n So the x axis is given by the number of operations on y axis so when I have the graph There are many points, their function How does their function work out? A straight through all the points By taking out the line, here is one of my Come and draw a straight line for me It will be infact if we want these points can be align in some way like this These are all the points going to fall on this straight line and this What is the equation of a straight line? Math The equation of a straight line inside is y = x or you can say it as fx2 x so my what happened to the function my function this function That's it, now what is on the x axis here? there is n on the axis so we can say my the function is going to be fn1 and this is where I calculated my time complexity The value comes that now my time The complexity is big of n equal to us Write the big o symbol followed by parens

8:36 and whatever is on the right side The value of the function is my time Complexion The more complicated this value is becoming The more my number of operations is increasing are increasing in the same proportion that is why it is called linear because n whatever will be fn1 meaning number of operations will also be same If there is then time is not complexity time we get one second two seconds Time Now the question arises that she will perform complicated what do we need to do to remove that behavior Basically whenever we need algorithms Let's talk about We Always Try to Calculate The Time Complexity for the Worst Case Calling a scenario a worst case scenario

9:24 Means when our input size i.e. our n extremely large because if we facebooksignup.in is performing that it matters more because it is the worst case scenario saves us if someone Algorithm is good in worst case scenario If it is performing then it should be at small values It doesn't matter how she is performing It doesn't matter because it optimizes on that too

10:11 will perform in the same way, that is why when also we talk about time complexity We generally consider the worst case scenario. Because let's talk about keeping it real within a life scenario or if we interview If we look from the placement point of view Worst case scenario is the most Time is important The first thing that we have is complexes Better algorithms build better systems The second reason is that we two be able to compare algorithms such as for Example: The time of an algorithm The complexity is big of n and the second algorithm The time complexity of is big of n then We must know which of the two which will be the faster algorithm then the same thing Gradually in this chapter we will learn Now let's talk in detail once again about this big o that we called big o What is written exactly, Big O basically our time There is a notation for a complexification

10:57 Notation means symbol, we call it you can imagine it as a symbol like if If it is written five then it is ₹ Rupee or $ Dollar symbol will tell you the same thing if I did n If it is written then in n I time This is not the time for complexes This time is a function of complexion It gets complicated which leads us to the worst case Scenario or Worst Case We can also call the worst case as our upper bound. It is said that any algorithm works if his time In the complicated case there will be only n operations more operations or worse our algorithm will not behave like this yes

11:43 Maybe something else in the best case A scenario should come up like for example if I have this array my numbers are 1 2 3 4 5 if Before searching, first go to the index or

14:43 we represent theta se we call it Theta, this is how we have the best case scenario There is also a time complex T which we call Represent with Omega we call it Omega and Omega are generally known as our lower Bound says but in interviews Coding Platforms within Coding Tests While practicing these questions we will just using this time complexion No one will ask you this in interviews Definitely both these values inside We have plenty to take out We are limits like the formulas of calculus and derivations can be used These are equations that end within mid sums is asked within the sums but we will not cover it here because Placement Internship Interview Point of From view to practical use for us If you are interested you can read it if you want

15:30 There is a book called Karman in which There are plenty of algorithms given, you may want to So you can read it if you want You can read about Masters Theorem If you are studying then these notations also exist just like we have the Big O There is a small omega as well, one of ours if it happens then there are many different This is what you always have to calculate If you are always asking Big O questions then During the interview, we asked for any theory or

16:28 There are two ways of writing space in that code The input is any vector This input can also be an array This input is going to be seen later on as a string as well we'll call it our input and we'll call it the input as space

17:19 We need to write another array which we will call ours

19:20 Whether it is 1 lakh or n = 1 million, whatever it may be size of array but these sum variables are same If this space is built, it will be that much occupied is the space of variables or what our auxil Space is what will remain constant If she stays then what will happen to our function Our function will be big of k i.e. one in a way equal to the constant because Let's suppose that all the keys to this entire code are: all of our variables are equal if k If Constant Space is Occupied then Space Complicated because constant in bigo If we ignore then the constant space Complexion Time They say why it became constant because As the size of our array input increases There is no edge truth inside this whole code

20:05 Extra Space Occupied Space Complexity space complexion like a straight line visualizes the distance parallel to the x axis This means that the input Whether the size comes here or here no matter how much he comes here Our occlusal space should increase that much that is how much this line represents big of one this line represents big of n And in this way many different different we have space and time There is a complexion, though space ends Both have a lot of time complexity They are important but majority cases or infact from interview point of view also we mostly focus on time They have complexion systems inside them Getting extra space is comparatively easier is compared to making an algorithm

20:53 Which is more efficient in terms of time So that is why for modern systems nowadays Nowadays we use systems to do what we Write algorithms Write code Definitely Space That time has passed in complex life Complexity is given because time It is very important to save money because nowadays Storage inside computers has become cheap but everybody needs a faster more If you want an efficient experience then that is why Inside your entire chapter, inside your lecture We are going to focus more on time complexity. and the majority which we take as examples They will see our time complex TK But whatever funda time complexion The same similar process is required to be calculated There is a process to calculate the space complexion now next we some common time The complexion is going to be seen and this Where does programming come into play? inside like our different times

21:42 complexes come this is our big off This is the graph of our big of n Graph is the best time Complexion time is complexion whether No matter how large the input size is The number of operations we will use depends on the space we will use Occupied As the values become larger than one, Our time goes by the way Big Off moves towards the complicated A little less than one better time The dedication to equal the complexion is also very good There is time complexity but big one not as good as log n time Complexion Time Complexion and here we are simply x

22:28 We can imagine s as n s n s our in a bad time complex we are going to what is n and this is even more There is a bad time complex and more to it Bad timing complexion time complex t says And this generally lets us look inside Rikers. We are going to see how she gets it too Let's calculate a level even worse than this has a time complexity of n equal to factorial so as the graph time complexity within the y axis It starts getting closer, that means time Complex T is going to our worst So these are some of our best time collections and these are ours for some time Complexions happen some time later Space complexes are teases that are repeated over and over again Over and over again, within many algorithms, we whom we get to see in common time If you say complexion then first of all these

23:15 Let's look at common time complex teases where do they come from what does it mean and after that we are Going to solve a lot of questions based on time How to use time in complex questions Let's calculate complex T as well You must be learning the most common time complex T is the big one which we call constant it is also called constant time to see the complexity within those questions where we get any loop or any recursion We don't need to apply a simple formula and you have to find your answer like for For example, we need numbers from one to n. How to calculate sum without using any for loop we will simply apply formula n * n + 1 / 2 this is the formula for even from 1 to n so we simply take an integer n we saw n and after that we got the answer I calculated n * n + 1 / 2 and this too This is a constant operation, this is also constant

24:01 This is an operation, all are constant operations Let's assume that there are all k operations on k What is the value? The input is always constant. Size means if this n is bigger then this We don't see any difference inside the formula Even for small n we would need an operation For large n also we will need the same operation So here our time complexity will look like this: will be equal to end of big of k because In Bigo we ignore constants then our overall time complex is t will be equal to Bigo of 1 so this is what we call it my constant time complexion this Time Complicated You've Got a Question in which we have a given array and we have Always the first element of this array That is, the element on the array of zeros is the same If you want to get it printed then I already know hoga what is index index is zero We need to print this value again

24:47 there is no need to run a loop, otherwise this When we are printing the value then what will happen to this, this constant time of ours The complexion will become similar if our have a sorted array containing numbers is 1 2 3 and always inside the sorted array Let us print the largest value if yes then know the ascending order sorted array Largest value is always last index in R i.e. within the area of n - 1 then Whenever we print this value we will always So we need to run a loop otherwise here also we will get big o one time We will feel big of one time here too You will find out infact later on our series inside we are going to study about hash Inside the tables we can see the hash tables in the table. reads the orders c+p within the same The Java version of HashMap is HashMap which is our maps are our sets

25:33 their time There are some complications inside the map We need to add something, we need to delete something We have to find some of those operations Big Of One Time Complexion Time Complexion time is not complex tea That Amortize Constant Time Complexion Amortized means that The average comes out to be their Big of One So on the basis of practical use From interview point of view we give them big off If one can do it then it can be done practically is constant but Thorates Go and read in detail inside HashSet You might have been there but basically for your program wherever there is no loop inside where there is any if A condition has arisen where any number is off If the operations are done inside the loop then inside the loop

26:18 The number of operations of Constant Ajuye Now Constant The time complex T inside our graph Where does it appear if this is our input The size n is our total number of operations then our constant time Complexion now next common time Complex T is linear i.e. big off We call n big of n linear and it We get to see many places like For example, we looked at linear search taken is a constant in linear search If we are finding n factorials, then The loop for factorial will be something like this loop This is what is happening internally It is a constant operation so we can call it k this is also a constant operation, we call it k If you can say then which thing is variable This is basically the number of times my loop will run thing is variable if my input is n input

27:04 The size is n so I need to find the factorial of n so how many times will this loop run This loop will run n times so every time the loop How many times is the loop running? Runs n times is doing and how much the loop will do by running once Constant is getting the work done then how much work will it do by running n times If constant will get the work done then this big of n*k It happened because we ignored the constants If you do then the time complexity will be here will be big of n equal to that is why n Factorial key time complex t big off n is similar if we use the algorithm If you look at it, we have already done this as well If you want, you can go and read it too So again, what do we do inside the algorithm? This is the constant work we are doing in this loop If I look at the work inside, it is also single this is a line this is also a single line this is also a single if there is a line then what is the use of this also constant work is done inside the loop The part that says this is constant work And how many times the loop is running? Loop zero

27:49 if it is running from to n then run the loop getting n times a constant by running each time If you are getting the work done then big off n*k time Ignoring the constant became complex If you do then big end time complex t It will become similarly dynamic later programming DP when we do it then NA of Fina we will code NA inside Fina So this constant work is happening, see outside so my loop is running from 2 to n Now whether the loop runs from row to n or not Run from one to n or from two to n Can run till 5, whether it is from 5 to n What is n? n has very large values No, it's like 1 million has become 1 billion So in this way you can loop over large values It is starting from row, it is starting from one It is happening from two, it is happening from five, it is happening from 10 It doesn't matter what is happening, loop on The average will run n times only and that is why the time taken for this whole loop

28:37 complex t is this is going to be big of n Similarly if we run a loop to get the sum of Calculate n numbers, that too ours The code of the linear time complex T is If we answer the question of majority element Morse voting algorithm to solve Use the one we discussed in the last lecture. It was used inside that too in linear time If there is a complex question then Linear Time which we get to see in many places and this This is a very good time complexion is not a slow algorithm this is a good one algorithm this is a good time Complicated Joe Common Time Complex Teases What we get to see was n s and n Where do we get to see n now? we have some sorting algorithms which are not as optimized as our bubble is sorted like our Selection sort Insert sort if I go then these are not very good sorting

29:22 These are generally time consuming algorithms Complexity is the big of n s It is equal like for example here We have the code for bubble sort Bubble Sort In the code there is a for loop inside which There is another for loop and inside it we have This is an if statement now this one is the if This is a swap job, this is a job For the constant, we can evaluate it as k Now if I do my outside loops Let me see, my outer loop is starting from 0 if n goes up to n - 1 then on average what is I could say my outer loop is he runs n times this is my outer loop now if my outside loop runs once Let's assume this loop will run once So after running this once the inner loop How can you find out how many times it will run? For that we can take some small value of n Let's assume we take the value of n If the value of Lee Fur's equal n is four then

30:08 The outer loop starts from i = 0 to < n - 1 i.e. first The value of i will be zero then the value of i will be one then the value of i will be two so in this my loop will run 3 times in this way gonna have n=4 now when my first time The outer loop will run for i = 0 so How many times will my inner loop run The inner loop will return the value from j = 0 4 - 0 - 1 i.e. up to 3 then the value of j is this In case first it will go to 0 then it will go to one and then it will go to j similarly for i = 1 The value will be the first row then one for i = 2 for which the value of j will only go up to zero because these ending conditions are like i As it increases, my inner loop will decrease times will run then my loop for n = 4 It is running so many times so now whatever I am doing

30:54 I am going to tell you, listen to it very carefully if my outer loop run first time So for that I ran the inner loop three times and the inner loop runs three times to get k operations performed in the outer loop when i = 1 then how many operations The inner loop performed was run three times and it performed k operations plus when the outer If loop is run second time then inner loop will be run twice and by running it twice it performed k operations if the inner loop is performed 2*k times Runs when outer loop is completed for the third time i = 2 then the inner loop is run once and How many operations did he perform k operations If performed then + 1 * k then it is with me The total number of operations has arrived. When did this total arrive? Number of operations when the value of n is now If I take any arbitrary value of n

31:41 If I take any value of n then keep only n What pattern do I see in that? I see a pattern forming in it is n - k + n - 2 * k + n - 3 * k Similarly in this way at last 2k + k is I will get a formula for how this My total number of operations should be I already know this for any n because from here to lake n - 1 So here also from the forest we are going to n - Now this is the total number that I have given, we are going up to 1. of general operations find its final value What comes to us if we do all these If we find the sum of the values then we have the sum What will come for k so we can take common inside will be n - 1 + n - 2 + n - 3 end In the same way we have 2 + 1, so here I basically need everything from one to n - 1.

32:27 I have to calculate the sum of numbers from 1 to The sum of all numbers up to n - 1 is n * n - 1 / 2 and this value is being If we open this multiply with k So this becomes k s / 2 - k / 2 and Because we have to ignore constants If yes then ignore it in this time complexion will be ignored 2 will be ignored k will be ignored I will ignore you, I have no terms Avoid n s - n Which is the bigger term among these is n s we obviously know so this n If we remove it also then the final time will be that will be big of n so i hope we You must have understood how nested For loops multiply their number of operations by By looking at them we can do their time complexity Now this is not the case in general nested loops that we need to take time for each nested loop

33:12 Many complexities have to be calculated As soon as we see the loops we can know that this outer loop will also run n times The inner loop will also run n times outside the loop is running n times and then The inner loop runs n times each time If the time complexity is big of n s If it happens then in this way we can get lots of When the algorithms do it, they see us It will be possible to identify which n is the score Nested Loops and Which is the Nested Loop of n there is no loop with this along with We have so many sorting algorithms we've asked patters questions like when we we do patters and what is ours in patters The nested loop runs whatever pattern questions you are asking Our time complex was also there in it is it equal to big of n s in fact As a homework problem we can solve our patterns that lecture which our class had You can go and see and every single one in it We can try to calculate the pattern time complex t now like we have n s in the same way we have n amount of time

34:02 The complexity is three nested loops Our outer loop also runs n times. there is another loop inside it which is running n times and its There is another loop inside that runs n times. We have already seen an example of what he is doing saw that if we have a given array and all of his possible all array sub array i.e. continuous part of the If you want to print an array then print any array for when we know all his arrangements If we get it printed then in its code we will see I have to write three nested loops whose When does time complexity big of n come We have already read this in the last lecture. If we have done this then this would be an example for us Apart from the n ka form, there is another common time is complex t login now log in students who don't know about By pausing here you can learn about the Basics of Log So once you know the basics of log

34:48 These are the basics that are taught in sixth grade inside school In 7th class we used to learn You can revise them a bit Login time complexity is very good is considered as the end login time But it comes if this is my graph and this Our Constant Time Complexion Time Complexity is something here comes the big off n s which is equal to n If something happens then it will come to our place of n k equals endlaw n times This is the complexion of constant it is very close because l of n is very The smallest value is log. What is log? So there is power that whatever the base is like Here generally if the base is two then you can

35:33 What power should we put so that we get n value If we go, then this power is of very small value that's why l n in general is a very small value hoti hai and this time complex tea is very optimized now login time The complex T is generally used for binary search. The binary search is seen inside The code we will use for the upcoming DSA chapters You might be doing many algorithms inside too If you learn then it becomes binary search or Like if we have binary search trees then We also have to log in to BST operations How to see the time complex tea Time complexity of binary search log Now let's see what happens with binary search This is an algorithm, my answer is binary Do you already know search, if you don't then when We will study binary search and then we will understand this You can see the section below to do binary search Basically to find out the time complexity we have two points s and e then How Binary Search Works Binary Search We apply over a sorted array

36:20 this array is already sorted like inside it 1 2 3 4 5 6 this is how we are elements We are going to take out the start of our array, Find one end of the array and do a binary search ka funda says that every time I have to check my array You have to find the middle part and always focus on your target if we want to compare it with mid then every time we Remove your mid and hit the target from the mid Let's compare depending upon the target Is it bigger or smaller than mid, ours is every time which is the search space of how many arrays to search now I want to search like let's suppose target is equal to five now I know from mid So the target is big for me in the second half I have to search the whole first half in it If we ignore it completely then every time The search space is getting reduced to half next time search space will be 4 5 end equal to 6 then if we find the mid then After that there can be a search next time

37:05 The space becomes smaller, if in the beginning How big was my search space? There was an array in which I need to search if it is n then next time it will be n / 2 Next time it will become n/4 from that Next time it will be n/8 and worst How much is in the case in the worst case In binary search the search space is reduced if I reach the forest I want to remove my time complexity again So basically I have to find out how much it will cost that's n every time if I divide by two should I divide that by two as well If I divide by two then how many times will it be n in i n will become one as many times as in n will become two If I divide it from one, then that will be my time How complicated are these values R n Divide ba 2 to the power 0 then n did ba 2 to the power 1 then n / ba 2 to the power 2 then n

37:52 / 2 to the power 3 and so on Where will that value appear when n divided by 2 to the power something k or There will be something x whose value is equal to one so I basically have to find out this x because here it is zero times We have divided here, we have divided it once Here pay has been divided into time Divided here by three times x If time is divided then whatever is my x's value will come this x is my time Now how can we calculate the value of x?

42:19 a permutation a b will be a permutation b a there will be a permutation b s will be a permutation its going to be c a c b If it is going to happen then what is its total The permutation will happen if the value of n is three Meaning the size of this string is equal to three. then it's going to have six permutations What does six of six permutations mean? What is 3 factorial and 3 factorial n factorial so if I get n factorial If you have to remove the permutations then it To find out I need the Big of n factorial it will take time, it will take time, that is why this The question is also about its time complexity is equal to the big of n factorial then In this way n factorial times complex Tea exists but it is also very bad infact this is 2 to the power n is also bad because 2 to the power n is what is 2 to the power n is that we have to How many times are you multiplying

43:04 Multiplying n what is the factorial over n times that we have n then multiplying by n - 1 Then how much are you multiplying n - 2 Multiply 3 * 2 * 1 even n Times now here it's just two which means small The number is being multiplied here so two th is greater than 3 is greater than 4 then n is greater than Till then numbers are getting multiplied then this Time those verses as compared to exposition these Time complex worse than expo cial It happens and if you look at it on the chart here comes my exposition here comes my n When factorial times complex T comes then I hope we understand how Our time is complicated and versed is going in direction and generally What are the use cases where the whole Inside DSA we get time complexity If you get to see it yourself then it is a

43:51 An overview of which times Complexities generally exist Where does it tell us inside programming Now we can see that time is complicated We're going to ask some questions, then we'll recurse. How do I calculate time complexity in php we will talk about it now as we I have to solve the question for prime number We have this code for a prime number, which Inside we have to calculate the time complexity if yes then my answer is this you will get this code The logic must already be present inside this code there is a small error that this is less than equal to It should be just less than, not less than It should be equal to so now use this method If we are given the code inside this code I know I have a loop running The work that is done inside is also a constant work this is also a constant work this is also constant If there is work then how much is the overall k constant work so I just need to check my loop I know how many times it runs The loop will always start from i = 2

44:37 is starting and the value of i s when till the less is not equal then my The loop is going to be How many times will it run in the worst case? The loop will run until the value of i is n will be equal to k and the value of i s will be equal to n Saying equal means that when the value of i If it becomes √n then can I say that My loop is running from i = 2 to a √n Now whether the loop runs from 2 to √n or from 0 If we run from √ to √n then the time complexity is the same because what is n then n is very large The value is millions, the value is billions So whether you start the loop from zero or two start with five or something It won't make much difference because 1 million two or four have no value in front of And that is why time complexity is very high I know that from 0 to √n if If my loop runs then the time complex T will be

45:24 will be sold off √ equal to n so this is mine The logical loop with a prime number is its time The complex t will be equal to the larger of √n Now my next question for you will be You need to find out whether the largest of √n bigger time Complicated Time Complex Tea is the first So this second big of √ n better time Is it a complex tea or a big log a better Time I am giving you a complication, I want to tell you that Who is better among these two and who is better among them this is the next question we will solve it is selection sort so my answer is that You already know selection sort so this My general code is for selection sort Here is my loop running inside selection sort inside the loop this is a constant The operation is being performed and I have to undergo another one

46:09 The loop is running, inside this loop there is this if There is a constant k inside this as well is a constant, this is also a constant, so Constant work is being done in just one loop There is another loop running inside which runs Constantly performing work forces me to do it now Know how many times my outer loop will run The outer loop will go from 0 to n - 1 so How many times the outer loop is running? Outer loop running n times if the outer loop is a times then how many times my inner loop runs running inner loop goes from i + 1 to n for example the value of i when the row then where will the value of j go from The value of j will go from one to n when the value of i value one is value of j is value two to n when the value of i is two and the value of j is 3 will go from to n so can I say

46:54 In First Time my loop runs n times I am talking about the loop inside will run n - 1 times in a second The third time it will run n - 2 times and I see this pattern then n - 3 n - 4 Last time how many times will my loop run will run once when the value of i finally reaches n - it will become 2 Last time n will be - 2 Then the value of j for n - 2 will be n - should run till 1 and j < n then my loop will run only once then the outer loop will My inner loop is running for the first time If n - 1 n - 2 runs in this manner then Total number of times the inner loop will run Well, technically it should be n - 1 n - 1 n - 1 then this is n - 1 n - 2 n - 3 n - 4 It should be this way but even if you are here write n here you can write n - 1 here 1

47:40 Withdraw ₹ from 10 Million in Million It doesn't make much difference above $10 million so that is why here whether we write n I'm writing n - 1 times If it won't make any difference to Complex T If I have to calculate n + n - 2 + n - 3 in this manner If you want to take the sum of all the numbers then 1 The sum of numbers from n to n is n * n + 1 / 2 and by running my inner loop that many times Perform Constant Operations of Operations if i am doing this then this is my total time complicated i already know it's big off n is equal to s that is why Selection Sort Key Jo Time I could have complicated it, I knew it This is a constant work, I know it The outer loop will run on average n times

48:26 Inner loop if outer loop is run once then How many times the inner loop will run in worst case In worst case, it will run n times then the outer The loop will run n times and the loop will run once If it runs n times then for each time in time So we can find it out this way also Every time we need to do some calculation

49:55 Save your time by solving it mathematically

52:01 If the value is row then we can simply return If you want to give one then when the value of n becomes row then this How much work will this constant work k2 do? We can say so let's suppose this is some Other constants, we will replace it now left side end right with k2 If we add up all the terms on the side So many terms cancel out each other will happen as if fn1 is cancelled out by this will go to -2 this will cancel out f3 If something happens here, it will get cancelled out Similarly this value will also get cancelled out If it happens then finally which one is on the left side The term will survive only on the left side what survives in fn1 is equal to right will write many k values In fact, she will survive here also, this is going to be k right has lots of k If constant values survive then their I take common k for this but how much k is this

52:48 We have constant values ranging from n to n - 1 n - 2 have reached the forest in this way, then whether We may go from one to n or we may come from n to 1 How many equations did we need? We got n equations If it appeared then how many times did k come? Did it come n times? Plus who will survive these terms Of these terms, just this last one is k2 If he survives then we can add k2 This is basically our overall time complex tea Time will come to a complicated side If we ignore constants then k is ignored hoga k2 ignore hoga and the time complex t is going to be bigger than n so in this way from our time complex t which is A particular code will come and that too a linear one time complex t will be big of n then this We have considered the time complexity as our Now found out from the recurrence relation There are many such questions for which Recurrence relation is a little intuitive Like merge sort, like finalize

53:34 But there are many regions with codes which Recurrence relation is not that intuitive for If it were so, we would have used our second for those cases Which method do you prefer for finding out the total number of requisition calls and these We find out by drawing our Recursion Tree Now we can define our recursion tree as how can you draw for this particular code let's suppose our n is The value of fur is equal to n = 4 4 What would the factorial tree look like for n = 4 For this, we will first call f4 then he will call f3 f3 will call f2 f2 calls f1 f1 calls f of 0 If it applies then can we say that every level If you are making a call from your lower level then If n = 4 then the probability of 1 2 3 4 is The calls are looking like this inside this code

54:20 If the value of is five then there will be six for that If the call is six then it will cost seven If calls are made, can we say that For any n we are making total n + 1 calls or infact it is not even n + 1 we are One can ignore it from the beginning if one wants If the value of my n is anything then its For in general we approximately call n If you are applying this then how many recursive calls are there These are n calls, calculate your answer will do it will return one to the upper level So what will go from here one return from here It will pass through here from one forest to another By multiplying the upper level, We will return here by paying one to two Multiply the upper level to 2 I will return it here with three We will multiply it and return it as six. Here, multiply it by four and increase it up we're going to return to level 24 finally what is 4 factorial 4 foto This way, ours is equal to 24.

55:05 If the calculation works then the total If approximately n calls are being made then Total calls we have is equal to n How much work is done per call by multiplying There is work in each call, if we talk about one call then that means a function call like this There is an if statement inside the function. constant function it returns n These calls are being made by multiplying by this is also a constant job so overall one Constants are being used inside a function call we can take the constant k as the time constant The complexity will be big of n*k or We can say this is equal to big of n so If the same answer comes here, even if the answer time is We extract the complex relation from the reference relation Whether directly through this formula try to find out the answer is always there going to be the same so the time complexity of This recursive code is going to be linear now Like we have time complexity

55:51 Similarly we are building a space complex for Rekjanes t can calculate the end of recursion space of normal codes it is complicated and the difference is that whenever we recurse we have a The call stack is now factorial when we If we calculate using a for loop then So we count that for our code of the inside or the factorial whatever we have Write a program with a for loop inside it How many extra variables did we create We created an extra array why did we make extra, did we We made an extra stack of all those things Let's calculate but if inside Curzon Inside this code we are not adding any extra variable Have an extra stack even if you're not making one which is used inside the memory and also We need to count within our space complex

56:37 which we have to do is our recon call stack which contains the data of all calls We store it in our space We count within a complex, for example For example, here we have used factorial. written the code we know first for n e 4 If the call of f4 is made then we will call f4 in this way we can show that the call for n = 4 will then be n = 4 will call n = 3 then n = 3 f2 then there is another call stack here Memory Occupied inside .NET memory inside another call stack If the call for occupied f0 goes then another call Memory Occupied inside the stack We have not used any extra variable here. created but when recording internally If this happens then this stack is inside the call stack Frames Memory Occupied

57:23 But now let's talk about this space complex t is what comes out recurs The space inside Let's calculate the complexity space The extra variable that we will use here Plus inside that we also have this call stack Let's add memory and this is basically become the depth of recurs Tree multiplied by memory occupied in each call or infact by changing this thing We can write that the space is complex Multiply by memory in each call if you want If we talk about the depth of the reconstructed tree, it means from f4 to 3 2 1 0 this is the reconstruction tree

58:08 What will be the depth, this should also be equal to n or whether we talk about the height of the callstack Do the worst case scenario in the worst case scenario What is the maximum callstack in the scenario The height will reach the maximum height approximately equal to n will be n + 1 going to n + 2 going to n -1 going to It won't make any difference approximately n so even if we increase the height of the call stack Let's see the recurse tree we created Let us see the depth of this method both The value of is going to be the same because The recursor is working the same way The way to visualize is through records This is actually happening inside the memory there would be a way to visualize it If yes then in both the cases total height will be is equal to n and how many calls are there in each call Memory is occupied, in every call we have The age true variable is not defined and Even if I had defined the variable, it would have If there was constant memory then this would have happened here is also constant to space

58:56 t becomes equal to bigger than n so here even two we'll see that inside of Recurse we've No variable defined yet big of n key space complexity our code it takes because of this call stack then Three things we can remember for Rikers First of all, time inside recurse The complicated method is generally easier and most Space is preferred can calculate the complexity and If you know how to make a recognition tree then we If you understand Recorn well then Removing space and time complexity from our It becomes very easy for you, now let us solve it A question based on reasoning let's suppose We have been given the code for the receivable fbo The meaning is that you have already recited the question the time has been done now if we Time and Space Time to talk about complexion

59:43 Let's draw the complexity tree Suppose for n = 4 the maximum number of phobos for n = 4 is first the call will go to f4 what will f4 do He will check the base case, if there is no meat then he f3 calls and f2, then f4 calls f3 I made a call and called f2, now what is f3 There will be no base casement for f3 also So he will call two and 1, then f3 and f2 will make a call and will make a call on f1 similarly f2 will call f1 and then call f0 Here also f2 will call f1 and f0 will call will make a call here the base case is hit this base case is base case base case base case base case so Our overall total calls are as follows: Now generally when the recursive tree is used This type of incomplete tree is being formed inside If it happens then we will form branches and

1:00:29 try to balance it out so that Our last level should be completely fed now If the last level is completely filled then What was the second method of time complexity time to do Compilation number of calls or total calls We call these multiplication by work If each call is done then what is the total number of calls We are done with our first level There is one call, two calls on our second level we have four calls on our third level Similarily because we have balanced out So our last level pay at calls will be Can we say 1 = 2 to the power 0 2 = 2 to the power 1 4 = 2 to the power 2 8 = 2 to the power of 3 so this is how our calls go what is there is increasing at every level if We have an arbitrary n instead of n = 4 If there would have been any other value of n then the total

1:01:15 How the number of calls appears Total number Of calls are on the first level 2 to the 0 + 2 to the power 1 + 2 to the power 2 + 2 to the I would have got power in the 3rd and last level know there would be n - 1 calls because here 4 is n = 4 so in last level 2 to the pa 3 If there are calls then similarly here the last level I have 2 to the power n - 1 calls now These are the total number of calls to us If you want to add then this is the total number of calls forming a geometry progression together The Total Sum within a Geometry Progression You can also search about it by going here

1:02:01 gp2 value which is equal to 2 to the power 0 what is multiply by r to the power n What is the common difference between all the terms The common difference is two is equal to 2 to the Power is the total number of terms from 0 to n - 1 means n terms -1 did 2 - 1 this The value becomes 1 * 2 to the power n - 1 / 1 So here this value becomes equal to 2 to the power n - 1 then the final value 2 to the power The result came from summing up all the total calls We have received calls; our total number of calls is 2 to the pa n - 1 and how much work is done in each call This is a constant work of one call

1:02:47 this is constant work this is constant work so multiply by k then overall time what will be the time complex tea this -1 will also be ignored then this if we don't balance it out, it would have been out Get accurate recursive time of Fina

1:06:24 First let's look at it here What is ours? This is our constant work. what is this it is constant work these two who Calls are going in, this is also constant work But this merge function is so much work how much time does it take us If we don't know then why don't we merge it first Time and space complexity of the function itself take out the merge function if we know If it goes, we will know about every call because every merge is a short call When a function is used, what do we do first? We treat the disease individually step occurs in the merge sort algorithm This is the time and space complex t If we remove it then inside merge sort this We have a merge step which if Hum Time This is not a complication, this is a normal function inside this we have a vector so this time Talk about complex tea, constant work There's a while loop going on here

1:07:11 The work inside is constant here is a value The loop is running and the work inside is constant while loop is running constant work then one and our for loop is running inside constant work so basically this is our loop This is the second loop running, this is the third one The loop is running and this is the fourth loop running So everybody's time is getting complicated If we add then we have overall time Complex T will now come with Mers algorithm Is this merge step logical? does it logically take two arrays Let's suppose there is an array of one and five and The second array is three and 6 in this way the array One will take and two will take, it is the job of both Merging these theories I hope we already have You must know that this merges these two. In a sorted manner, this one will come first, then this one Three will come, then this, five will come, then this If six comes then it will be sorted in this manner So its job is to merge them

1:07:58 This first while loop is merged Within the sort this continues until Either of the two arrays is complete not traversed and remaining end to end The three loops run until there are any remaining The array remains, for example, 1, 3, and five All of them have been copied, only these six remain here If this is done then there is a separate loop for six If it works then these three loops together will be merged step What work do the three loops do inside Combining both arrays within the merge step Traversing the entire top Do Now I know inside the merge step I can compile this array the size of if is half the size of this array if If it is half then the size of both arrays together is n It happens in the worst case i.e. overall if my n value was four so merge step one array will be of size 4/2 the other array will also be of size It will be of size 4/2 and by merging both Array of size four will be recreated merge

1:08:44 You will understand these things only then inside the sort will come if we understand the logic of merge sort If it is already clear then these three loops together Big of n times go and this is the last one there is a loop because it has this temporary from this we can put the values inside our final array If you are copying then temporary size The size of the final array is also equal to n If it is equal then this would be the fourth step this also runs big of n times so The time complexity of our merger has become big off n + n i.e. on the equal of big off 2n Because we ignore the constants then our overall merge step time is complex t comes big of n equal to That is why the time complicated step not merge sort merge step is bug now In the same way we can see the space complexity there is space One has to look at the complexity values

1:09:31 So the input is an oxilave input what is it this array is input so the array is I don't want to count, besides we have a I have created a vector and inside this vector Push back on these values again and again If we keep pushing back, should we say can we complete it because i have n If the elements had to be stored then n n Size to store elements that we take temporary memory for this merge inside the step because what does merge saw do he has two arrays he takes both of these arrays A temporary way to merge elements takes an array and what is this temporary array this is our temporary array If Oxyclave happens then the space complexion also become equal to big of n so this jo The merge step is its time and space

1:10:16 Time and Space of the Complicated Sort We do complex calculations individually For this merge step, we have Calculated the big of any time Complexion B of Any Space Complex T This is how merge sort works when n = 4 Let's take the example of an array of size merge What does sort do if the array is of size 4 then first divide it into two parts will do it means it will divide it into two calls in which it will be of two sizes and then two if the array is of size then it will also be generated in two calls It will also divide it into two calls will divide inside and while back tracking When we go here, the disease that will This is a step that will be performed here as well If you track then perform the merge step If you back track here also then merge will happen This step will be performed generally like this The way our merge sort works So in merge sort our recurse tree it will be made in such a way that the most

1:11:02 If we first make a call of f4 then this call f2 will be done this will also be done f2 then this call this will also be done f1 this will also be done f1 this will also be done f1 it will be done and this will also be done f1 yes but this the value inside this is our array size so can we say that first Level 2 to the power 0 calls in seconds Level 1 2 to the power 1 call third level 2 to the power 2 calls or should we call this thing You can also see it like this that at the first level if the size of my array was four then next The size of the array is halved with each call to the level In the next call the size of the array is halved So if this is any other option instead of n = 4 If there was arbitrariness, then in that case our What do we have we can minimise it a little bit let's do it let us reset this if anyone If there was arbitrariness, then in that case our

1:11:48 the call of f n passes the call of f n f n / 2 He would also make a call of n/2 and then this f I would have made a call of n/4 but here also f is n/4 call sounds similar f n / 4 similar f n / 4 so in the first level if my array has size is n next level is becoming n / 2 next level is n/4 And for how long will this half keep happening until finally If the value does not equal one, then every time what we are doing is every time n = 2/0 n / 2 ^1 n/2 ^2 what will happen similarly was n / 2^k so if I want to figure out What is the total number of levels then I I can say I have x levels if of this recurse tree if I have the total If there are x levels then what will be the value of x or

1:12:33 Basically it's not k but 2 to the power x but n / 2 to the power x = 1 this gives me n = 2 to the power x on both sides if we take the log l n = x so can I say I have If there are total l n levels then one of them is The understanding came to us that our What is the total number of levels passed? Recert The total number of levels inside the tree is equal to l n now if we have to calculate the time We see a complicated tree, then time Complexity is Equal to Total Calls multiply by work done in each Now in my recurse tree if I have

1:13:25 If there is any work then give me a call in one go. Big offer n work It seems that if I have made a second call then In this call of fn2 I will have n/2 work in the call of fn2 i will do n/2 work so Overall, combining both, my re-jo Work done on these two calls is that too big of is going to be equal to n similarly next At this level, n/4 work will be done in this call in n / 4 in this call n / 4 in this call n / 4 and the sum of the four calls is n / 4 * 4 That means if the work will be equal to the big of n then my In recursion tree if at every level big of n work is being done at every level if big of n The work is going on and how much do I have in total I have total of log n levels so the work that I am doing is equal to l of n so my total time complex tea will be done that will be big off n multipla ba l n and this is the time

1:14:13 If it is complicated then merge it in this way Joe Time If you get a complexion then it is done How to find out our time complexity We will know our space You will have to find out the total depth of your records The depth of our recursion tree is Equal to login but within each call Now all of this is constant space No extra space occupied inside it Space Occupied within the step The step has a space complexity is equal to big of n because in this we You are using your temporary vector now this is a temporary vector when our Space inside the merge sort algorithm stack If it takes it then one of our calls will go here to n then next call will go for n/2 Then the next call will go to n/4 next call will go for n/8 then this

1:15:00 How many total login levels we occupy inside the cursive call stack but for every call what are we doing inside separate merge calling the step and giving your one You are creating a temporary array of size n so this Space occupied by n it is in the memory as it is and the next At the level we are creating a new vector from that Creating a new vector at the next level and this space we created the Acumatica vector here and As soon as our function got over This vector is also deleted from memory So in such a case we will say space complex t is equal to l n + n + n because This space with n is not such that when If this whole stack continues, then every level N Space Occupied Every level would not have been en space

…

Share

More video summaries