# DAA 27 Sep

- **Course:** 5101 - Design & Analysis of Algo
- **Recorded:** 2026-09-27 00:00
- **Transcript:** 771 segments, 10289 words
- **Made by:** deepdml/faster-whisper-large-v3-turbo-ct2

---
Okay, so let us start. So, in the last class, we were discussing about maximum sub-array problem. Okay, so here maximum sub-array refer to set of values stored in an array with both negative and positive signs and we need to identify a subset whose sum will be maximum. Okay, so discussing about maximum sum. So, we have two sub-arrays. identify a subset whose sum is maximum. So, if there is one positive number and all are negative in an array, so you need not to bother anything, just the positive number will be the maximum summary. Okay, because that is the only positive number and if you include any other number whose are negative so obviously that number becomes the that numbers becomes the maximum sum then yesterday we have also witnessed brute force and divide and conquer based mechanism in brute force we have used some heuristic we have used some heuristic that is to minimize the search space, to minimize the space, to minimize the search space.

Okay, so the problem was we have removed the reoccurrence of elements or duplicate entities and thereby we have reached to the computational complexity of order of n square. Then we have started discussion on divide and conquer, okay, that imposes a computational complexity of n of log of n. The divide and conquer strategy primarily rely on the similar feature what we have studied in merge sort and fix sort, dividing the subarray into the smaller component till we have reached to a single element label. So the base condition of the recurrence is what? when we reach to single element label. And then we will do what? Then we will identify left sum, right sum and cross sum. So, left sum is single element, one right sum is that and if there is one element then cross sum will be just the addition what yesterday we have seen. But when it is applicable when there is one element and here is one element. And later on we will decide

and in cross sum what we have identified, from median we will go this side and from median we will go this side and whatever the maximum possible sum value in the left side, maximum possible value in the right side will be added, Ls plus Rs will be added to get the cross sum. Okay, so this was the logic and as we have seen the sub array can be on the left sub half or right sub-half or on the intersection. Okay. Then, this is the algorithm. Okay. To find. So, here it is written if high is equivalent to low, this means that we are having one number. Then, we will return what? Low, high and A of low. A of low means that the array having one value. That is the, this is, this condition is called as the base case also written, base condition. Otherwise, what we will do? Here we are using floor. Floor of something. You can use int. You can also use ceiling. That is totally up to u. Then we are doing what? Left low, left high and left sum will be again coming from a low mid.

That is half. This is a mid plus 1 and high. Again that is half. And for cross sum what we are supplying? A, low, mid, high. Okay. Then it will call. And after, so this will be a recursive call to the same function. And this will be the recursive call for cross sum. What we have did? From left, from median, we will go to the right. From median, we will go to the left then what we will do we will check out we will check out maximum sum this side and this side so this this is a functionality of the find crossing survey okay then we will check if left sum is greater than right sum if left sum is greater than cross sum so result will be what left sum otherwise right sum otherwise cross sum so what we were doing there could be left sum highest right sum highest or cross sum highest whichever is highest will be your result so this is how the algorithm proceeds and there is one more part for this one that is called

maximum crossing subarray and that imposes the computational complexity of order of n because if you see what we are doing from the median we are maximum going what n by 2 and here we are going n by 2 for calculating the sum so that is why it imposes the complexity of order of n. And this recursive division imposes log of n and these one or two comparisons we can say it is 2 or something. So it is n of log of n plus 2 or something. So this could be ignored and ultimately the computational complexity reaches to n of log of n. So this is one problem, the same problem that we have addressed today. I have tried to solve via the discussed algorithm. So, just try to follow because it is a bigger problem of 16 elements. So, you may, it will take longer time. So, this is totally up to you. You can take 5, 6 element sub-array and try to identify the maximum sum. Okay. So, before starting the dynamic programming variant. I am showing you this example again with indices. So now we have

13, minus 3, minus 25, 20, minus 3, 16, minus 23 and 18. We have to identify maximum survey array for this. Now, we have divided. So, we have reached to this situation. 13 minus 3, minus 25, 20, minus 3, 16. And they have indexes 1, 2, 3, 4, 5, 6, 7 and 8. So, this will help to identify the from where to where we can have, we have to identify the cross sum indexes. So, 30 left sum is 13 so we will see what low high sum this is low high and sum value similarly from this side we will have low high and sum value that is 2 2 minus 3 so which one is highest this one is highest because cross sum if you calculate the it will be what 1 2 10 this is cross sum this is initial index this is longest index and this so ultimately after this we will get this as the solution that will propagate and here these are the lower lower index higher index and the sum what we are exactly doing yesterday then from this side 25 20 so we will get 4 4 20 so the right sum is now 20 and

here left sum is now 13 okay so for cross sum what we have to do lowest of this and highest of this So, it will be 1, 4, 10 this side. So, from here, if we are going, so what we will get? 10 this side and here we will get minus 5. One thing. Second thing is that if we take 25 as medium value, what yesterday someone was asking, then what will happen? 20 this side and 28 minus 28 and 13. That is minus 15. So, I guess this may be wrong. Okay. So, what we are getting? 10. And this side we are getting minus 5. So, we are getting what? 5. Okay. So, among these 3, which one is highest? So, among these 3, this 20 is highest. That is 4. 4, 20 will proceed upward. Similarly, for this, we will get likewise this. And similarly, for this one. So, just check it out. Maybe some mistake is there. Okay. so what basically we are doing we are doing what we are calculating the cross sum so this is was shown to get the lowest index from the left one and

highest index from the right one yes okay highest index from the I guess I'm audible okay now there's some issue I'm audible okay okay I'm audible there was some issue. My screen will have stopped shared. Okay. Let me continue. Okay. So, now what we are basically doing. Okay. And there was one more confusion yesterday before going proceed. Further, I was just doing it. So, one more confusion is there. So, for identifying the mid, here we are taking what? Ceiling of low plus high by 2. Floor of low plus high by 2. That is low plus higher index by 2. So, that you can use. And median, median you can use n by 2 for even n, n by 2 plus 1 for odd. Okay. So, just check out these two, then your problem can be solved. Okay. Now, here it is saying how the time complexity is calculated. Okay. So, this one and this one we can ignore. And every time we are trying to do what? We are trying to get two sub-problems and the cross sum that required order of n computational

complexity. And if you witness the recurrence relation, the recurrence relation is as similar as merge sort and therefore the computational complexity reaches to n of log of n. Now, we have seen two algorithms. One is brute force whose computational complexity we have identified n square, then we have divide and conquer whose computational complexity is n of log of n. The good thing about this maximum subarray is that there exists also a dynamic programming algorithm. Okay, so before starting the dynamic programming, I will just briefly say about what is dynamic programming. We will see separately other problems as well in the dynamic programming, but the dynamic programming variants of algorithms are faster. Second thing is these algorithms are faster and we know that what we have did in divide and conquer big problem is splitted into smaller then is splitted into much smaller. So, this is basically a top down approach or problem solution will be from top to down this

is divide and conquer okay and i am talking about dp dp are faster but in case of dynamic programming we are primarily following what bottom to up okay so here basically divide and conquer what we were doing dividing a larger problem into smaller sub problems and subsub problems then solve them and go ahead. DP say that I will solve smaller variant of problems, aggregate them to get the final variant and lastly I will aggregate to get the final result. So, though they look like similar, one more important thing is that at each stage, it preserves the outcome of the previous state. So, at each step, it stores the outcome of previous results. Now, it is storing the outcome of the previous results. So, what will happen? Let us say at step 1, it store O1 operation, O2, O3, O4 output. So in step 2, it will automatically reach to a solution where it need only O2 and O3. Means that it will curtail down the insignificant value at each step.

or we can say the dynamic programming, identify or get the best value possible at each step, best value, best result possible at each step. So on each step, if you check, you will find available and as it proceeds, the results are proceeding. So in each step, you will find the resultant will be the best one. So we will also see in detail how this is working. So there is one thing that they are faster. Dynamic programming are faster. And one thing is that memory inefficient. Memory inefficient because it stores the result of all the previous outcome or all the optimized outcome in each step. Now, one more question arises. Sir, you are saying in divide and conquer also, we are, when we are proceeding upward, we are storing these results. Okay, now think about a situation when we are solving a problem different. Okay, we are following bottom-up approach. So, I store this result, this result, this result, this result, then I will change the label.

Okay, now I will store this result, this result, then I will change the label. So, we are having all the outcomes of the sub-problem saved. or in each step. So, that's why on cost of memory, we are getting a faster algorithm and if we compare with the processing speed and the memory, so we are now having sufficiently large amount of memory. Thus, we can say that dynamic programmings are more viable in current word scenario. And you will find that this DP variant will impose a computational complexity of order of n. Again, I am saying I will explain this dynamic programming algorithm in a detailed one after some time. So, let us begin the problem. So, the cadence algorithm, this famous algorithm that is a dp variate for solving the maximum subarray problem. So, that is cadence-ergonic. This is the only simplified algorithm. So, we have to initialize two variables, that is current sum, I am writing as cs,

and max sum, that is m. These two values, we have to do an initialization. Some initial value, let us say, let us say, I am taking first index, or any of the index. So, this is the initialization on the first index. So, as if you remember in case of insertion sort also, we were taking the first value, okay, as a reference point, similarly here we are taking the first index as the decisive point and we are identifying, we are taking current sum and max sum as the first value. So, what we have it cs is equivalent to ms is equivalent to the array first index element that we have stored. Now what we will do for starting from the second position iterate throughout the array so of course it will be a for loop or a while loop whatever you want. So for each element what we are doing current sum is nothing but maximum of current element. Current element means that the element as index number 2 comma current element plus current sum. Then we are updating max sum

equal to max of current sum at max sum. So these two steps you have to remember then you can easily solve any problem via cadence algorithm. So simplified version I am writing for 2 to n. What I am doing? Current sum is nothing but max of current element comma current element plus current sum and max sum is nothing but max of current sum comma max sum. So, these two steps you have to remember and surprisingly if you see there is a single for loop that is running for n minus 1 time and thus the computational complexity of this variant of the maximal saveria or dp variant or the cadence algorithm will reach to only order of n because here we are not making any more complicated multiple for loops or recursive division. So that is the great thing about the cadence that it imposes only a computational complexity of order of n. So, if you remember, this you have to say, current sum, maximum of current element, current sum plus current element.

So, what I will do, I will try to solve this problem via the cadence. Just remember these two looping steps. So, just give me a minute, I will write down the problem. Then we will start the solution. Afterward, I will take the queries. I am writing. So, here the algorithm that is or 2 to n or 2 to n what I have to do, current sum will be replaced by max of current element comma current sum plus current element and max sum will be updated as max of current sum and max sum. These two things we have to update otherwise the algorithm remain intact. And we have a problem that is minus 2, 1, minus 3, 4, minus 1, 2, 1, 1 minus 5 and 4. So, let us solve. So, you know that in initialization current sum and max sum will be the value stored as the first index. So, I have initialized. So, current sum equal to max sum equal to minus 2 initialization. Now, we are starting with for i equal to 2 first condition. so what we will do current sum is equal to max of what max of current element so if you see

second index we have current element 1 comma 1 minus 2 which one is highest of course the value will be 1 because this will be minus 1 now max sum is equal to what max of max sum and current sum So, this and 1, of course, we will get a value of, I guess you understand, it is current sum 1, okay, I have just written reciprocal. So, current sum is 1 and max sum was earlier, if you know, its value was minus 2. So, what we will basically get 1 as the resultant, okay. So, step number 1 is over, then we will do what? i equal to then i equal to 3. So, current sum equal to max of current element. Now, the cursor is shifted here. So, current element is minus 3 and current element, current sum that is 1 minus 3. So, what we will get? Minus 2 as the current sum and max sum is equal to max of current sum that is minus 2 and max sum was 1. What is the maximum? 1 and if you see for these three part the maximum possible value is 1. So, that we are getting. Now cursor

has shifted here and we have shifted i equal to 4. So, current sum is equal to max of max of current element that is 4, then 4 that is current element plus current sum. So, 4 plus minus 2. So, what we will get? We will get 4 as the resultant. Then max sum equal to max of what? Max of current sum that is 4 and this one is 1. So, what we will get? 4 as the resultant. Okay. Now, i equal to 5. So, cursor shifted here to minus 1. So, current sum equal to max of current element that is minus 1, current element minus 1 plus current sum 4. So, what we will get? 3 as the current sum and max sum equal to max of current sum that is 3 and max sum that is 4. So, we will get 4 as the resultant. Now, at i equal to 6, we will get the pointer shifted here. So, current sum equal to max of current element and current element plus current sum that is 5. What we will get? 5 and max sum equal to max of 5 from a 4 then we will get what? 5.

Okay. Now, the next element is 1. So, i equal to 7, current sum equal to max of current element that is 1 and both addition will be 6. So, we will get 6 as the current sum and max sum equal to max of 5, 6. So, obviously, it will be 6. Okay. So, then we will shift to this one that is minus 5 okay so I am just trying to do if I can so i equal to 8 what we will get current sum equal to max of what current element that is minus 5 and when we will add minus 5 with the current sum what we will get 1 so it will be 1 and max sum will be what? Maximum of 1 comma 6. So, obviously, the resultant will be 6. Then, i equal to 9. The pointer is 4. That will be max of 4 comma 4 plus 1 5. So, obviously, it will be 5 and max sum will be 5, 6, the resultant will be 6. So, for this sub-array, the maximum value is possible when we are getting these sub-array, that is 4, minus 1, 2, and 1. And the value is 6. Okay. So, but the good thing is that this algorithm is taking order of n computational

complexity. Now, I will take the queries. So, Rakesh, you can enable mic for those who raising hand and then we will proceed i hope rakesh you are there just give me i'm enabling one by one just rakesh you can intervene uh yes uh anuja uh sir good morning sir good morning so just wanted to understand two things uh like how this 2 to n is being implying here and second thing is the initialization step like when we have this right this error so i'm just trying to get there if you can just give a dance actually actually if you see the algorithm so what it is saying this time we're doing this we are starting from the second position and initialization is nothing but we are declaring current sum and max sum equal to the value stored on the index number one or zero whatever you are using at the first index okay so i have just used that value and then moving ahead so what is the maximum values stored in the first element itself so nobody so your max

sum will not change here it is the max sum is max of current sum and max sum so your current sum change but max sum remain intact so let's say initial 20 is there and others are negative watch something so so maximum will remain intact okay still if you have problem because let's say your doubt is there now so others doubt will automatically resolve okay yeah i mean i'm just trying to make a version again if you can go back to the l you can say the array that will help so if i say that i have to initialize this so how should i proceed like is it like the minus 2 and then minus 2 is your first index that we are taking as current sum that is equal to max sum equal to minus so let's say here we have taken 200 plus 200 so never this max sum will change so that will be of course first value will be the result and iteratively when we go from this 200 to the next index sorry the next indices so automatically

it is the third index right yes okay and is there is any like a specific like relationship say I'm just trying to understand more here like how it could be more more in terms of 2 to 1 is it just because we are saying it is starting from the second index that's why okay yes or because if you are having a zero index thing so you can do 1 to and minus 1 yeah yeah like any list we have that as in 0 so that's where it is okay thank you so much yes Rohit yeah good morning sir sir can you go to that the example you were just okay so I understood the step but how you identified the subarray like it's starting from like 4 as the value and it goes to 1. So, for this algorithm, you will get the maximum sum value. Okay, maximum sum value. But the index, if you specifically want, so you have to add some points to store. For example, when I am getting 6 here, okay, when I am getting 6 here, so I have to look back the previous value. So, it was 5. So, it was 4.

Earlier, it was 4. So, whenever there is a higher positive value, i store this index and whenever there is increment in that value that i will store that so from this to this will be the sub array getting what i am saying for this algorithm okay whenever the maximum value is there a maximum i am also preserving the index so i preserve the index of 4 i preserve the index of 6 and then use those index to identify the sub array but in this algorithm there is no such mechanism of directly identifying the indices but what we will do when the we are storing the maximum we are we also store the maximum the indices okay and whenever and the for for example i have stored i equal to four okay so here if you go so i will store i equal to four in some additional array okay and i will store i equal to six seven n so what we identified that from where to where there was an increment in the value that will be your third

subarray so but there is no direct linkage of identifying that subarray in this algorithm okay so that we have to explicitly add some steps here so uh so would you would you mind like uh just like using the indices as such and then expecting it so it would be more clear let's say i initializes a list okay or that is i or i and i am writing initially it is empty so here the index will be what 1 then what we will do because that is holding the max value then index is 1 next Next one is 2 and correspondingly you have to also maintain an output. So we are maintaining 1, 1, 2 arrays we are simultaneously doing. So there is output array. Then here we are using in that is 1, 2, 3 and the output is what 1, 1, 4. we are storing in that is 1, 2, 3, 4 and the output is how much? 1, 1, 4, 4. Okay. And similarly, we will store for this one that is 1, 2, 3, 4. Now, after evaluating all what we have declared the sum is 6. So, we will immediately check into this array where the

6 was first appearing. So, 4, 4, let us say 5 and 6. So, we identified that its index value was 6, let us say. So, we identify its index value was 6, where 6 was the maximum sum. Then we will go back and see that from where the increment was already started. So, we identified the first increment that we have encountered was 4. So, that is an, you can say, The assumptive algorithm not exactly exists in this cadence. So you have to modify the algorithm accordingly. Getting. So that is not there in cadence. You can store the values in two surveys and accordingly take your decision. Okay. Getting. Yeah, I got it, sir. Sir, I have the question related to the divide and conquer. Can I ask that, sir? Yeah, please. so so in that strategy like you you have said like we divide the array like in two from the median value right yeah so like if if like when we reach the top and if we have the the left sum and the right sum that that are equal so how do we identify that like which sub array we'll have

to take like in any case like if just see this if left sum is greater than or equal to right sum or left sum is greater than or equal to cross sum so obviously it will be left sum so this equality sign is always there so we will solve either if or else if or as getting equality we can take any any somewhere you are saying like or it has to be always left sum i'm saying if you go with strictly with the algorithm so always the left sum will be taken up. Okay. Otherwise you are free to take any because ultimately you have to give the output, highest output. Either it is appearing in left side or right side that is, both are correct. Both are correct. Okay. Got it, sir. Thank you, sir. But if you strictly follow the algorithm then you have to choose the left one. Okay. Thank you. Yes, Suraj. Good morning, sir. my question is similar to the last question can you re-explain the problem with the for variable as we used to do current

sum, subarray, maximum subarray and start and end point so it will help me to understand how the subarray will be created as we maintain the state on start and end variable if time allows sir no no so I am not getting exactly what so you want that how to identify the indices yeah if you maintain in start and so just simply simply understand it whenever there is change in value we store the index okay or whenever there is increment in value we store the index so from one to four there was a change i store that index and every time we are saying what then it is five then it is six and from six to again it is reducing to 5 if you see in a maximum so now 6 status is maintained because we are never getting a maximum value so whenever there is an increment and we store that index and whenever there is change in value that is 5 to 6 we store the index so this index and this index will be the subarray now what i am saying sir can you can you if time allows can you re-explain

with the four variables so it will it will better for visualization and understanding yeah yeah so what i will do just give me a moment we have minus 2 1 minus 3 4 minus 1 2 and 1 i am just taking okay not going beyond 1 2 3 4 5 6 7 so current sum equal to max sum equal to minus 2 we are starting with the i equal to 2 let's say current sum equal to max of current element that is 1 and 1 minus 2 that is minus 1 so we are getting 1 as this max sum as 1 okay not calculated i equal to 3 so here we have stored index what this is the index variable i equal to 2 okay now in the here the next step what we are doing current sum equal to max of minus 3 comma 1 so of course it is 1 max sum again 1 then i equal to 4 we are storing what current sum equal to max of 4, 4 plus Sir, sorry to interrupt you. Can we initialize two more variables so we can manage the start and end variables? One is index and one is the sum value.

So here index was 2 and the max sum already we are storing. no need to store here index is 3 and here we will get 4 comma current element and current sum that is 5 so we will get 5 and max sum is equal to max of current sum and this one so we will get 5 I guess I am skipping something so here also we are storing so what basically happening there is change in the value. So, that is from 1, we are getting 5. So, that index I have stored as flag 1 or something in index array I have marked as 3. Okay, where there was a change in the value. So, I preserved this value. Earlier, it was 2, then 3 and now we are storing the index number 4. Similarly, i equal to 5, we will go and at that point, whenever there is change in the value, we are storing those two values so for example here i was storing index 2 3 4 5 so i will just store 2 2 because the value is not changing okay simply understand i and t is 2 i and t is 2 because

value is not changing but here when we whenever we get 4 so i and t become that index that is 4 okay and i have stored it okay now we are moving ahead moving ahead when we are getting 6 first time then also the value is stored so two values we have stored four and six because these are the two point where there was change in the value and then we will declare that is the severity getting okay sir got it still confused then i will because other may be having same problem so what i will do whenever there is change in value i will store that index so whatever the mechanism you apply that is totally up to you whenever there is change in the value of max sum then we will store the value so just i am writing one logic whenever whenever there is so whatever logic you write that is up to there is change in max sum we will store the index okay or we can say that in start there will be a value that hold the

that is index start that is the starting point of the subarray point of subarray and there will be end finish that is the end point of subarray and this is nothing but this is change in the value and this two index will treated as the start and end of the subarray whose sum is 6 or something getting it? yes I got it next is Sreya Sreya I have enabled your mic hello sir good morning yes yes you are so my doubt is that for the dynamic programming approach the result that we are getting is a maximum sum and you told if we have to get the summary of that maximum sum we will have to store the indices but that is not provisioned in cadence okay so but if our main aim is to find the subarray of that maximum sum will the dynamic program approach still be better if we are storing the here our target is to identify the max sum sum of the subarray every time of the sum of the subarray we have to identify it so this cadence is doing

and we can store key if there is change in the sum then that is a start if there is change in sum, that is the end. So, the two comparison will max to max come. So, that will not hamper this performance. Okay. So, even if we have a separate algorithm in this, if we add in the cadence algorithm to find the sub-L. So, that will not impact the computational complexity. Okay. So, but in each step, what we are seeing is four conversions are making. So, the computational complexity will what 4n getting 1 1 1 1 1 and for larger value of n the 4n becomes n yes sir so for its model let's say n is 10 or 6 or 4 so obviously you will find a bigger value but when n become very large then it will tend towards order of n okay yes sir good morning sir so basically I have a question regarding the dynamic programming and divide and conquer I don't know might be this is the good time to take that question just ask so basically in divide

and conquer we know that we divide the problem into sub problem basically for solving the problem we used to do the bottom up approach right yeah and in the dynamic programming it is also the same we are following the bottom up approach yes and in divide and conquer if we see we have the extra spacing that we take every time we try to solve a problem this is what happens in the dynamic programming as well so sir i don't so how can it become faster and memory inefficient in comparison to divide and conquer That's my question sir. Good, good. Now, if you see faster is clear, I guess from the last example, you need not to worry about it is n, it is n of log n. Now, in dynamic program, what we are doing, we are solving this problem, storing the result in temporary storage, solving this right half, temporary storage then. So, what we are basically doing, this problem I solve, this problem I solve, now this is,

so these results are gone. Then I will solve these two problem, then this, the temperature is gone and here we are getting what? We are getting the result. Or if I want to explain you a simple example, that will be good to differentiate, so in divide and conquer we are having this problem. And we are solving like this one. We are going ahead storing the result in temporary, storing the result in temporary and aggregating here. Then this, this, aggregating here. Then this, this and aggregating here. So that I hope you agree. But in DP if I solve this, this, this, this. So what I will do? I will solve for this label, all the outcome, O1, O2, O3, O4, then I will go to O5, O6, then I will solve O5, O6, then I will go up O4. So, at every time, what we have, we are storing all four output, then we are storing all two output, then we are storing last final output. So, how the difference is arising? Here, we solve one part, then we will shift to next part, solve it, store in temporary

and go ahead. Here we will solve all the sub-parts simultaneously, then we will go. Getting, first time we solve this, then this, then this, then this, then among these, which is the best case, then that will be propagated upward. And here what we are doing, we are solving one half, other half and getting back the solution, then this half, this half getting. So at each instance, what we are doing, we are taking one part and here it is what it is doing, it is solving all sub-parts, then moving ahead. I guess you got the difference. But we are moving label by label, label by label, label by label in dynamic. And in divide and conquer, we are solving one left half. Then we are shifting to the right half. Deference, I guess you agree. I will go like this. I will go like this. And here we are going this, this, this, this, this. So every time we are storing the previous result, that's created the memory in FSC.

But here we need not to store the result because we are already getting a solution at each step. okay so basically if i get it properly it is like dynamic programming is managing the whole set of the backward uh like yeah yeah yeah yes and while the divide and conquer used to like forget it of what has done it's just previous operation yeah yes yes yes okay okay so thank you sir that was my doubt about so we'll take the doubt in another two three minutes taking a break and then we will start the next part of this I am taking your doubt yes sir can you please go back to that first example yeah here in this case we considered the starting point of the subarray as i equal to fourth index because there is a shift from ms from 1 to 4 right similarly we have a shift from i equal to 1 to i equal to 2 right from the initialization to i equal to 2 minus 2 to 1 why are we not considering that so we have just started with the first index now where the maximum this is the first maximum this

was just the you can say just the previously stored value and here so that is again i'm saying that is not the cadence you can you can use your mechanism what you can develop for storing the indices okay that was one of the assumption i was considering okay and another mechanism you can take of of getting this value so that is not the part of dynamic programming that i was supposed to provide so you can use any of the mechanism of the thing that you can adopt for identifying the index but as such cadance does not have any provision of storing the indices of the server it just give the answer of the maximum value getting okay okay uh yes rithika now you can enable your mic if your doubts are clear so those who have raised hand can down their hand as well and if they someone has genuine doubt then i will take it up uh yes rithika i have enabled your mic you can speak okay uh yes because you can say good morning sir good morning sir i have a

question around time complexity so since we we are trying to find out the max sum for that we have the time complexity of order of n which i understood but if we want to retrieve the indices to get the sub array so isn't it the time complexity will change because we have to compare each index with the prior index so that the that i'm saying key in each step we are getting more comparison so it will be let's say 4n or 8n okay but if n become very large then that 8n will shifted to n so that i'm saying i'm not saying it is always n when we try to store the index so it will also impose some comparisons okay and that will not be the you can say a for loop is running some if statements are running so that will impose let's say one complexity in each step so it will be four and five and six and or something but ultimately it will be n okay so for each comparison it will be order of one yeah and if in each step yeah

yeah but if let's say n tends to infinity or if we have a huge array in that case then then 4n will become the yeah n okay okay got it thank you okay i hope the doubts are clear now sir i think rithika have a doubt she posted in chat uh her mic is not working so she was asking how cadence follow bottom-up approach so uh in uh cadence what basically we are doing we are moving from one this to this so we are solving sub problems like first two index so if you see first two index so you will find maximum value is one then first three index maximum value is one first four index maximum value is four so that's why incremental steps it is identifying step one step two is we are getting the value of maximum so dynamic programming uh if you see in a different form or a different angle so what basically we are doing we are storing the previous value okay so we are storing the result of third we are storing first second and third to get the

result of four and so on so every time we are adding one by storing the previous result so that is how it is working so because this hierarchical arrangement is only possible in tree-like problems okay but here we are considering a problem where we are getting we have to identify the max sum value and that max sum value we are getting iteratively by step by step. In each step we are getting the max sum, if we consider 2 element max sum is this, if we consider 3 element max sum is this, 4 element max sum is this likewise, so that we are moving. So by using the previous result we are moving. So I guess we can go ahead and no worries when dynamic programming come in its full fluid form then you will the more clarity will come okay don't confuse so two three thing important importantly i want to convey one is this cadden algorithm will just give you the maximum value if you want to get the indices you have to perform some retrofitting

over it second thing is that the dynamic programming is itself a complicated or bigger topic so when we will go with the dynamic programming we will discuss again these definition and other aspects. So, at that time, it will be more clearer. So, do not worry about that, I will surely cover all those things. Now, there is one more interesting thing and I hope everyone understands that is matrix multiplication and there is one known algorithm that is called illustration algorithm. So, that we will see how it reduces the computational complexity from n to the power 3 to n to the power 2.81. Okay, so though it looks very less value, but in a longer run or a bigger computation, even this much decrement matters. Okay, so if we perform basic matrix multiplication, we are using primarily three loops, that is loop inside loop for, then inside that there is another for, inside that there is another for. So, basically n into n into n, n cube is there and if I take an example, I had a space, okay,

simple matrix multiplication if I perform, if I take two array, one is this one, a11, a12, a21, a22 and next array I am taking b11, b12, b21 and b22. If we simply go for a cross b or a into b, then what we will get? a11 into b11 plus a12 into b21 and here we will get, I hope everyone understand matrix multiplication. If not, then just see what I am doing. A11 plus B12, A12 into B22, okay. Similarly, here we have A21 into B11 plus A22 into B21, A21 into B22. So, this is how we will get a matrix C that will hold the value of multiplication and in multiplication we know that n into m and m into k is allowed where row and column should be matched. So, what we find the number of multiplication if you count, multiplication you count that is 1, 2, 3, 4, 5, 6, 7, 8. So, total multiplication is 8 and here with the matrix order was 2 cross 2, 2 cross 2. So, it is basically n into m into k, that is the total number of

multiplication we follow that is 2 into 2 into 2, that is 8. And therefore, its computational complexity will be order of n cube, that is 2 cube is 8. Similarly, if we have 4 cross 4 matrix, then what we will get? 4 into 4 into 4, that is 4 to the power 3 and so on. So, if this could be the scenario, then we will get the computational complexity in order of n to the power 3, that is n cube. So, for simple matrix chain matrix multiplication, we will get always the computational complexity of n cube. So, stressors introduce a mechanism that says, despite of getting 8 product for 2 cross 2 matrix, what I will do, I will perform these operations in 7 products, 7 products by increasing addition and subtraction, by increasing addition and subtractions. So, what he has done, he had defined some steps, he has defined some steps. Just a minute, there is a small doubt in this or we can say there is a small change in,

So, what he say, if you have to multiply A and B, so rather than directly going for A's multiplication, perform these 7 steps, then these 4 steps and ultimately you will get the result. So, now you can see number of addition and subtraction has increased, but number of multiplication has reduced by 1 and so total number of multiplication here required is 7. okay now understand one more thing so here is this is a plus sign okay so I have corrected because let's say you start solving then you will not start so what what is the issue here rather than doing this eight product I will do seven product so now one question is sir how this is a divide and conquer why we are studying here so now I will show one more example, if I had a 2 matrix of order 4 x 4. The important consideration in stresses multiplication is that the matrix should be square matrix and they should of order 2, 4, 8, 16 and so on. 2, 4, 8, 16, 32, 64 and so on. So, now what we can say that a 4 x

4 matrix is nothing but it is 2 x 2, 2 x 2, 2 x 2, 2 x 2 and similarly another one can be what? 2 x 2, 2 x 2, 2 x 2 and 2 x 2. So, this is how any bigger, so 8 x 8 will be combination of 4 x 4, 4 x 4 that is ultimately a combination of 2 x 2, 2 x 2. So, every time when we get a 2 matrix, let us say we get the 16 cross 16, we will first convert into 8 cross 8, then 4 cross 4, then 2 cross 2. And when we reach to a multiplication of 2 cross 2, so rather than going a normal multiplication, we will choose these 4 steps. So, let us do one problem on this and how to check whether this algorithm is working or not. okay so let me add one slide and then we will solve okay already there is one slide equal to one two three five one matrix i am taking and v equal to three two one three simple taking Then I will do a cross or a dot b that is what it is 1 into 3, 1 into 3 plus 2 into 1, 1 into 2 plus 2 into 3. Then here we have 3 into 3 plus 5 into 1 and 3 into 2 plus 5 into 3.

So, what ultimately we will get? 3 plus 2 that is 5. Here we will get 2 plus 6, 8. Here we will get 9 plus 5, 14 and here we will get 6 and 15, 21. So, this is the resultant. So, let us solve this problem via stressors. So, we will calculate P11, you can refer. So, that is A11 into B12 minus B22. What we will get? A11 is 1 into B12. B12 if you see 2 minus 3. So, here we will get minus 1. P2, sorry, this is P1, not P11. P1. Now, product 2 is what? A11 plus A11 plus A12 into B22. So, what is the value? A11 is 1, A12 is 2, that is 3 into b22 is 3 that is 9. Now p3 we are calculating so that is a21 plus a22 into b11 So, A21, A21 is 3, A22 is 5 that is 8 into B11 is 3 then we will get 24. Now, P4, P4 is A22 into B21 minus B11. So, what we will get A22 is 5, B21 is 1, B11 is 3. So, minus 2 minus 10 we are getting P5 equal to A11 plus A22 into B11 plus B22. So, what we will get? A11 and A22 is 6 into B11 and B22 is 9. Sorry, 6 again 3 plus 3 is 6. So, 36 we are getting. Okay.

Then P6 is equal to A12 minus A22 into B21 plus B22. So, what we will get? A12 is 2, A22 is 5. So, minus 3 into B21. B21 is 1 plus B22 is 4. So, we will get minus 12. Now P7, the last one. p7 we will get a11 minus a21 into b11 plus b12. So, let us check. So, a11 is 1 minus a21 is 3 minus 2 into b11 is 3, b12 is 2 that is 5 then we will get minus 10. If you now calculate c11, C11 is nothing but P5 plus P4 minus P2 plus P6. So, what is the value? P5 is 36, P4 is minus 10, P2 is minus 9 plus P6 is minus 12. So, if you calculate also 36, 19 minus 31, So, we will get what? 5. C12 is P1 plus P2. So, that is minus 1 plus 9. So, we are getting 8. C21 is P3 plus P4. So, what we are getting? 14. And C22 is P5 plus P1 minus P3 minus P7. So, what we will get? P5 is 36 plus P1 is minus 1, P3 is 24 and this is P7. P7 is minus 10. So, it will become plus 10 because minus minus will be equalized. So, this is 25 and that is 46.

so ultimately we will get 21 as the resultant and if you calculate all then then what we will get 5, 8, 14, 21 that is the resultant and this is as equivalent to what we have done. So now a question arises here the number of computations seems to be more than that what it is performed in the simple matrix multiplication but here the dominating operation that is multiplication is reduced to only 7 and that is the beauty of this approach and when we will solve the recurrence relation, then we will identify how the computational complexity reaches to n to the power 2.81. One more thing that we should remember that in Strasson's algorithm, if a problem Tn is given, then what we are doing basically, we are breaking into 7 subproblem of n by 2 size. Okay. And n square will be the number of, because we are multiplying it. Okay. So, where we are getting n square as the multiplication aspect 2. So, this is the

recurrence relation for stress and logarithm. Okay. And here it is written. So, via master's theorem, we will try to obtain how it is n to the power 2.81. Okay. So, if anyone has query, i'm ready to help so rakis just helping me out in taking the queries and then we will go ahead so for saharab i'm enabling the mic saharab your mic is already enabled so you can ask so basically my question was about this i think it is a divide and conquer algorithm right yes yes so so anything that would be a large matrix would be uh ultimately bring down to two cross two and yes yes yes and sir there is one more question that i have i want to just get the logic of it like in normal multiplication we know that row and column wise we will be doing the multiplication to get the first element so i want to get the logic behind this the formula derivation of like t1 how it is what is the logic behind it how it is derived to this okay so that okay these seven

steps were given by the mathematician Strassens. That's why its name is called Strassens algorithm. So what was the logic? What is the proof? So in this Corman, I have tried to identify but I don't get it. But if you get it, then that is good. But Strassens has somehow do this, these seven step if I perform, then I perform this C1 and C2, then I will get the result. So that was the logic behind it. So now you can prove it because both the things are available but at that time how he has done that is great so we are studying now oh no yes sir so only my question was like the as we know how to derive like in normal multiplication we know it is like row and column what we have to do similarly i just want to know if there is any logic that is sitting behind it where we can get like p1 how it is derived or p2 how it is derived no no so the derivation part you can just look back in the core man okay if i get i will share

you in as well but primarily this is why this is this a p11 p22 is coming this is has been researched out by stress only okay so and there is one another last part of it like i want to get that recurrence relation you have told like plus order of n square that is the last portion i'm not getting about how that is multiplication when we when we do the multiplication component of of of this one so that will result into the order of n square because if you remember in case of merge sort quick sort and last algorithm maximum summary we were doing a comparison and whenever we do a multiplication then there will be the multiplication of two entity a and b rather than comparison so it will always result into the square term and from where it is coming so you have to again refer to the algo part of this station so these are the steps only and whenever we perform multiplication it is order of n square always okay sir thank you sir so rakis provide access to other as well

good morning sir good morning sir in this straw's algorithm number of row n's columns should be equal is it the mandatory condition this is this is mandatory the order should be 2 4 8 16 and so on and and then only we can apply this okay so that's the second question that whatever we are doing and now uh maximum sub array now this dynamic programming before that whatever i think we are doing for space uh complexion deduction time and all those things yes or or we are doing this so that the problem can be solved faster yes kind of so so do we have some means slide or some document where we can see that the comparison means with this maximum sub array this complexity has reduced to this order and so actually what happened dynamic programming cannot be applied everywhere okay or even divide and conquer cannot be applied everywhere so there are some set of algorithms that's why i was saying maximum sub array is the you can say very good approach

because all maximum paradigms are existing simultaneously but you will find no none of the algorithm that is so we later on we will see greedy and dynamic programming so in that we will find algorithms are comparable but here for example there is closest pair problem so their dynamic programming cannot be applied so there are some set of problem where dp is not applied so we have to go with the divide and conquer if both are not applying then we will see the mechanism of brute force so that that is the thing yeah yes sir that is my answer so whatever limitation you for this approach we can divide and conquer with this approach we go brute and force with this approach we go this all about do have some any link or something so that we can miss at least I go through and able to kill your this is the condition come I will go with the brute and force this is the condition come I will go with the divide and conquer so that's

the thing and if I be the divide and conquer this is the complexity come so this is the ask yeah so one thing is that when you identify a problem where there is a provision of divide into sub problem if there is a problem where we cannot go in the division mode okay where we cannot break the problem okay so in that situation we will never go with the divide and conquer okay similarly when there is a provision that we can solve smaller smaller problems at each level and concatenate them to get back the or in the we are in a situation if we store the half solve problem result and we will get the full result then their DP can be applied but if there is no chance of storing the previous result or if still we store the previous result that is not fruitful for in the next step then we will not apply dynamic programming there so that kind of things are there so a problem that can be solved by divide and conquer should hold or even in dp

so two important if we can make recur recurrence relation for or a recursive variant then then only we can apply divide and conquer and dynamic if we unable to make this recurrence and structure then we cannot apply dynamic programming or dp now in dp if it is possible for storing the previous result to taking the next step decision then dp is applicable if it is not possible to store the result or or or breaking the problem likewise then we cannot apply dynamic programming that so these are the studies so if you make recurrence relation fine so you can apply dynamic as well as dp if you store previous result to get the next value so that is dp if it is not then you have to go to dynamic programming both are not applicable then we have to use brute force which kind of

