Hi, everyone, welcome back.
So in this video, we are going to write the code for this problem distinct no in Windows, so let's
start writing the code.
And we discussed that, what do we do?
So let me take one example.
So if the window size is three and if this is your Eddie.
It's a one four one five seven, eight and nine or zero letter.
This is your that is what we need to do.
So first of all, we need to create one map and this map, what it will contain, it will contain the
element and the count of that element and window size three.
So first of all, let's create one map and also we need to return answer any.
Right.
We need to return answer vector.
So let's consider these two.
Let's create these two things first.
So let's create one map.
Key and value key will be integer and value will also be integer and my map.
So what we need to do, we need to create one answer, so victory and answer, and now what is the first
step?
I think the first step will be what we need to do.
So we will insert all three elements that is ALBE elements.
So I will insert, first of all, all these three elements in the map.
So my map will look like this one.
So the count of one is two and the count of one is count of this one.
So first of all, I need to insert these values one for everyone.
So the count of one will become two and the count of four will become one.
And after doing this, what the unique elements in this window, that will be my map glasses.
That is to my eyes, I will contain two.
So let's do this much amount of work first.
So first of all, as discussed, what to do?
We need to go to B elements that these three elements.
So let's insert so my map.
Of age of 80 plus plus, right, I'm inserting the element in the count and after coming out, my answer
will contain two and what is two?
Two is basically the number of entries in the map.
So answers Eddie will contain will you two and two is nothing but my map that size the number of entries
in the map.
So after this much amount of work, my answer is containing to solve what I need to do.
I need to start one loop from this and I will go till the end of the area.
So what is this index?
This index is three three means B, I will start from industry that is index B and I will go till the
end.
So what I will do first step is I will insert five, so I will insert five and the count of five is
one.
Five is coming for the first time.
So you are considering one new element.
That means you need to remove one element because the window size has to be three.
So you need to remove one element in which element you will remove.
You will remove element one, you will remove element one.
So from this element from index three, how can I reach index zero?
Nothing.
What do you need to write, i.e. minus B?
So I is basically three.
Right.
You are standing at the next three and the value of these three.
So three minus three will be zero.
So what do you need to reduce the count for this element.
So you will reduce the count and the count will become one.
So you are considering this window for one five and we need to find out the number of unique elements
in this window.
And that will be my map Dartez, which is containing three entries so that you will insert three in
this way, we will move forward.
So what I need to do, I need to start iterating over the array from index three.
And what is index three?
Index three is nothing but B only.
So I understand it says.
I placeless, so what we need to do, we need to insert the current element, so I am inserting the
current element in the map, but now the window size increases by one.
So we need to maintain the window size for maintaining the window size.
You need to remove the element that we will not consider.
Minus minus.
And finally, what we will do.
So how many unique elements are present in this particular window?
That will be my map, not size.
And finally, what do you do after this loop is over, you can return your answer and that's it, and
that's it.
So let's see whether this quote will work or not.
So what I'm doing, I'm considering the current element.
So I am pushing this element in the map.
So I'm considering five.
That's why I am pushing element five in the map.
And now I am talking about this window, so I need to remove this element from the map, so that's why
I am doing I minus B and I'm reducing the count, so I am reducing the count and count becomes one.
So in this way, we will be able to update this map and after updating the map, we can push the elements
inside the answer.
But this code is not correct way.
So what will happen?
Let's see.
So the next element that we will consider will be seven.
So you will insert seven and the count of seven is one since you are doing seven.
So you need to remove element four.
So what you will do, you will reduce the count by one, so reduce the count by one.
So the count becomes to zero, the count becomes zero.
And what they are doing next, you are figuring out the size.
So what is the size?
Map is containing four elements, so I will insert four.
But four is wrong because in this window, in this window there are three elements.
And since there are three elements, the maximum answer can be three and all the three elements are
unique.
So your answer should be three, but you will retain the answers for and via four, because after reducing
the count, when the count becomes zero, you also need to remove the element from the map.
So you also need to remove the element from the map if the count becomes zero.
So you need to check that if after reducing the count, the count becomes zero.
Then you also need to delete this element from the map, so my map dot it is we need to delete this
element and this element is.
I minus B. So now this is correct, discord is correct, but one more thing, what we need to handle.
So it is given the indication it is given in the indication that if the value of B is good, then and
and is basically ignored.
Sighs So it is given the question, if this is the condition, then you need to return Empty-headed,
then you need to return Ampitheater.
So let us use this condition also.
So it is mentioned in the question.
That F B is good.
Then Edward says, then your answer will be empathy, so you will retain empathy, Eddie and I think
our code will work.
So let's test our code.
Yep, so basically our goal is working and let's try to submit our code now.
So, yes, basically our goal is working and we can also discuss the time and the space complexity for
this.
So what we are doing here, we are creating one map.
And this map will contain a maximum of bee and trees at one time.
This map will contain maximum bee entries at any given point of time.
So the space complexities big off bee.
And we are also returning this.
We are creating this vector and we are returning this.
So since the question is asking us to return the answer in the form of vectors, let's not include this
in calculating space complexity because it is the question prerequisite.
It is the question demand that you need to create and return.
So let's not let's not consider this in space complexity.
You may or may not consider this, but let's say we are not considering this.
So the only extra space that we are using is basically this map and this map will contain maximum of
being present at any given point of time.
So space complexities, big off B, what will be the time complexity?
So first you are outrating Dahlby and next you are retreating from the number of elements and minus
B times.
So in total you are iterating basically you are iterating.
See, this is your area.
First you are trading only this much will be the next.
You are reading from this till the end.
So basically I can say I am I reading like this.
So time complexity is big often and this operation is constant.
This is also constant again, constant, constant and constant.
So all these operations are big, often so time complexities big often.
So this is their time in this space complexity.
And if you are not satisfied with the space complexity, so we can also consider this instead of calculation
and what will be how many elements in this answer will contain?
So there are elements, right.
And the window sizes be so we can say that space complexity will be and might not be the answer.
Addresses will be and minus be.
So if you will add so B and B will cancel out each other.
So space complexity is also a big often.
So finally, the time is big often and the space is big enough and when you will consider the space
given to this answer vector.
So this is the time and the space complexity for this problem.
So this is all about this video.
Guys, if you have any doubt, you can ask me in the junior section and I will see you in the next one.
Thank you.