• 4 days ago
Transcript
00:00The next question we are going to talk about in the stacks is called next greater element
00:04and this question is very important and very classical question
00:09The logic of this question is used in other questions
00:12In fact, I have made a special note in my DSA notes
00:17so I have dedicated it to about 2 pages and explained it well
00:21through the diagram that this question is how it works
00:23but this question is not difficult, its logic is very easy
00:26but it is important to remember the logic of this question
00:30because this question is directly applied to many other questions
00:34The question is next greater element
00:37What do we have to do?
00:38We have an array in which we have many numbers
00:42and for every single number, I have to take the next greater element
00:46What do we have to take?
00:47Next greater element on the right side
00:50Next greater element means that the first number
00:54which value is greater than x
00:58and x on the right side
01:00Once again, let's see the sample input and output
01:03Let's take the sample input
01:05I have an array in which there are numbers
01:076, 8, 0, 1 and 3
01:10Now, if I analyze for 6
01:12What do I need to do next greater?
01:14What do I need to do?
01:14In all of the elements, the first number
01:18which value is greater than 6
01:20Let me remove it
01:21What is the way to remove it?
01:23To remove it, I will go to 8
01:27Before I ask 6, I will ask 8
01:298 is greater than 6
01:298 is greater than 6
01:31What is 8?
01:328 is the next greater element
01:34on the right hand side
01:36All I read is at 8
01:39Then I have to remove next greater element
01:41for my 8
01:43So the next higher element will become
01:46different
01:46and that I will go to 0
01:50I will answer the 0
01:50To echo my comment, if 0 is greater than 4
01:520 is greater than 8
01:52It is no greater than 8
01:54so I will go to the next one
01:55and then Hola
01:56and I will ask you if 1 is greater than 8
01:581 is an greater than 8
01:59then go to the next one
02:00Then Holand 3
02:01and I will answer 3
02:02and 3 is greater than 8
02:03thats not greater than 8
02:04I am not greater than 8
02:04After this, I am not neither of the elements
02:05So for 8, in this entire array, no one exists next greater element from this array.
02:13So for 8, I have to write the answer to minus 1.
02:17Now I will go to 0.
02:19For 0, I have to remove next greater element.
02:22So what will I do first?
02:23I will ask 1 from 1.
02:26What will 1 become 0?
02:27So what will 1 become?
02:281 will become 0 for next greater element.
02:31This is next greater, this is next greater, this is next greater, this is next greater.
02:36Now let's talk about 1.
02:38For 1, I have to find the next greater element.
02:41So what will I do first?
02:42I will ask from 3.
02:43Is 3 bigger than 1?
02:45Yes, 3 bigger than 1.
02:46So what will 3 become?
02:473 will become 1 for next greater element.
02:50So this is for 1.
02:52Now I have to find out for 3.
02:54For 3, I will search for next greater element.
02:59But there is no element in the right side.
03:00I will store for next greater element.
03:03So I will store for next greater element.
03:04I will say minus 1.
03:04Minus 1 means that no next greater element exists.
03:08I have assumed that my array is greater than equal to 0.
03:14So there is no negative number in it.
03:16But if I wrote in next greater,
03:17I have to write minus 1.
03:18It means that it doesn't exist for this element.
03:22How will there be elements?
03:23Or it will be my last element, 3.
03:27After that, there will be no next greater exist.
03:29No number in it.
03:30Or then it will be my 8.
03:32It will be my right side.
03:33The value of all elements is small.
03:35Therefore, there will not exist for next greater.
03:39So I have to write code for next greater.
03:40I have to write all the array elements.
03:43First, let's discuss the brute force approach.
03:47What will the brute force approach be?
03:49Brute force is the most simple solution that comes to my mind.
03:53The simple solution comes to my mind that I will run a nested loop.
03:57Nested loop means that I will run a loop.
04:00And what do I do?
04:00First, I will start to pick 6.
04:03I will run a loop for 6.
04:05I will run a loop for 6.
04:06I will search all the elements after 6.
04:07The first element, which is the value of 6.
04:11I will find out.
04:13Next, I will link to find out for 8.
04:16So, when it comes to 8,
04:18I will check 0 and 3.
04:19After that, when it comes to 0,
04:21I will check 1 and 3.
04:24Then, when it comes to 1,
04:26I will check 3.
04:28And when it comes to 3,
04:29I will check 2.
04:32So, I will check 1 and 3.
04:35I will check 1 and 3.
04:36I will check all the elements.
04:37So, it is my looping.
04:39I will run a loop.
04:40I will traverse the area.
04:42I will pick 1 and 3.
04:44Then, what will I do?
04:46I will start with the index.
04:48I will look at next.
04:50I will show you a pseudo code.
04:52If I write the nested loop,
04:55I will start with i is equal to 0.
04:57I will not remain less than array.length.
05:01I will do it plus plus.
05:03What will happen in the inner loop?
05:05In the inner loop,
05:05I will start with i plus 1.
05:08And the value of j will go to array.length.
05:11And I will do it plus plus.
05:14And I will compare here.
05:16If my array of i is less than,
05:19from my array of j.
05:20When the first time is less than,
05:22I mean, first time.
05:24What did I get there?
05:25What did I get there?
05:27I got my next greater element.
05:30As I get the next greater element,
05:32I will update this value.
05:33And I will break here.
05:35I mean, just from this inner loop.
05:36I will run the outer loop for the other element.
05:38So, there is no break here.
05:40I will take a variable here.
05:41To track if I got it or not.
05:44Or the inner loop will only go until I get the next greater element.
05:50Until I get the next greater element.
05:51Not found.
05:52Until I get the inner loop.
05:54So, there will be some code in this way.
05:57When we write the code in the brute force approach.
05:59But in this code, there is a problem.
06:01The problem is its time complexity.
06:03In this code,
06:04the time complexity will be n2.
06:06Why will it be n2?
06:07Because I have a nested loop.
06:08This loop,
06:10which is from 0 to 0.
06:12Under i plus 1.
06:14We have already calculated this loop.
06:17This is n2.
06:19But I need an optimized time complexity.
06:21Because we are studying stacks.
06:24So, I know.
06:25To solve this question.
06:26With optimized time complexity.
06:28Who will I use?
06:29I will use stacks.
06:31Now, I am going to discuss next greater element.
06:34Stack approach.
06:36What will I do?
06:37First of all, I will create a stack.
06:40I will create a stack.
06:42I will store it in this case.
06:44I will store it in this case.
06:45What are we going to do?
06:46In the beginning,
06:47I have an array of numbers.
06:50I mean a stack.
06:51What will I do?
06:53I will create a next greater area.
06:55In every array,
06:56I will store all 1 index.
07:00First index,
07:01next great later.
07:02Next is 0.
07:04Next is greater.
07:04Next is greater.
07:04Next is greater.
07:04Next is greater.
07:07So in the beginning, I have a next greater, which is the size of my array.
07:12I am going to store the final answer within which I am going to store the final answer.
07:15Next, greater.
07:20I will also write the index here. This is index 0, 1, 2, 3, 4.
07:26This is index 0, 1, 2, 3 and 4.
07:31What do I have to do?
07:32Because I have to track my right element that my next greater exists.
07:40So what I will do? I will start backwards.
07:42Backwards, I will go to this element.
07:44Then I will find out next greater.
07:47Then I will find out next greater.
07:48Then I will find out next greater.
07:49So as I am coming to 8, what I have to do?
07:52I have to traverse all the rightes.
07:55So I will travel and track them.
07:57Which is the smallest one from 8?
07:59Then I will find out next later.
08:01The logic is that I will travel forward.
08:03I will travel backwards.
08:05So that when I reach someone to an element,
08:08I will analyze all the rightes.
08:15Because the next greater is the rightes.
08:19So I want to analyze all the rightes.
08:29So I will run a backward loop.
08:32If I have to run a backward loop,
08:34I will analyze all the rightes.
08:36I will analyze all the rightes.
08:37And I will try to find out next greater.
08:41I will try to find out next greater.
08:41For 3 or any number,
08:43I will find out 2-3 steps.
08:45The first step that I have to perform is this.
08:50That is when my stack is empty.
08:55When the stack is empty,
08:59I will say this is my stack top.
09:01Which we will peak.
09:03When I have to find any element in stack,
09:06I can only access fake elements.
09:07One time I can access my stack top.
09:10Or I can also call the peak operation.
09:13So when we start the top,
09:16We will peak the stack.
09:17The above value of the stack,
09:19If it is equal to me,
09:21If I am analyzing the number,
09:24I am going to find out next greater.
09:26If this value is small,
09:28Then I will remove the value from the stack.
09:31That means,
09:32For any element,
09:33I will store a particular value in stack.
09:36So until I remove those values,
09:39I will remove and remove and remove.
09:41Until I have no element,
09:43That will increase from this value.
09:45Because I have to find out next greater.
09:47I assume that,
09:49I am reaching out to my number here.
09:52So if I can say that,
09:53The right side elements,
09:56There will be some elements in stack.
09:59I don't need one of those right side.
10:00I don't need one of those.
10:01I don't need one of those.
10:02I don't need one of those.
10:03I don't need one of those.
10:06I don't need one of those.
10:07That's why I will do it.
10:08Until I have to find out next to my small elements,
10:10I will remove them from the stack.
10:12This is the first step.
10:15The second step is to perform.
10:17If I believe,
10:18I have removed the small elements from stack.
10:21I have removed the stack.
10:21And I have removed my stack.
10:23I have removed my stack.
10:23I have removed the small elements.
10:27So when I have removed my stack.
10:31When it is empty? After running the while loop,
10:34After running the whole step 1, if my stack is empty,
10:38What is my case?
10:39In that case, I have a particular element for next greater.
10:43Minus 1 means that next greater doesn't exist.
10:46But if the stack is empty,
10:47then there is an element on the top of the stack,
10:50which is greater than me.
10:51And that element will be my next greater.
10:55If the stack is empty,
10:56then my next greater will be the top of the stack.
11:00I call it a peak here.
11:02I have to perform these 3 steps for every single element.
11:06In fact, these are not 3, these are 2 steps.
11:08The first step is to remove elements from the stack,
11:11until my stack is empty,
11:14or I have small elements.
11:15Because if I talk about myself,
11:17I want to remove next greater.
11:19And I have stored everyone in the stack.
11:22So if I need next greater,
11:23then I don't have small elements.
11:25I will remove small elements,
11:26so I will remove small elements,
11:27so I will remove small elements,
11:28until there is no greater element.
11:29And then I will check that,
11:31either it will be true in two conditions,
11:33or the stack is empty.
11:35It means that there is no greater element.
11:37So now my next greater is minus 1.
11:39Or the other condition will be true in this while loop,
11:42and I will remove my stack.
11:43Now I will remove the value in my stack.
11:46Now I will remove the value in my stack.
11:47Now I will remove the value in my next greater.
11:50Now I will dry run this one.
11:52And as we will dry run this one,
11:54now the first time when we are learning,
11:56I was also learning the code and logic.
12:00I will not understand the logic.
12:01and see the words of these words.
12:03I will not understand the words.
12:03In the sense of this question,
12:05when I will dry run this one question,
12:07and take one example,
12:08you will dry run yourself.
12:10When you can use the pen and pencil of mine,
12:12after doing my dry run,
12:13you will pause yourself.
12:14And then you will dry run yourself.
12:17It will be clear logically because this question is not difficult.
12:21But it is slightly difficult to think if you are doing this first time.
12:26But this question is important.
12:29This logic is used in other questions.
12:31It is used in exams.
12:32It is asked in interviews.
12:34That is why this logic is important to remember this question.
12:37And we will remember when you will at least dry run it one time.
12:41So what do we do?
12:42We will dry run it one time.
12:44After that you will pause and try to dry run it.
12:47According to logic, we will start backward travel.
12:51I didn't tell you why.
12:52I will go to last element 3.
12:56I will run my 2 steps for 3.
12:58What do we need to do?
12:59I will run this loop until my stack is empty.
13:03Or I will remove the small elements.
13:06The stack is empty.
13:07So I will not run this loop for 3.
13:09Now I will check the condition.
13:11Is my stack empty?
13:12If this condition is empty.
13:13If this condition is empty,
13:14If this condition is empty,
13:14In this case,
13:15Next greater is empty.
13:17If it is empty,
13:18in this case,
13:18Next greater is empty.
13:19No one exists in the right side.
13:20Because the stack is empty.
13:22So I will store the next greater to 3.
13:25Now I will ...
13:26Now I have to perform the last step.
13:28After I perform the last step.
13:29After I perform them,
13:31What do I do in the last step?
13:33I will push my element.
13:34Last step,
13:35I was going to check my element.
13:37I am going to push the stack.
13:39So I checked for 3, now I have minus 1 for 3, so what do I do?
13:43I add 3 to this stack.
13:46In fact, when we do actual implementation of code,
13:49I will not push 3 into this stack.
13:51I will push 4 into this stack.
13:54But because we are only learning logic,
13:56I am not pushing 4 into this stack,
13:59I am pushing 3 into this stack.
14:01But in code, we are going to push this index.
14:03Because it is easy to track things from index.
14:04It is easy to track things from index.
14:05Now the first question is why I have pushed this 3?
14:09I have pushed this 3 so that when I go to the next elements,
14:13such as if I have 8 or I have 0,
14:16I know that there is 3 in the right side.
14:19So that's why I pushed 3 into this stack.
14:24Next, let's move on to the next element.
14:26The next element is 1.
14:29We perform step 1.
14:31Until my stack is empty,
14:33or the big elements are removed from 1.
14:36So if the value of 3 is 1?
14:38No, it is small.
14:38So I can't pop it.
14:40This means that I got an element
14:41which is greater than 3.
14:43So I will go out of this loop.
14:45Now I have to check if my stack is empty.
14:47It is not empty because it is 3.
14:49So if it is empty,
14:50I will store it in next and greater.
14:52Which is on my stack.
14:54It means that for 1,
14:55which will be next and greater,
14:57it will be equal to 3.
14:58So for 1, I will store 3 as my next creator.
15:02Then what will I do?
15:03The third step.
15:04What will I do?
15:05I will push 1 to my stack.
15:07So I will push 1 to my stack.
15:09Actually, I have not pushed 1.
15:11I have pushed 1 to 1.
15:12I have pushed 1 to 3.
15:13But I will show 1 to 1.
15:16Then what will I do?
15:17I will come to 0.
15:18Now what will I do?
15:20What will I do?
15:210 until the stack is empty.
15:22If it is empty,
15:22it will not be empty.
15:23It will not be empty.
15:23Or it will not be empty.
15:25Then it will be empty.
15:26If it is empty,
15:28it will not be empty.
15:29Then we will go out of this loop.
15:31Now I will check if it is empty or empty.
15:33So what will I do?
15:34I will create next and greater.
15:36I will create a stack.
15:37With the top or peak.
15:39So there is 1 to the top.
15:40That's why I will create 1.
15:43So I will store 1 to 0.
15:44I will store 1 to next and greater.
15:47Now what will I do?
15:49I will push into my stack.
15:50I will push into my element.
15:52So I will push into the stack.
15:52In the last,
15:53I will push into the stack.
15:55But I will push into the code.
15:56I will push into the index.
15:57Which is equal to 2.
15:58After that,
15:59I will come to my next element.
16:00The next element will be 8.
16:03Now this is 8.
16:04This will be very interesting.
16:06This will be more clear.
16:08What will we do for 8?
16:09We will run this loop.
16:11Until my stack is empty.
16:14Then,
16:15I will use the number of 8.
16:15The number of 8 is small.
16:16It means that if there is a small element in the stack.
16:19I will pop those elements on the top.
16:21From the top of 8,
16:22the number of 8,
16:22I will pop until the stack will pop.
16:23I will compare 8 with 0.
16:261 is small to 0.
16:28Yes,
16:28It is small to 0.
16:29So I have to remove the 0 from the stack.
16:31I will remove 0 from the stack.
16:33Then,
16:34I will run the loop again.
16:35I will check.
16:36If the next top is小.
16:38The number of 8 is small to my number.
16:39I will compare 8 with 1.
16:41If 1 is small then it will be removed
16:43Because for 8 is no work
16:458 will find a big element from right side
16:48But 1 is small from 8
16:49So 1 will be removed
16:51I will remove 1 from the stack
16:55Now we will check in the loop
16:57If the stack is small from the top
17:00If I compare my 3
17:03If I compare my 3 is small from 8
17:06Yes, it is small
17:06So 3 will be removed from the stack
17:12Now when we check the condition
17:13If the stack is empty
17:14Then the first condition will be true
17:17Which is that my stack is completely empty
17:19So I will break this loop
17:20We will check in the second step
17:22In the second step we will check if the stack is empty
17:24Then yes, it will be true
17:25In that case my next is greater
17:27It means that I don't exist in right side
17:31Because the right side elements are in the stack
17:34And the stack is empty
17:35So for 8
17:38That will be equal to minus 1
17:41And in the last step
17:42What will I do
17:43What will I do
17:43I will do my number
17:44I will push
17:45So I have pushed in the stack
17:47My 8
17:48Now intuitively
17:49One logic will be clear
17:51What will I do
17:528 has done
17:538 has done
17:538 has done
17:53All elements
17:54So I will clean
17:55And now
17:56If I will check for 6
17:58Then the first question
18:00In the beginning
18:00That
18:028 has done
18:030,1,3,3,3
18:04To delete the stack
18:05Then
18:06How will 6
18:06How will I know
18:08That there will be no next
18:09Greater
18:10No
18:106
18:117
18:117
18:248
18:267
18:318
18:338
18:368
18:388
18:388
18:398
18:398
18:40no difference to 6 because 6 also needs a big number
18:42and if the big number is passed
18:44I don't have a problem, I need a big number
18:47so what should 6? It needs a big number or next greater
18:51so finally, when we come to 6
18:54so what will we do for 6? We will perform the first step
18:57until my stack is empty or
18:59the small numbers are empty
19:01so we will compare 6 with 8
19:03so what will I do with this loop?
19:07now we will check the condition if the stack is empty
19:10so what will my next greater become?
19:13which is the top of the stack
19:13so for 6 the next greater element will be 8
19:17so I will store the value here
19:19so let me see the final answer
19:21so the next next greater element is 8
19:24so here is 8 stored
19:25after 8 there is no next greater
19:27so it is minus 1
19:28what is next greater than 0?
19:30it is 1
19:31so here is 1
19:32what is next greater than 1?
19:33it is 3
19:34so here is 3
19:34and what is next greater than 3?
19:37what is next greater than 3?
19:38so here is minus 1
19:40so this is logic
19:40to perform the next greater
19:43now in this question
19:44in this whole approach
19:46which will be time complexity
19:47that will be big of n
19:49why big of n?
19:50because what did I do?
19:51or I have reversed all the elements
19:54this is my linear time
19:56this is my linear time
19:56but the elements
19:57you can say
19:58that in the loop
19:59I am running
20:00I mean my code
20:01will see something like this
20:03that I will start the elements
20:05from i is equal to 0
20:07in fact backwards
20:08so my value will start
20:10from i is equal to n-1
20:12i is greater than 0
20:14i is minus minus
20:15so I am going backwards
20:16every time
20:17I will perform
20:18these 3 steps
20:19i mean my loop
20:21will go out
20:21until my stack
20:22is not empty
20:24or the smaller elements
20:27that are going
20:28until I am empty
20:30until I am empty
20:31after that
20:32what will I do?
20:32I will check
20:32if the stack is empty
20:34if the stack is empty
20:36if the stack is empty
20:36then it means
20:37to add minus 1
20:38if I am adding
20:38I will not add
20:40then in the other case
20:41the stack
20:41which is the top
20:42or the peak
20:43will be my Linux creator
20:45in this
20:46the first thing
20:47in the beginning
20:47is that
20:50there is a loop
20:51outside
20:51this loop
20:52how will it go
20:52in which time complexity
20:53in big of n
20:54and in this loop
20:55there is another loop
20:56which I am doing
20:57inside the stack
20:57which I am doing
20:59I am doing
21:00I am doing
21:00I am doing
21:00and I will push
21:01so the time complexity
21:03should be n-square
21:04but logically
21:05the time complexity
21:08because the loop
21:09inside the loop
21:09if I count the total steps
21:11in this loop
21:12we can say
21:12that one element
21:14will only go into
21:15one of the stack
21:16and one of the stack
21:17will come out
21:18so if all elements
21:19go into one of the stack
21:20and go out
21:21one of the stack
21:22then we can say
21:23that it will be
21:23two times
21:24if I am getting
21:26all the operations
21:27in the loop
21:28then it will be
21:292 multiplied by
21:30n-time
21:32and
21:32if I am adding
21:332n
21:33with this n
21:35with the loop
21:36then it will be
21:37time
21:38so I can say
21:38this is already
21:39time complexity
21:41big of n
21:41because we ignore
21:42the constants
21:43because
21:44every element
21:45in this array
21:46will go into
21:46one of the stack
21:47and once
21:48it will come out
21:49this is why
21:49time complexity
21:50is linear
21:50n-square
21:52will not be
21:53two important things
21:54in the next
21:55greater element
21:56one is its logic
21:58logic
21:58will be something
21:59and the other thing
22:01is time complexity
22:02that this is an optimized
22:03time complexity
22:04linear time complexity
22:06n-square
22:06no
22:07now
22:07here
22:08the most important thing
22:09that you can do is
22:10pause the video
22:12another
22:13another
22:13sample
22:14you can make
22:15any array
22:16try to make
22:17five to six elements
22:18make a proper
22:19array
22:20and use a stack
22:22and use these steps
22:24you will find
22:25next greater
22:25find out
22:26on paper
22:27and pen
22:28after that
22:29we will write code
22:29after that
22:30we will write code
22:30in the next
22:30we will write down
22:30the next section
22:30we will put
22:30and
22:30we will