Hi, everyone, welcome back.
So in this video, we will be writing the code for this problem and now let's start writing the code.
So as discussed, what we need to do, we need to perform the level order reversal.
So let me write all these steps here.
So what we are going to do, we are going to perform lavalier traversal for performing level or traversal.
We need WANGU.
And this will contain two things Naude and the horizontal distance for that node.
And then we need one map.
So this map will contain the horizontal distance ASCII and list of nodes as well.
So let's start writing the code now.
So first of all, what do we do.
Let's create the map so we need one map.
Map of integer horizontal distance would be integer and then the list of nodes.
So vector end and let's name it my map.
Fine.
And now we're going to do we need to do level or traversal.
So for doing level traversal we need two and vertical with that type of queue.
So the type of goulet's define struct let's name it element or what should we name it.
Yeah, let's name it one element, one element of Q So what it will contain, it will contain two things
the node and the horizontal distance.
Actually is the horizontal distance, so you will be of type element.
It will contain two things in horizontal distance.
So this is not and this is horizontal distance.
Fine now for doing traversal.
What do they need to do?
Let's push the first element.
So culotte push.
So what is the node?
Nor will be the route obviously.
And what is the horizontal distance?
It is zero.
So for you, the horizontal distance is zero.
And now what we need to do, our standard laboratory ourselves, or that is while the queue is not empty,
we will do something.
And what we will do so element, temporary or legitimate element friend.
So what is this dude, old friend?
It will return one element and obviously not pop to pop this element also.
Now, what we need to do, let's find out what is the horizontal distance for this node.
So what is the horizontal distance for this node?
So what is the current horizontal distance that will be front d'arte, horizontal distance.
So front dot, horizontal distance.
I will get the horizontal distance.
And what is this node?
So let's figure out, let's name it current node.
So current nodes, front dot node.
Now all they need to do, we need to insert in map.
So let's insert my map of current horizontal distance.
So my map of current horizontal distance is basically a list.
Right.
This is one list.
So you will put this is one vector.
So that's why you need to you will use the function, push back.
And what will push you will push the data and how we can figure out the data we have the current A.
So current value will give us the data to so to add value.
So what I am doing here is so it's very simple, we decided that the will contain two things known and
the horizontal distance.
So I created this struct or basically if you are having confusion, you can write class also.
So I'm creating a class of Purita members.
One is the node and that one is the horizontal distance.
And this is our map, right.
Horizontal distance versus the date of nodes.
And then we need to push the route and obviously it's zero.
So Bush route, that is, if this is the route node, I will push the node three and the data, the
distance is zero.
Right.
This is the horizontal distance and this is the node.
Then it's very simple.
Take different elements of different elements, find out the horizontal distance, find out the correct
node, and you need to put this data inside the map.
So my map of current horizontal distance that will give you a vector and in the vector we will push
the data.
Simple.
So the code is very simple.
And now what we need to do, we need to check if left exist, then insert left and the right exist,
then insert right in the queue.
So the next step in doing traversal is we need to check if left exist.
So if.
Left exist, that is left is not close to null.
Then what will do, you will insert this in cute, so cute or Bush.
What will push so, you know, to push two things, what is the node?
So the node is the left of this and what is the horizontal distance?
So when you are going left, your horizontal distance will decrease between the horizontal distance
minus one.
Right.
Similarly, check if right axis.
So if the right of Canada did not exist.
Then you will push in the queue, so queue that push,
you will push two things.
The first is the node, so the node is the right of the current node and what is the horizontal distance?
So you are going right.
And we decided that when we go right, the horizontal distance will increase by one.
So horizontal distance.
Plus one simple and then you will come out of this very loop, your map is ready, your map is ready,
and now we're going to do we need to iterate over the map to populate our answer.
So let's create one answer.
So what will be my answer?
My answer will be the perfect parent answer.
You can see the return type is this.
So let's I tripped over the map.
So for outrating or the map authority is my map darte begin.
It is not close to my map dot end and 80 plus plus items.
I created this variable.
It, it means that it's an arbitrator.
So I need to iterate over the map.
Let's consider this.
So for this example, we discussed that my map will look like this minus one zero one and also so against
minus one, I have nine stored against zero.
I have three and 15 stored and against one.
I have 20 stored and against two, I have seven stored.
So what I am doing, I am I rating, I am migrating over the map.
So it means I iterator I, it means I trade and so I am migrating over the map.
And what is this.
This is the first value so we will access the first value.
I drew first and this is the second value.
So that is ideato second.
To access the list, you will write it at second to access the horizontal distance, you will write
it at first, but we are interested in this idea.
Second, that is the useful thing for us.
So Waterloo.
So, Victor, let's name it, small answer.
So what is this small answer?
This is it, ideato.
Second, and you need to push this answer in the big vector, so Ensenat push back, Small answered,
and finally you will have done your answer.
So many of you will think that this court is correct and it is correct also.
But it is not 100 percent correct.
And why it is not 100 percent correct.
Because in the they are saying that when you are returning the answer, make sure that these values
are sorted, make sure that these values are sorted.
But here what I am doing, I am figuring out this whole answer.
I took this small answer and I directly inserted the answer in my answer vector.
So before directly inserting, you need to first start and then insert.
So it is given the question that you need to first start and then you need to return your answer.
So before returning the answer, we will start.
So we have inbuilt function Sarte.
So small, answered, darte, begin small answer dart.
And now the code is correct.
So I think now our code will work and for the basic basic test cases.
So for example, if your route does not exist, so if the route is basically null, so then you do not
need to do anything, you will simply return this empty vector, right.
You will return empty vector.
So OK, so instead of creating the answer here, let's create the answer above so that I can return
empty answer.
So I am creating my answer here and if the rotational I will return empty vector so I will return empty
vector.
Right, so let's return.
Ouran said, we will return, Emboli answered, so yeah, I think our goal should work and let's test.
OK, so we need to put semicolon here.
OK, so this is actually you will see this is VSL and I have written the complete value.
So I need to write V.L..
OK, so it doesn't know what is actually and that's obvious because I need to write current horizontal
distance, so current horizontal distance minus one and the current horizontal distance plus one now
is correct.
Yep, so our goal is passing the basic discuses, let's try to submit our code.
OK, so basically, let's see what's happening here, it's delayed this, so we are getting a wrong
answer for this and my output is zero zero matching.
So this is two to three.
This is my output and the expected output is three to two.
I have four, I have four, so the expected output is three to two.
Let me see how many discuses our goal is passing.
OK, so we passed 14 out of 32, so we do not need to sort of strange.
Let's try to it again.
Interesting.
Here we are passing 16, so.
I think the test cases are not correct.
Because the question is asking us to start before returning.
OK, let's try to sum it one more time.
Well, I think there is some problem with the test cases, because here you can see, you can read that
we need to we need to return our output sorted by their values.
So that's why I'm returning one, five, six here.
But when I am submitting the answer.
So it's saying that the expected output is three to two.
I think this test case is wrong because the expected output should be sorted.
We read that the data expecting that the output should be sorted.
So two to three is the right answer and not three to two.
So I don't know, like what's happening here, why they have given this test cases or maybe we are missing
something, but I don't think we are missing something.
And if we try to draw that result, it will be like this one three, then I have one here.
I have zero here.
I have four here.
There are two here and two here.
This is not creating and our output is correct because first one will be zero, then next one will be
one.
And then I have three, three, two and two.
Right.
So they are just directly returning without sorting them.
And the question they are asking us to sort.
So output should be two, three, three and then four.
So I think the best case is for this problem are not correct because our goal is giving the right output
right.
They are asking us to start before returning and we are sorting before returning.
So it seems very strange that lead court test cases are wrong for this problem.
They are clearly asking us that sort and not according to their values.
And when we are done, we are sorting through our output is correct.
And this problem has definitely some issues with the test cases.
Right.
So our goal is correct and what I will do so I will figure it out by whether we are doing something
wrong or we are doing something right.
So I think we are doing everything right.
And the problem definitely has some issues.
So this is all out this video, guys.
And let me know if you also figure out something, because I'm not able to understand what we are doing
wrong.
So this is all about this device.
I will see you in the next one.
Thank you.