- 5 days ago
Transcript
00:00Let's talk about the code of next greater element. I have created a main function in a class and I
00:05have imported the utility package on the top so that I can use the stack.
00:10I have taken the array in my example which we have done dry run. What will I do? First of
00:16all, I will create a stack.
00:17The stack will be my integer. S is equal to new stack. With this, I will create another integer array.
00:25I will name next greater.
00:30I will initialize the next greater with size. The size will be equal to my array.length.
00:36Arr.length. Now what I have to do? I have to run a loop for every single element. I will
00:42start the loop from my array.length-1
00:47until i is greater than 0, i is minus minus. I have to perform three steps in the loop. First
00:53of all, I have to run a while loop.
00:56My second step is to check the FLS and the third step is to push in stack. First of all,
01:03I will run a while loop.
01:04I will run a while loop. What is the work of while loop? While loop is not empty until the
01:08stack is empty.
01:09Or when the stack is empty, the small elements are not removed from the stack.
01:12The small elements are removed from the stack. Then the small elements are removed from the stack.
01:15So, not of s dot is empty. Until my stack is empty. This loop will run until the loop.
01:21Or until my array of s dot peak is less than or equal to my array of i.
01:35I will tell you what I have done. I will tell you what I have done.
01:38Because I have told you what we are going to do in the start of the stack.
01:41We will push the index in stack. We will not push the index. We will not push the index.
01:45We will push the index. Because I want to track all the things from the index.
01:49I will push the index in stack. When I look at the top of the stack, it will be an
01:53index.
01:54Now, which value was in the array? I have used this array.
01:59So, not to do s dot peak directly. There is index in s dot peak.
02:04I will search the index in the array. If I have pushed the index in the last time.
02:10So, if I have pushed the index in the last time. If I have index in 3, I will have
02:114.
02:12Then I will do s dot peak. I will get 4. But I need a number.
02:16Because I can compare the number with any number. So, I will remove the value stored in the index.
02:21I will remove the index in 4. So, my index in s dot peak will be 4.
02:25And I will have 3. So, to store the index in stack, I will store the number. This is important.
02:32What will I do every time? Every time I will pop.
02:35So, when I have small numbers, I will pop all the indexes.
02:39After that, I will check in the second step. If my stack is empty.
02:44S dot is empty. What is in that case?
02:49In that case, my next greater of i will be minus 1.
02:54And in other cases, my next greater of i will be s dot peak.
03:01Now, s dot peak is already an index. I don't store the index. I have to store the number.
03:06So, what will I do again? I will put the array in square brackets.
03:11So, in this index, we will make the value in the next greater.
03:18So, I have not done a small change. This is not difficult to understand.
03:21The third step is to push in the last step. I will put the element in stack.
03:26So, I will put the element in stack.
03:28Because I will track the element in the index. So, I will not push the element.
03:33I will push the element. What is the index? What is the index?
03:36Because the ar of i is my element.
03:39I will start from the back.
03:40First, it will become 3.
03:42My ar of i will become 3.
03:44Then, it will become 1.
03:45Then, I will go to 0.
03:46Then, I will go to 8.
03:47And I will push the index in the last step.
03:49So, this is my entire code to find out the next greater element.
03:53After that, I will run a loop.
03:55For int i equals 0.
03:57So, i will be less than next greater.length.
04:01I will do plus plus.
04:02And now, I will print my next greater.
04:08Next greater of i.
04:11And I will do next line.
04:16This will come to small.
04:17So, this is the whole code.
04:18What did I start with stack and next greater?
04:22I have started a loop.
04:23I have started a backwards loop.
04:24Because I have already tracked the right side elements.
04:29So, when I have left next greater in the right,
04:31Then, the backwards loop is going.
04:33You can change the question form.
04:35You can change the next greater in the left.
04:37So, I have to change the left loop.
04:40I have to interchange this loop.
04:41That means, I have to start from start from starting 0.
04:44We will start from end.
04:45Right, then we will start from right.
04:48And we will start from left.
04:49The first step is performed.
04:51Even when I am not empty stack.
04:53I will not pop all the small elements, until my loop will go.
04:59If the stack is empty, if there is no greater element, then it will be minus 1.
05:04If the stack is at the top, it will be my next greater.
05:07At last, I will push my self element into the stack.
05:11Then I will run a loop to print my next greater.
05:15Let's save and run.
05:22I have my next greater. Let's analyze them.
05:25For 6, the next greater is 8.
05:28First, it will be 8.
05:30After 8, there is no next greater.
05:32So, for 8, it will be minus 1.
05:35After 0, the next greater is 1.
05:38So, it will be 1.
05:40I will talk about 1.
05:41After 1, the next greater is 3.
05:43So, it will be 3.
05:45After 3, there is no element.
05:46So, the next greater is no problem.
05:48Therefore, I have minus 1.
05:50So, the next greater is no problem.
05:51The entire time complexity of the whole code is bigger than n.
05:54So, we will solve the next greater question.
05:58This question of next greater is many forms.
06:02This question has 4 different forms.
06:04One is the next greater.
06:06How do we remove the next greater?
06:07How do we remove the right side?
06:09We have analyzed it.
06:10Another one can say that you remove the next greater.
06:13You remove the left side.
06:14So, we don't have to do anything.
06:15The next greater is to remove the left side.
06:17So, we have to change the loop.
06:19We have to start the loop from 0 to 0.
06:21And we have to take the end to the end.
06:22This will be the condition.
06:24Another one can say that you can remove the next smallest.
06:28Or smaller in the right-hand side.
06:31So, what do we do?
06:32We have to reverse the condition.
06:34We have to remove the small elements from the stack.
06:37Because we don't have any work.
06:38If we remove the next smallest,
06:39So, remove the big elements.
06:41Because we don't have any work.
06:42So, this is less than equal to.
06:44We will give it greater than equal to.
06:47Besides, we have another question.
06:49Next smaller left.
06:52Next smaller left.
06:54What do we do?
06:55We have to reverse the loop.
06:57We have to take the end to 0 to 0.
06:59And we have to reverse the condition.
07:00We have to reverse the condition.
07:00Or greater than equal to.
07:01So, in this same code,
07:04We can solve these 4 different questions.
07:07And on this particular question,
07:09You can see that we have not written so much code.
07:12We have not written so much.
07:12We have no time to dry run.
07:14But I have to take a lot of time.
07:16I have to take a lot of time.
07:16I have to explain this question.
07:18Because this is not the same question.
07:20This is the logic of 4 questions.
07:22And with this, there are more different questions.
07:25Which we will do now.
07:26In this question,
07:28The logic of this question is very good.
07:30It's very important.
07:32It can be either directly or change the form of this question.
07:38Make sure it's very good.
07:38So, I need to do it good.
07:39And make sure it's very good.
07:40I'm putting notes on it.
07:41I'm putting notes on it.