# DAA 23 Aug Lab

- **Course:** 5101 - Design & Analysis of Algo
- **Recorded:** 2026-08-23 00:00
- **Transcript:** 855 segments, 13017 words
- **Made by:** deepdml/faster-whisper-large-v3-turbo-ct2

---
Going through the, just start going through some pseudo-codes, not writing, jumping into the code directly. Okay. You just start with the pseudo-code. What do I mean by pseudo-code, if you can understand? Why do you delete? Yeah. I'll go to the playground. Morning, so please. Just let me say I'm asking. Okay. Spend me some time. Okay. okay yeah let me say my spend first and let me share you the today's link also okay uh this is your link go through the link okay if you haven't created your account please create one account okay register with the today's assignment okay i don't know why these guys not running from external so today's class is about uh um you can say about the stack okay uh not really stack but the array also okay so this kind of way you can say question tricky questions okay uh yeah so we have to the first question is like okay so what we will do today okay we'll do this question okay so this is the first question i'll give you the brute force answer

okay and another one i'll give you the optimal answer okay now i'll i'll go through this question okay i'll give you the solution of a brute force answer okay and you have to find out the optimal answer uh in that you have to solve it got it okay so this is a simple question only there is no any there is no any like rockets and things okay someone is submitted also okay oh how's the day going guys like good why eight links because people are asking like many times okay that's why i send it eight times you want ten times i can send you one two more times okay so let me go to this link by this not scrolling okay what's the last one okay so did you go through the question what the question is saying and what you need to find made it sit full screen okay yeah no i'm going forward i can't i cannot able to join more guys okay you guys join in from this thing okay so yeah let's go through the question what

this question is saying hi lakshmi harika but i'll take the question in the middle of the station okay please keep your uh hand raised okay uh but i'll take yeah so okay so problem statement is you are given in integer okay integer of array n of size n for every element in the array find the first greater element to its right side okay correct we'll take one example and we'll see what this question is saying okay and if there is no greater element to the right you will return minus one okay and your solution should be uh use a stack to efficiently find the answer okay we'll use stack but in the optimal way first we will go through the brute force brute force way okay like how our mind is selling to the question like let's say let's say you first see the question how your mind is telling what should be the approach to this problem okay we'll go through first that and then we'll go through the optimal okay what would

be the our worst case and how should we find the optimal for that okay so the input format will be okay the first line uh says as it is like a contains uh it is an the size of the array okay the second line contains n space separated integers representing the array okay the constant is given okay perfect and then the output format will be print and separated integers the ith value is output should be first element to the right of a of i that is greater than a of i okay uh we'll copy we'll copy in this only okay so first uh we given one uh like a six six six elements are there okay inputted and some output is we'll understand the problem first okay and then see okay okay so first is telling that we third element okay so i copied this okay so we need to look at only its right side okay as the question said correct so if you will look into the right side what what's the right side of four five two eight uh five two ten eight seven correct so the first greater uh then four is five

right so i'll write like this okay so this is the first greater okay so this is the first greater correct now we'll take now we'll take the five how will you take five okay who is the right side of it okay uh so we'll list out the all the elements also okay so let's say let's say you are looking at uh right side of four then what are the elements five two ten eight seven correct who is the next uh who is the first greater element is five correct we found out okay so now take the five okay two ten sorry eight is ten minute seven okay now two is not greater than five right who is not greater than five correct who is the next greater than ten okay so five then 10 will come correct now we will repeat for this every element okay so like uh then for what element is there two okay so who is the next greater okay is the next created 10 only okay now 8 okay sorry 10 there is no greater element right so if there is no

greater element then we will put minus one okay now comes to the eight okay there is greater element to the right side no we'll put minus one okay now seven always minus one because we don't have any right side element correct now there is a simple approach we can find out okay for every element we have to start looking for the next positions until we find a greater element correct so uh why i said like that because in the case of a 5 okay in the case of a 5 we did not find out like a next greater element that's why we look for the next greater element okay which is 10 okay so until that we have to find out okay who is the next greater element and list down it okay so if you will see like that if you will list down this okay what is the problem statement now if you will list down it then 5 then 10 again 10 okay because uh 2 is there so uh 2 is there okay then uh or 2 because 10 is greater element okay in case of a 10 there is no greater element that's

a minus 1 in the case of a 8 there is no greater element to the right side so that's why minus 1 okay this one in this case of a seven there is always a minus one okay why there is no any right side element okay in the case of a you can say uh this example also you can take and you can find out this okay so what should be the uh like a answer okay so as a bhavna you can like ask okay so how to write the code okay we'll first go through the pseudo code first we will not jump into the python port okay we'll first jump into the simple solution okay how how should we solve it okay so we are going through this list okay so if we are going through this list okay then for sure that will will like a uh apply the for loop okay what came what the bare minimum requires the for loop okay because we are going through this list right so how i will write for i equals to 0 to n minus 1 okay something like that i'll write okay then i'll take one

answer okay which will compare which will like a simple storing is minus one okay because as we know that like in the last element we are always getting the minus one okay so that's why i'm keeping by default as a minus one to the answer okay i'm taking one variable okay the minus one okay and then and then what i will do i will check okay for the checking purpose i'll take another variable okay i'll go through it okay i'll take another variable which is i plus one okay this is the simple approach i'm telling okay so first element we are comparing with the second element that's why we will take i plus one okay we are starting from i plus one okay that's why which will go up to n minus one okay i'll keep column over here okay okay why these guys are joining from external i don't know two to five guys okay i admitted them all okay Now, I will go through the question. So, I am taking another variable, another forlook for the comparison.

I am taking from the i plus 1. See, this one I am taking from the 0 because index is always starting from the 0 position. I am taking from i plus 1 because we have to compare with the second element, which is right element. That's why I am taking i plus 1. Now we will compare. How we will compare? if a of j a of j means this is the if the second element is greater than okay this is greater than the first element the answer answer will be a of j okay correct because if if that is a greater then we are taking answer is a j then sorry then we will break this okay because we got an answer we have to break out of the loop that's why i play the break statement then what i will do i will print the answer immediately okay i'll print the answer correct answer yeah sorry guys i was attending another call also okay that's why i'm bit of a diverted okay oh okay so there's the break uh that's why i put okay now you understand okay if you if you still cannot able to understand

we'll try run this brute force approach okay so let's say i'm taking the same example what's that example is four five two eight ten seven okay so okay uh for the simplification approach okay i'll i'll also keep the index uh zero one two three four five i think yeah the value i will keep okay okay i think so value okay i'll keep as four five two ten eight okay and seven okay this is the index number and these are the values okay so now what we are doing we are taking first i value okay i equals to zero okay loop started okay now the current element is current element is r4 okay now it will say look at the right okay why it will say look at the right because our second for loop is also started which is starting from the i plus one if i is starting from the four then i plus one should be zero plus one which is five okay now it will compare uh compare with five okay compare with the 5 okay so answer is so answer is 5 okay if you want to show the comparison i

can show that also okay so answer is 5 simple okay now we stop the searching okay why we'll stop the searching because of this break we got an answer that's where please stop hey we started the recording right yeah we started the recording so okay we stop the searching okay because of this break statement okay now now what now we will print the answer okay print five okay then i equals to one is started okay because we break that so now current element is five okay then it will first look at two okay is greater than five will say no okay i am writing in a simple manner only english word so that you can understand guys okay now it will look at for the next statement okay so according to this okay if this is not greater i'm i'm not writing any else statement also that's why it will not break okay it will not break because this condition is not satisfied yet okay that's why it will go to the another i plus two okay it

will go to the another i plus two okay and then it will look it for okay it will look it for next element which is 10 okay if it if this is a greater it will set uh yes okay sorry yes okay so so answer is five and uh we'll stop the searching as usual because of this break statement okay and then we'll paint the 10 okay simply if we will go to the next statement okay i i equals to 2 okay then go to the i i equals to 2 then what's the current element is 4 4 5 2 okay currently is 2 uh 2 and then uh it will look look for the right okay it will look for the right okay then it will ask like it 10 is greater than 2 we'll say yes okay then stop the searching okay because of this break statement okay and print the answer print the answer is then okay what we have found out okay now in case of a 10 okay so what by default answer is storing by default is is it so by default is storing as a minus one correct because we have to show minus one right

that's why i'm storing so uh in cause of a 10 uh what what is that i equals to three okay okay now currently current element is 10 okay it will look at the right okay when it will look at the right it will what appears 8 okay it will compare if 8 is greater than if 8 is greater than 10 it will say no okay then it will it will like a go through another okay i plus now i equals to i equals to 3 then i equals to 4 i i plus 4 okay i plus 1 basically okay it will go to the i plus 1 and will also check for the 7 okay because that condition is not satisfied so that way it will also check for the 7 okay does 7 is greater than the 10 it will say no okay don't choose okay so nothing is greater okay now simply so answer is it will not even go to the break okay simply it print the answer so answer is minus one so it will print minus one okay what we have printed so far now five okay uh there is a five then there is a ten again there is a ten okay now it's minus one

okay same it will go to the eight eight will uh it will be the current element it will look at the right side right side will be the seven okay seven is uh seven it will check okay it will not greater okay it will bring the answer as a minus one so this is the simple approach okay so what should be the python code okay okay okay so uh how how we will write uh if we are going to write in a python uh something code so first we will start i can write over here but let's write over here okay so yeah so i'll write one next creator okay la i'll write one function okay so this is how we define the one function okay so yeah sorry so this is how we define one function okay uh using def keyword okay by uh by taking smaller element with the underscore okay you can you can define one element okay we can define and then we will take the array length okay by array okay then we will simply let's say we are storing one answer okay answer will be as i said okay minus one okay

two cross n okay so this is an array okay so basically this is an array okay which is storing minus one okay multiply with the n okay let's say uh the current uh what we have taken the example as a six okay so length there is six it will simply save the array of length states with the elements minus one minus one minus one okay so there is a there is a we are storing right we are we are we are printing also answer okay in the area format okay so that's why we have to take the answer okay whatever the result also you have to take the eric so that's why i have taken the eric correct then then for i in a range for i in a range of n okay what i have taken the above okay in the pseudo code okay so for i in a take of a range okay it will by default start from the zero position and it will up to go to the n minus one that's why i have taken in the this format okay then what i have taken for j in a range of i plus one i have to take right so i plus one i'm starting

from i plus one till n till n means it will take from n minus one so range function is always going from going to n minus one value okay by default so that's why i am taking this as a n okay then we'll start the program simply what i have written array is hey one second i didn't take in the i didn't take in the array as input right so how i can define so this is how we take the input also okay in the function okay then if area of j is greater than area of i what i have written in above okay then you simply have to store the uh answer of i answer of i equals to area of j what we have found as a answer okay correct then what i have written break break okay then you have to return the result also okay here you will return the result okay so let's say i'm taking this as a four five what i've taken two uh so a tenth habit then eight seven then i will simply write print i will simply copy this okay okay so i'll simply copy this okay and then i taken as a

array as a input okay and then it function with a this function will appear okay as an output okay so this is the simple approach okay which you can print okay now what's the problem okay if you see uh i think the last class who is asked like a to to show the something complexity okay right so what's the complexity if we see okay so okay so there is a two type of complexity in the programming which we say okay so first is time complexity okay now another one is space complexity correct so no issues you cannot want to see so another one is space complexity okay so what is the time complexity how we how we calculate the time complexity okay so the many times you the many times you apply the for loop in the for loop okay so the your time complexity will be appears okay so right uh if here if you see okay there is a two for loop and that two for loop is one inside another okay so and what that is up to going n

minus one times okay so we are removing uh we are like it it's going up to the zero to n minus one time correct if this is going to the 0 or to the up to 1 n minus 1 time okay we are removing the constants constant means minus 1 okay we are rounding this value as a n okay correct now if you will see here i plus 1 okay why i am taking uh why i am removing the constant i'll i'll tell value okay i i-1 and it will also go into the n-1 okay so let's say so tomorrow uh today we are taking the value as a six okay so that's why constant is matter okay so if we are taking tomorrow as a 10 lakh 10 million okay so something like bigger values okay so this will not matter in the long run okay so that's why we are removing the constant and we are keeping it as a simple okay if i will remove this constant also so overly it will go into the n values okay so if we will calculate this time complexity okay n cross n okay it is coming as a okay n cross n

okay which is big o value of big o value of n square okay so whatever you are putting okay so let's say your n value is 36 okay what is the n value n value is your length of the array so let's say length of your array is like a 6 okay this loop is will run 36 times okay so let's say you are taking uh something length as a 6 so it will loop run as a 36 second or like a 36 milliseconds or something like 36 times it will run okay so this uh is it not uh right approach like a big of n square is coming okay something bigger values okay and what what is the space complexity as i told you like a big o value okay big o value of n square that that's how we write okay so big of n square we are we are writing okay what is the space complexity big of n how how we know that okay so in this case we are taking extra space okay rather than the array okay rather than the array we are taking the extra space of a result also result function this one

okay so what up to it is going up to the n length we are storing okay so that's why it is going to the big of n okay so something like that you can able to imagine okay so something you are storing in a memory okay to the to your array okay you are asking for another n memory okay so that's how it is calculated as a space complexity hope i am clear to you okay so this problem is we are repeatedly scanning the same elements only correct so so what should be the like a smart approach like a uh and where is the problem is lying okay uh where the problem is lying i'm telling you okay so let's say tomorrow you are keeping your uh greater value here something like this 20 okay so the loop is okay so we found the greater element as a like this okay so let's say you are keeping your like a something greater value in the 20 okay here it will for sure it will run up to the n square time okay because 20 is here it will calculate to

this okay will again calculate to this 10 will again 10 will again go up to the here and it will calculate it is a greater or not correct wait a second guys yes so it will calculate it will go go to the up to end time okay so what should be the better approach okay so can you guys tell like what should be the better approach how how we can do the better so hint is like you can you can use the stack okay so let me introduce also to the stack okay so in the simple manner stack is nothing but but you can see daily to daily life like a book of stack okay so something like the value which you are putting in the uh above element okay so the value we are put the value you are putting in the first element it will go to the last of the stack okay like a cloth okay you are putting cloth as a first element so it will go to the down then putting your another cloth okay it will go to its above okay then you are putting another cloth okay so this is kind of

a stack okay real life example stack you can able to imagine okay so you can able to use the stack also so can you tell me like a how you can able to use the stack and uh there uh there you have to compromise with the time and space space complexity okay so we can reduce this time complexity okay using this stack okay can anyone tell like a using stack we can able to do it but how uh nobody okay everybody is left i think okay so what's the current approach so so sorry what's the problem with the current approach okay so there is a repetitive work right so let's say as i said okay yeah sorry i was like looking at the second follow up okay sorry okay so what's the like a problem with this so let's say so array is something like this i'm putting random number oh why should i put random let's say something like this nine eight seven six 5 4 3 so this is the simple uh yeah 20 okay something like that i have taken okay for the

for the 10 okay it will check to its right side it will not found this like a in the row okay but this is found okay i have found out okay so that's the 10 check so for the 9 also it will also found out the 20 okay for the 8 it will also find out the 20 okay so we are repeatedly checking for the same element right so in this we are repeatedly checking for the same element so let's say uh this is the worst case i'm taking okay so it's the worst case with the element cannibal to come okay and you are checking up to the last okay repeatedly checking for the same element same element same element okay so what we can do we can we can store this element okay in the top of the stack okay so that it will not again check for the uh element so let's say uh the element is coming uh to the nine okay so it will check like oh so nine is not a greater element so it will remove the nine from this array okay so it will remove the nine from this array so it do not have

to check okay for the rest element okay rest of the element won't have to check so what do i mean like a i'll say you okay i'll say to you okay what is the simple intuition you can able to understand okay you understand like a step one we are removing the element from the stack okay because if we see the current element is not greater okay so we are removing the element from the stack something like that we have we are doing here so for that we have to start from the this right to left okay so we have to start the like a for loop from the right to left okay so we'll maintain the both of the things stack and the answer okay so let's say in the in the simple manner i'm taking okay so stack okay so there is stack is something look like this only okay so there is nothing on right uh nothing on the right side okay for now okay so for the element seven okay i'm taking no not taking this okay taking this only okay so i'm

taking this okay uh so for the element seven okay so there is nothing right so therefore well we are starting from by the way from this okay starting from the last okay so therefore the answer of five would be okay so again zero one two three four five okay so answer of the five will be store it is a minus one okay now post the now post the to the stack okay who's the seven seven with the stack okay now we will push okay so stack is something look like this will save like this now coming to the current element okay eight okay so for the element eight it will check for the stack first okay it will check for the stack so what it's right element is seven okay so right element seven it will ask and check okay so seven is greater than eight okay it will check okay seven is less than equals to eight right so seven is useless uh it will get to understand okay what i'm telling to you okay now pop it okay so seven is useless will crop from the stack because it checked right

we processed one element okay first and we see like its right element is nothing so we stored answer as a minus one okay now it is it is going for the another element okay which is right side of it okay again we are checking if that current element is less than or equals to the if the previous element is less than or equals to the current element okay if it is so that element is useless because that is a smaller so we will pop it now the stack is empty therefore answer of 4 is also I will tell you in the code also don't worry therefore answer is minus 1 now who's the 8 okay if we will push the 8 okay now the stack will be 8 okay now the current element will come uh the current element will come as a 10 okay so now it will again check okay is there anything in the stack okay so stack becomes nothing okay so sorry like we are checking okay so stack in the stack we have a 8 okay it will again check for the 8 is greater than 10 okay yes

then 80 is also useless okay remove the 8 okay remove the 8 okay and stack becomes empty again okay all right make all okay so stack becomes empty correct now uh so uh after that we will save the answer also okay so answer of three will be okay answer of three will be minus one again correct now who's the 10 into the stack okay now stack becomes then okay then the current element will come okay which is two okay two will come okay two will check okay if the 10 is greater than two oh sorry 10 is less than two we'll say no okay now what will happen 10 is so the 10 is greater than two right and because we are processing from the right to left the top of the stack represents the first useful greater element okay so we are only storing the top of the stack the useful element only okay so now so now answer of 2 will be answer of 2 will be 10 okay answer of 2 will be 10 correct we are we are saving also the array

also we are maintaining the answer element okay now we will push the two to the stack okay now stack becomes two okay now current element will come okay which is five okay now it will see the stack okay see the stack stack is two or ten so it will check okay this two is this two is like a less than five we'll say yes okay so two cannot be a match later right right so pop it now stack becomes only 10 correct now it will check for the 10 okay so if 10 is 7 5 is it so no okay so therefore the answer answer of answer of answer of one will be 10 now you should also push the 5 okay the stack stack becomes 10 or 5 okay so in the top element so this this one is a top element then this one is a bottom element okay so whatever you are pushing into the stack so 10 is the in the bottom side 5 is in the top side correct because stack nature is like that finally correct and then what the current element is current element is

four okay so current element is four so it will check with the five okay so now it will look into the stack which is ten and five okay now the top element is like a five is less than equals to four okay okay it will it will say true okay it will say no okay correct let's you know so five is next letter correct 10 is also next letter okay okay so therefore therefore your answer of a zero should be five okay so the moment you get your answer it will not check for the next letter because we have to check only the first grader right we are not going for the next grader when we are going for the next grader when we are not getting the first grader correct so now we will post the four also okay to the stack so 10 5 4 so now the final answer okay whatever you we are maintaining the answer it's like we'll go as a five okay then we are getting the 10 then we are getting 10 minus 1 minus 1 minus 1 okay so this is the main logic okay this is the smart solution

okay which you can see correct if you want to write the root pool uh sorry if you want to write the pseudo code for it how you can like for every element okay i'm writing in english only for every element we are going from the right to right to left okay you should write in this manner first okay you shouldn't write for that okay and then we are maintaining the while loop okay why we are maintaining the value and when we have to maintain the while loop when we don't know where is the end okay so in the for when we have to apply the for loop is you know where is the end is okay suppose you know that we are going from this to this okay you know the end okay in the while loop you don't know the end okay where it can go so in that state we can apply the while loop okay so we are maintaining one while loop well stack is not empty okay Okay, when the staff is empty, then the other condition is, and the stack top element, if the current element is less, then we will pop it.

Okay, we are popping this. Okay. Then, if the stack is empty, then we push the next element. So, if the stack is empty, then first of all, we are storing minus one. Okay, in the array. We are storing minus one. else okay answer will be the stack top only stack top okay if the stack is empty then we are assigning minus one okay if stack is not uh empty then we are taking the top element as a our answer okay correct then we are printing uh we are pushing to the current element okay current element to be pushed so like that we push the four okay the current element it will if there is any element it will check for the first first grader then it will check for the second grader if not found it will check for the third grader okay correct you found out so let's say uh another element is came like a six okay something i'm imagining okay so it will check okay next element for the stack this is on the top right it will check like a four

is greater okay no no no four is not a greater it will it will pop okay then it will check for the five five okay okay five is greater all right no five is not a greater again pop okay then it will check for the 10 okay 10 is greater yeah 10 is later okay then we will uh storing the answer as a 10 first okay here we are checking the answer is uh storing as an answer 10 and the stack top okay then we are pushing to the current element into the top okay so it will be removed both of the elements and six will be the remaining correct hope you got it correct if you are going to write the code for it okay how you will do then you can able to write like a okay you can able to write same like uh where i have written the code okay so let's uh don't pop this okay so let's say i'm writing next creator okay create an element okay then i'm taking one array as usual i have taken okay length of the array okay then i'm taking one result or an answer you can say as a minus one

then i'm taking one stack okay then i'm taking one for loop which is going from last okay for i in a range where you have to start from sorry you have to start from you have to start from n minus one okay we are going to minus one okay we're going like this okay uh i need a range yes i'll tell you what i've written here okay this is simple like a while stack and stack of a minus one okay zero for i okay then that dot pop correct if stack okay stack is empty here if means if stack means it will give you one if this will give you one negative number then it will come as a boolean value okay so it will come as a boolean value and it will save minus one correct stack and this one okay that got happened okay with the current element right we are appending current element we will write like this then we will return you will return simply the result okay result error correct okay so hope you're able to getting it okay do you want to try run of it okay okay

do you want try run of it okay want to understand by the way uh if you cannot able to understand this okay so range is range function is also can able to take like a three values okay so as as well like a one is a start where you are starting another one is a stop okay where you are stopping the another third variable is a stepping okay so the step is like a uh suppose your last in suppose your last index is like a n-1 okay so minus uh five okay so it will put as a five over here okay and then you are going to minus one okay up to up to last okay we are checking okay and then uh we are stepping out okay stepping out minus one okay so let's say if we will process this okay cannot able to understand then i don't have to go through it but why i have taken let's say we'll give as a like a we'll give as uh let's say i equals to five first okay i'm copying this okay 3-paste, 4-paste, 4-paste, 4-paste, 1, 2, 3, 4, okay.

So now this is 4, 3, 2, 1, and 0, correct. Now we are processing the elements, okay. So now we will process the elements, okay. We will process the element, the array of field. save first then it will go as a seven correct it will go like like like this okay something like this then you are starting uh from the seven okay then why we need as a minus one as a stop because python's race range is doesn't include this stop value okay so if like it means like a stop before the minus one you can if you feel like a confusing okay like for i in a range you can you are starting from like a zero to n minus one whereas you you are written like this you will return like this okay you will return like this then you are going like this sometimes minus two minus three one and zero something like that you are going you are writing this so yeah so this is the way you can like a like a solve okay so in this you won't have to check

okay so i have written this while you okay don't you think that it is going for like a and okay instead of we are limitizing it it will go for the only big open time okay so time context it will be big open time you have full any hands okay we are getting any background noise okay there is another net okay uh means allowing the mic okay meanwhile you can you can solve this okay meanwhile you can solve okay i'll give you what the hack man who who's who is rohan rupta by the way and he is unverified yeah so i'm giving you this okay this link okay how should we know the yeah i'm putting the lab link also okay yeah kindly please save the last link the reporting comments or in the model yeah i think in the model you can able to see right and about malik like you can able to know about your email id from your outlook okay if you go to the outlook and you will log in okay so for the login also you need general id right so if you will you login already in the teams right you can able to search by your

name if it is not visible in the model then we have to contact with us like uh uh contact to the sar uh like a professor because he's only he's only like a controlling this i don't have any access of admin okay uh about one question like a part where is that question wait a second okay yeah sure i'll repeat okay is that question of sanyam okay so sanyam you can absolutely use like a reversed okay uh and in many cases it is actually easier to read also okay so it is a predefined function okay so you can write instead of for i in a range of n minus one minus one minus one you can write simply as if for uh i in a reversed okay in the array so you can print the value okay something like that and print it means okay uh what type of what does it use okay so so it's using uh stack okay it's using both okay like array and a stack both it's using with the model if anyone is option they can able to answer and submit later

whatever to submit uh yeah so okay i'll i'll give you one link of excel state where all this lab links is mentioned with the date okay and the deadline also and you can able to be able to check it's possible to attach the lecture notes as we watch recording witness that you have to check for the module okay that you have to check for uh with the sir only someone said like a time complexity is a space complexity okay uh robin yes like a stack is you can able to use for the like a see we uh as i said we have to compromise between the time and the space complexity right so you have to either increase the time complexity or you have to do by the decreasing the space complexity or either you have to increase the space complexity see we have taken uh the stack as a another element right another we are storing as a space okay so in that case we are we are taking two space complexity two of kind of like a we are increasing with the

space right while our time is reduced space is increased okay robin so this is the some kind of trade-off you can do within this uh yeah uh explain this stack one more time does this lab contain the mark c yes what type of stack it is what do you mean it is like what type of stack it is yeah it's already 12 we will go monolithic monolithic is like a uh simply a stack where you can keep the element in a particular order either either in a increasing or a decreasing so we are not using something like that but but yeah it's formally used for the it's formally used for the monotonic type of a stack where you can use so what do you mean is like you can you you are increasing your space complexity right while you are decreasing your so that's the main main point okay okay let's move on to the next question next question is easy kind of things so you can able to do this you don't need to so this some kind of a sub array kind of things okay if you ever went to sub array

okay uh you are given one integer a okay containing only one and minus one okay and a sub-array is called balanced if the sum of its element is zero okay you can take as you you can you have to take as a sub-array okay and whose uh if it is a balanced then then it's had to give the answer is zero your task is to find the length of its longest balance sub array and you must be use the stack to help identify the matching positions okay so for the sub array what you can do what you are coming into the mind okay the simple input uh the simple input is like eight okay so which is uh we are taking the element okay So, the numbers are 8, 1, minus 1, 1, if you add this, okay, you are getting as a 0. That's why your output is 8, okay. If you will see this one, okay, if you add this, 1 plus 1 minus 1, something to the last element, your output is like a 0. So, that's why you are getting 6, okay. similarly this is one sub array okay so full arrays can be can able to become a sub array

okay and the part of the array is also a sub array hope you understand so how you can do it like you can use two correlates to count someone is given on prefix some solution for this you can use the prefix sum this is the optimal answer yeah you can use the prefix sum but but what should be the proof force approach first like a you should understand that some kind of things okay so what we can maintain is like a sub array okay so first we will maintain one then we will maintain minus one then we will maintain 1, minus 1, 1 will sum it and see okay, we are getting, no, no, no we are not getting, then we will maintain this, okay, up to this will sum, we are getting no, we are not getting, then we will move further, okay, we are getting, okay we are not getting so, something like that we can do correct so, we can maintain the we don't have to maintain, but we have to compromise with the time complexity here in the brute force approach.

One is something like here so let's say one so this one and this one one okay one minus one minus one six element is given. you are doing the plus kind of things okay so we are getting zero okay if you will add so the answer is six so this is the simple problem we are keeping okay now how you will approach in the brute force manner uh in the simple word what i can say is like you are maintaining one array okay array of a sub array you're going to one element you are adding it with the zero okay and then you are getting one first and then you are approaching to the second element you are adding it you are getting something okay then from the second element okay or like a you can you are maintaining uh that that only you are adding this and you are checking uh you are getting is or not okay and then something like that and we're going okay i'm writing full okay because this is six element minus minus one and then one okay uh one one minus one minus one

one minus one then you was you you are starting from the index one and then you are going like this so on okay so if you will write in this manner how you will write uh you are maintaining one max length equals to 0. Then for i in a range, okay, so if you will write in a group close manner, then I'll write like this for i from 0 to n-1, then you are maintaining one sum also okay variable then you are taking one another variable j which will maintain the i plus one value direct from i okay from j from i to m minus one chip then you are summing this sum plus array of j then we are checking uh another things if sum is equals to equals to zero then stop there and calculate the length how we will calculate the length we can calculate the length like this j minus i plus one okay okay up to this okay we can calculate and then the max length will be the maximum of max length and then current length correct so it will compare like a what is the maximum length okay previously and then it will

check for the current length okay then it will update the maximum length and it will return something like that we can do okay uh we need to also write down the try run for this there is no required right i have written like this you can able to understand so this is the manner how we are going first we are approaching this first then we are adding this then we are adding this this this and this okay you can write the python tool for this like you can maintain one length of the array and maintain one variable and you can do and what will be the optimal solution that that already given in the comment okay by the way while i'm explaining i cannot able to see the comments okay uh so that's why like i i missed your comments mostly you understand this um logic right i don't need one uh you can write the optimal solution for this and you can submit to that you can write this solution and you can you can do it just just one second guys

now i'll take the and i'll leave you by 12 40 or something you want sapna is not there i think sapna janget here is you have more doubts you already asked yeah i had one more more for a second it was like this it's like a question so for that means like i have seen it's like can you miss like better it means like where while explaining just give some this like real life example for that so people will better understand it which one which one the second one second problem there but you one minus one this one so you need uh some can you explain like what i'm doing with examples you explained the last problem is like can we can you explain this like this also okay okay you need dry run i think you mean that yeah okay okay i'll explain i'll take the some data and it's like yeah some example i'll take and so let's see okay let's take one example let's take that example okay i'll maintain one index here okay so that you

can understand what i am going for so uh it's not a optimal approach this is a brute force approach only okay we need to find out the optimal one so initially i'm maintaining one max length as i said here okay by the way this is the pseudo port not a python team okay so max length i'm maintaining first we are going to i equals to zero okay we are starting first okay now our variable is one at the i equals to zero which you can see here correct and see now i'm also starting my sum value right here then i'm starting my j again okay as as going through the code i'm i'm going okay what i've written here okay now add one okay so as i as you go through this okay what does it mean sum is 0 which is 0 array of j which is 0 okay array of 0 means which is a 1 okay 0 plus 1 is 1 okay so now add 1 which is 0 plus 1 equals to 1 okay so now it will check okay it will check it every time right it will check if the sum is 0 it will say no no no sum is not 0 actually

the sum is uh something about one okay it will not not go through the if loop okay now sorry to interrupt miss like if sum is not zero that it will pop it out what if sum is not equals to zero then it will pop it out right no no it will not will go through the if statement okay okay see what is the current sum now one right yes yes yes so what is the current sum is like a one so it will say if the if this is a zero no it's not a zero it will not go through this loop this if statement okay we'll see the statement will come okay we'll see when it will zero it will go through this okay So now again it will go through for j. It will go to the n-1, right? Okay. I didn't apply any break statement also so that it will break the whole loop and it will go to another loop here. Okay. So it will go to the for j loop. Okay. j equals to 1 is started. Okay. Now what j is telling? Add another 1. Okay. So what is the next element is 1.

Okay. add another one so what is my current sum what was my current sum sum plus array of a j now one plus one becomes two okay now j equals to two okay which is uh j equals to two is a minus one i think okay and then it will take it as a current sum okay now previously we have a 2 okay which is this one okay 2 is a plus 2 plus i'll write like this for the better understanding like this then it will come as a 1 okay now j equals to sorry j equals to 3 it will go to the n minus 1 right so it will check okay it's checking its time okay i'm not going through that but it's checking each time if the sum is zero or not if the sum is zero okay the moment it comes i'll also like a tell you now what's the j equals to three is a minus one okay so it will uh check okay current sum okay minus one okay so now it will take as a zero okay so now we have a balanced r8 sub net okay which is okay minus one sorry one one minus one minus one correct

its length is how its length is calculated j minus i plus one right so what is my j is three minus zero plus one okay so three minus zero is three plus one is four okay so maximum length is now max length is now fourth max length is now four and then we are calculating like a maximum sub array right we are not even stopping here we are calculating maximum sub array so it will go through the j equals to 4 then what is the j equals to 4 is like 1 okay the j add 1 so current element current sum is so far is 0 plus 1 equals to one okay which is not balanced okay and then the equal to five okay j equals to five and minus one now is zero okay now maximum length how it is calculated jd edge five minus zero plus one correct it will come as a six so that's why it will return you return you as six okay it will like do like this and it will check okay it won't stop here okay why it won't stop here because we know that we have taken one array which is like a solvable correct

So, now it is completed i equals to 0. Okay. Now it will go from i equals to 1. It will check like this or the g equals to n minus 1. It will go to the i equals to 2. It will check like this. Okay. So, this is increasing the n square time. Correct. So, it's going on like this. So, hope you understand. Yeah, thank you, Aditya. Thank you. not misliked quite and misliked but i will go through this solution it's like yeah sure sure thank you so next uh sraddha kamath you're there uh hello yeah yeah uh i'm hard to go yeah hi yes so the first question we were able to do through stacks uh the second one also has a time complexity of o of n square because we're using two that's that's what i said like that's what i said like yeah you're correct like this coming as a big of hands where that's why i told like you you need to do the optimal approach find out it why it is optimal okay and submit it so that's what i said

okay so this is the brute force approach only this is a simple approach you can able to see the question and you can able to get out the answer something like that yeah also the first question you explained stacks but how is the concept of last in first out how is that applicable so like stack simple rule follow like a last in first out okay there is no need to any confusion queue is like a first in first out you see so okay no no queues There is no discussion about the last in and first out. How is it used in stacks? It's simple. It's used in it, isn't it? Okay, okay. I was able to visualize how it was last in and first out. Let's see. One second, I'll show the stack. I'll show the stack like this, correct? I'll show it like this. I will see you as a stack. This is something like this. Push happens, which is, if we can see it here. So, suppose like this, for the current moment, I will take this example. 10 and 2.

So, 10 will be stored here. And 2 will be stored here. Or something like that. how to use the stack, I will tell you how to use the stack. What we are making is the next greater element. If the next greater doesn't get it, then take it to the next. This is a simple fund for our question. I have done this. If the current element is coming, first check it. First check it. What is greater? Okay, let's do this. If it's greater, then pop it. is the Our stack is 6 and bottom is 10. So, it will also be the next element. First, it will check 6 and then it will check 10. So, it will flow. Next, second, third, and third element. So, you have to find this too. Which element is greater in the area, you can find this. Use the public. Okay, got it. This is store with the stack. Okay, got it. One more request. These assignments are graded. Yes, I'm telling you that they're graded. No, no. I didn't have a question. Do you want to confirm it from the server?

No, no, no. I'm saying that... No, no, no, no, no. I got that the server is graded. I just checked one more time. I was just confirming that if it is graded, it would be good if I get an email that the assignment is given with the assignment link. I will give an Excel sheet because I don't have access to admin. I will provide an Excel sheet with the deadline and the date will be given and the assignment link. And that's it, we will not be able to do that. And if you will forward in the mail, then we can quickly find out that this assignment is going to be able to do that. I will forward in this one. If you have a WhatsApp group, then you can chat and pin it in there. Or I will forward in this one every Sunday. Yes, okay. If you don't attend class, then at least you will know that this assignment is going to be able to submit it. Correct. Thank you. Thank you, Sridhar. Ravi Ramjan Singh. do you have any doubt like if sometime it will be not able to like attend session then

we can do this code ourself also before meeting also you can sharing that you will be sharing like either sir mail module like module access thank you manesh yeah so if you are giving any program So, the program which you are giving in the classroom, right now, whatever you are given for the assignment, so normally, what is the end date for that one week, right? One week. Okay, and which language you can use while solving this program? Any language, like there is no any boundary of like a language. See, you have taken AIDs, so that's why I'm teaching in a Python. That will be useful to you. But there is no any boundary of language. You can use any language for this. Okay, so this is the second class for lab, right? Correct. Okay, I think I missed first class. I couldn't solve the assignment, actually. No worries. Then I can submit. You cannot able to because that is passed. Although I can able to extend that to another one week.

Whoever cannot able to submit like one is. I can able to extend up to one week. You can able to guys submit with the same link only if you have. Otherwise, I can send again. Yeah, I can submit one is. actually when i'm logging into the uh hacker rank okay so there i can see that uh you know uh empty test lab one i can see that it was visible yeah that is visible right so if you will if you believe so if i can submit then it's still valid right yeah it's visible only it's visible now so no need to extend that right no no no if you can able to submit you cannot able to submit the mtech lab just a second mtech test lab one i can see uh you cannot able to submit that i'll extend that to another one week so that whoever cannot able to submit they those guys can able to submit submit code option i'm getting i think maybe submit submit code option is enabled for me is available till now yeah i can see submit code and when when i'm opening 12 night of today maybe

maybe uh so when i'm going under mtech test lab one if i'm taking any um question here and i'm clicking on the soft challenge okay then submit code uh button is enabled for me you didn't submit it you can able to submit yeah yeah i i didn't submit it yeah any further question no no no thank you yeah okay thank you i think there is you people do you have any question i have a question uh actually i was uh can you hear me yeah i can hear you but yeah i have a question yeah yeah one second one second now is it fine yeah it's fine actually i have one question here uh i was approached i was like uh using this approach of stack here for the second question first question i have already solved that in the second question i have one approach that uh suppose like here we can use a map also like and traversing suppose if you are searching anything any element using the keys in the in the map also then the uh like the time complexity would be we go of one right

in case of searching using the keys or using the values how you can complete like a in a big one you are not searching anything or you are not like doing anything my plus things you are not raverson here yeah what i would what i was thinking like if we are going through this tag what i was doing i was just doing the sum of it and wherever i i was getting uh the value greater than one greater than zero sorry i was just uh putting the value as one in the stack and if it is less than one sorry less than zero we are I was putting the value minus one and for the zero I was just putting the zero so that I get the same length as we have in the array so the length is going to be the same so after that I just need to find out that what is the longest length in between which I was getting zeros so that will give me the value of the maximum subarray length where we would be getting the balanced value is it fine like I was

having that approach yeah it's it's fine only with your approach but any questions regarding this or like any fault in my approach actually i was just trying to solve by my own so i didn't see actually you were doing you were going through the brute force right brute force yeah yeah correct so brute force would be all will always be greater than the optimized approach so the brute force is i think here it would be uh n square we go off n square right then optimal approach like no no no no yeah brute force it's actually whatever is coming in in our mind even if we are trying to solve it using the two loops or the three loops nested loops then obviously because because why i'm solving in this approach is uh one guy cannot cannot able to solve the problem okay the first approach is coming to that mind is brute force approach okay so that's why i'm solving in that manner yeah brute force okay fine uh brute force

i i just saw yeah it's fine it's in n square i was just trying to solve it using that approach and i when i just uh check the uh like the solution on the internet then uh it was using some other approach using the strat but i like i came up with this approach that's why i thought to ask you okay anything else no no that was my question actually okay go through that approach and you can solve with that yeah okay thank you uh okay i think he's not there chatin Yeah. Hi Aditya. How are you? I'm doing good. What about you? Yeah. Great. Aditya, I don't have a question. Actually, I have a query, uh, the link you are mentioned in the chat, uh, like, uh, this assessment link is routing us to the hacker rank. If I am right. Yes. Like it will route you to the hacker rank link only. Okay. All right. Uh, okay. that is my query all right i'm good okay uh abhishek upadha hey hi i'm audible hey are you audible uh actually just i just wanted to know uh the connected for loop how you have used

like a first question or like second question second second one second or first whichever oh yeah yeah sure okay yeah please go ahead not this one i think okay we'll go to the first one only okay so yeah man what's your question um means how that nested for loop works how that nest nested for loop works you didn't attend at the dry run but no huh i think you need to look at to the you need to ask to the cpt or like something else i already repeated man thank you ah sorry yeah hi hello sir yeah hi man um actually you explain everything very well so i don't have any doubts yet uh but i just have one small uh request to you that could you please ask the admin to share the uh lab current link through model assignment since it's graded sir so uh as we like other assignments uh to moodle the same link will be shared via that assignment so that will that will be very like helpful for us yeah sure sure sure i'll tell to sir that i had this

yeah thank you so much sir thank you uh vishal reina i think he's not there ranjay kumar no very nice gotham bharati yeah are you able to hear me yeah i can't wait to hear yeah yeah you teach us very well so nothing doubt on this uh only have uh curie this is not technical is a non-technical question uh regarding uh the last year assignment and then uh what kind of the questions are coming in the final exam so where we will get that one so if we have the time we can go through yeah yeah in the final exam you will get able to what else what type of portion you will get okay let me tell you in the final exam or your final semester and in the mid semester okay so in the mid semester you will get can able to see both type of question one is a theoretical question another one is a practical type of solution okay so in the theoretical question you can able to see there are mcqs okay given or like a msq is given okay

with the theoretical things okay so what do i mean by theoretical is like let's say one bubble shot is there what is the time complexity of a bubble sort okay so this is the one simple theoretical question what do i mean by practical is so there is a code block given you okay and there is a some space given you okay you have to you have to fill that okay so let's say there is a algorithm is there okay and then some code part is missing okay in that algorithm and that code part is given in the option okay so all all question will be in the mcqs or either msqs okay or either mask the following okay so these three kind of questions will be come in the final in the mid semester okay and these two types of like a uh question will become one is theoretically and one another one is a practical so yeah hope i'm yeah yeah one more thing sir last year can we get the last year uh was in paper or anywhere i don't have okay i don't have any

question last year because i'm not preparing for it or similar sample paper like such kind of things where we can just refer but i i'm not preparing for it so okay thanks thanks a lot yeah thank you gotham sujit pumar does hi uh hi sir uh yeah good afternoon so my question is like uh the uh for lab two the approach would be brute force or that we discussed today or it can be any other optimal approach as well in the in the like a asking the what we have to submit yeah yeah for lab two you can submit either of one you can submit optimal approach you have you can submit the brute force approach sure and the language can be anything right it doesn't have to be okay okay thank you thank you K.M. Rizwan hello hi sir good afternoon good afternoon sir as we can see on the screen the first code which was explained I just had a doubt with the value for suppose uh we were discussing that the value may look to the right and uh just confirm that

if it's greater than that or not or it returns minus one so what if the right value is also the same digit which one for suppose if we have the value 4 5 2 10 on the screen then 8 7 yeah yeah if we have to go with the current element 5 so it looks to the right and it uh okay gets to know that it's a two which is lesser than five okay what if we have five in that place in place of two which is the same equal number not greater not less what is the i can't able to get you to speak louder yeah for suppose if we have the number five itself after the five in the value i can say like four five five ten eight seven if we have in that way four five there is a duplicate value you want yeah if for suppose we have in that way which way like a duplicate value you are asking yeah yeah yes sir it's simply reject right so let's say we are comparing okay you are getting something uh you're in the middle of this okay

you are something instead of two it's five okay so looking into this do you think this is a right no right correct yeah yeah so it will simply no it will say no okay yeah i was having that question so we are not like uh applying kind of this equal correct yeah yeah so that's why it will strictly less than okay okay thank you yeah sure yeah actually i want to have like what we want to like what i need to prepare for the next class so that i'll be uh well prepared before joining this oh what what will be the next class yeah like what will be the topic that i can prepare uh this topic is given by the sir only uh i think two three days back of the class i even don't know what should be the next class or like yeah I'm sorry I can't yeah thank you oh by the way thank you guys like a I got so many requests on a LinkedIn okay could we submit the solution Ryan Marathi yes yeah you can I went to submit, JC Sarma. Hello. Yeah, hi.

Sir, mughi puchna tha, joh hacker rank mein, humare previous submission ko kaisa dheek saakta hai? This lab man, humne submit kiya tha, to account. I don't know, aap saayad hi dheek saakta hai. I don't think so, aap dheek saakta hai. So, aapko kuyo dheekhna hai? Guess pal chalega, kya humne kitna assignment submit ki hai? Thank you. Aapko wohu pata nighi hai, like, aap itna to accountability rakhna chahe hai. I don't think so okay I am allowing the mic of Vikas and Anand. Yeah, I have a question regarding lab 1. So I missed the lab 1. So I was also trying today. I submitted the solution. But I am not able to see the deadline. The 6 days with lab 2 is pending. It is not happening with lab 1. So I doubt that the solution I have submitted has been captured. So, I guess, I guess, you already were captured because you doubtful that he left one submission away. Okay. So, in that case, the first thing is that you have to submit, which means you got submitted

the live one and then I don't know but I'll explore and let you know

