 what's up guys my name is michael and welcome to my youtube channel today we are going to go over street parade this is a problem on spage so basically you have some organizers they want to have a fixed order of trucks you want to bring the trucks in the correct number of order so the increasing order basically so the trucks is a narrow so no two trucks can pass each other you are given the order in which the trucks arrives you need to decide if the mobiles can be brought into order that the organizers basically this is an illustration so let's say we have the truck three four two one five so if i want to bring this into order there's like a hole here what i could do is i could just put the five into this hole and then i let one pass and i let two pass right so i want one two pass instead of adding adding five after four instead of doing that i want to have the next truck pass me also right so i want to have one two three i want three right so what they do also is that they put four into the hole so then they have one two and three so one two and three now pass and then now they could put four and five so basically we have like a hole or a street that we could basically hold the values in and we want to make make all these values be put in the same order that they want them to be so we want them to have one two three four and five so that's basically what we want do this problem you have to think of think of this hole or this road as a stack so every time you encounter value that is just not in the right order that is like greater than the previous value you push that onto the stack so this would have a five as you see here one two three four five right so in this case five was out of order so that's why they put five into this hole and then they let the other ones pass so they let the other ones pass for one two now four is also out of the order right because four is greater than three so they put four into the stack also and then they let three pass now um essentially is that after they let three pass when it's over they just put four and five to see if uh it's in the right order and that's what you get okay so the thing is that if you have a value that is greater than the current value that you want that's in the stack then it's definitely not possible like if I had one two in this case if I one two three four right there's there's five here right there's four here um let's say this four was not here let's say this four was like a six or something then we know it's not possible right because if I take the the the six if this four was a six right have one two three the next value has to be the value after three so next five has to be four right so basically whenever we pop something off of the stack we have to check if it's actually equal to the next value that is in line so what we need is we need a counter showcase the next value that we want to be put in order right we want to all these trucks to be put in order from one two three four five six so if we have a counter that keeps track of the next number of the truck that we need then we could actually keep track of it and then that would actually help us make sure to check if it's possible or not possible so I'm going to actually just explain the code to you guys right now because it's actually not that difficult to understand okay so remember we need a counter in order to count the current the next truck that we want right so one two three four five six and um not only that uh also the input statements here they said that uh the input statement ends with a number zero so it's not the same thing as like input number test cases like on code forces and then you just do wild minus minus so what you have to do is you have to either use a while loop and then break in the middle or you can use a do while loop and that's what I did so first of all I'd use the do while loop to basically just do this while this condition is occurring so it does it at least once and then um while n is not equal to zero so the n is the first value that you input input uh which is input with a number zero or not so if that part is inputted right if it's zero then we just stop the the whole program right so that's why I said do while n is not equal to zero and it has to run once so that's why you need to do a while loop so that's why I did um then I read in the value of n and then um what I do is I once I read in the value of n I create my array I call it e for the array but uh I don't know you could name it whatever you want then I loop through all the values of n from one to n to read in the array so read in my array right the list of numbers that we'd have five one four two four three right so that's what I do here remember we we want to check if we could actually put this in order using uh using the hole that we're given right so yeah all right so let's think of the hole as a stack so this I create the stack and I call it st and the reason why I call it st is because I don't know it's just good it's a name for a stack so st is just like our stack and that's kind of like the hole that we need that in this case so this hole that that is put into this case so um here into need equals to one this is the counter right the number that we currently need that which is like the next the next uh the next truck number that we need so remember we want to be able to order this these trucks three four two one five into order of one two three four five right using only given this hole so that's why we need to we use the we need a counter to keep track of the next number of the truck that we need so we need to have once we have one we need two then we need three and four then need five okay okay so that's what I do here and then I need a I have a boolean which stands if it's possible or not so if it's in what they want us to do is if it's possible to order them in one two three four five six right then we print out yes in this case output yes otherwise we print out no so that's why I have a boolean for possible okay so at this point what I'm going to do is I'm going to loop through all the values of the truck truck numbers again it's going to loop through from zero to n right this is the index so I'm going to go through all the truck numbers which is five one two four three right now after that what I'm going to do is I'm going to check while my stack which is kind of like my hole is not empty okay if it's not empty I'm going to check if the top part is actually equal to the next truck that we need because if that's the case then we could just remove the top part and add the the truck right so yeah because like in like in this scenario one two three four five right after three is gone right three is gone if our top part of our our stack four this top part of this truck is four right this if this is the equal to the next number that we need which in this case after one two and three is over right four is the next number so what we what you would do is you would keep popping off of the stack so you remove four and remove four and then increment the next truck that we need which is going to be four plus one which is going to give us five so that's going to be the next number that you need and then you could pop that off also so this is basically just this while loop is basically just every time there's a it's not empty and the top part is actually equal to the next truck that we need the top truck of our is equal to the next truck that we need we're just going to keep adding one we're going to we're basically just going to add one to the counter of our truck and then we're going to remove the first value off of our stack so in this case first value off of our stack it would remove four right so we have one two three so four is equal this the first part of the top value of our stack of four is equal to the next number after three so it's going to increase three plus one which gets us four or no yeah yeah actually yeah no no so the the top value of our stack is four right and then that is actually equal to the next number that we need so one two three next number that we need is four right so what it's going to do is it's going to it's going to pop four off of the stack and then add one to the next number so the next number is five it's now going to be five because then we add four into the line that of trucks that are there and then we have five and then we pop five off of the stack of this also right because this is not empty so we're going to pop that off and five is also equal to the next number because the next number after four is five so that's why we're going to keep popping off of the stack for that case so that's what this loop is doing okay all right now if the first uh if now if going through the going through the array now um if the value that we're currently at going through our array is actually equal to the next number that we need then all we just have to do is increment the next number that we need so like in this case if it's um let's say we have one two uh in in this in this example like one right one is there uh the next number that we need is one so it's going to just move the the truck to the right side so it's going to add one to the counter of one so it'll get to two and then two is going to two is equal to the next number that we need right so then when it's just going to add one two which gets us three so that's what this part is doing okay all right now here's a bit tricky part um if it's not empty and the top part is less than the current value of the what we're going through then it's not possible and I'll I'll explain why so like let's say we had um um one two three um yeah instead of so we have one two three right instead of this four this this was a zero so let's say this four was a zero then we know it's not possible to reorder it because the top part of our stack is zero and this is less than the next number that we need next number that we need is four right so if this four was a zero so if this four is zero next number that we need is four so that's no matter what it's not possible so here I'll I'll draw it out again so let's say I have one two three and this let's say this is our stack and then instead of four we have zero and then we have five right so the trucks are going to the right and um the next number after three is supposed to be four if I know this number zero the top part of our stack right this zero is less than four no matter what I do right if I if I were to remove the top part of the stack and let it keep going so I remove zero and I have zero here and I remove five and have five here it's not going to be in the right order regardless right if I just attach it to the end of this this train these numbers it's not going to be in the right order and the reason why is because this number is less than the next number that we need of four so that's why it's just not possible so at that point you could just break so in here it would just not be possible so then possible I said defaults and I break out of the whole loop all right now otherwise if these cases aren't are not the case right well let's say it was greater or something um then we just say we we're just going to push the next value onto onto our stack right so that's that's the case if um um that's the case when we have when you have a one two so we have one two three uh let's say this was in the original case five right and this was like uh let's say there was four here right so what this is going to do basically this part of pushing to the stack is when all any of these cases are not true that means that um five is going to be greater than one this five is going to be greater than one so what it's going to do is going to push five onto the stack of values that we're going to check later and then um it's going to move the one to the right so then this five is gone and then moves one to the right and the next number we need is two right and then um since four is greater than two it's going to push four onto the stack so then we could check four later to see if we could actually do that or not and then it moves two to the right so then four is gone and then two go two goes away right so now we have three right three is the next number there and three is the right way right three is in the right number so then we're done with three and then um after one two and three are all done right then we add four and five right four and five would be the case so then in this case it would be possible so the only way if it's not possible is if the top value of our stack is actually less than it okay so yeah that's what I do here that's basically just the code um so remember they say that they wanted to have the case when um when when the output is zero right then you have to stop the program so if input ends with zero you have to stop the program so I have to have this if condition if it's uh because if it's equal to zero right then you have to stop program and you can't display anything right you can't display yes or no right so what I did was I check if it's not equal to zero then what I'm going to do is I'm going to check if it's possible then I'll just print yes otherwise no and then this just stops it from printing yes or no when it's equal to zero so that's what this loop does this f statement does and then while the while it was done it's basically over yeah so that's pretty much the gist of this problem I hope you guys understood what I was saying and yeah rate com subscribe it's just push it putting stuff onto the stack and checking if it's possible or not possible and I'll check you guys later peace