All right, guys, here is the code for the sliding window.
Let's jump into it.
First of all, I have defined my frequency array globally, so it's in half hour of Emax.
And then I have defined my function and my function is going to be called sliding window.
And it's going to have three arguments.
And the arguments are my array, its length and the last one k the maximum number of distinct elements
one subarea should have.
And initially I am going to have my Conder equals zero, which always counts.
How many Distin number my current salary has.
I am going to have as or equal to zero initially obviously.
And my right pointer, which is going to be always the marks are of the current left pointer and I just
found initially what is the marks are four left equals one.
So four right equals one right.
Less or equal to N right.
Plus plus I have just increased its frequency.
So plus plus frequence of Arite.
Remember this plus plus is here because what.
Plus plus X.
Let's see does is that the variable X is first going to be incremented and then it's going to be compared
to the right side operator.
So I am going to increase the frequency and then if the new frequency of the increased one is equal
to one, that means I have a new element in my summary.
So-called or plus.
If counter is greater than key, I should stop.
So break.
And after finishing the for loop, I have two possible outcomes.
Either the four stop the execution through here through this break.
So counter is greater than K either.
It's stopped normally when right equals and plus one.
So if it's the second one and right became equal to and plus one that means that the whole array is
valid.
So if control is less or equal to K, so it didn't break through here, that means I should return end
the length of the hole or else it means that this four loop finished execution through here.
So for the current value override control became greater than K, so obviously I should decrement the
right in order to be valid.
So I should delete the value a right from the frequency rate.
That's why I have minus minus frequency of right.
And you've probably noticed that earlier I had the bug in this court because I've only subtracted one
from the frequency rate, but I wasn't updating the counter afterwards.
So if minus minus frequency of this value becomes zero, that means this value is not anymore in my
summary.
So contrariness means after that I just decrease the right right.
Minus minus.
So now I know the marks are four left equals one and a half all the elements in my frequency right now
I do know what is the marks are for left equals one.
So my answer is going to be initialized with right.
OK, the first valid sabera that they found is from one to right answer equals right now I have my main
for loop which starts a position two and goes all the way to an end for each new left.
First step.
I should delete the element a of left minus one from my summary.
So if minus minus frequence of a left, minus one becomes zero now count or minus minus.
So I deleted from the frequency.
Right.
And then what do I do is I increase the rate as many times as I can.
So while the rate is less than N right.
Plus, plus I charge it into my frequency array.
So if plus frequency of a rate becomes one hundred plus plus and then if counter is greater than Kate
I should stop.
So break and again after this while loop successfully finishes execution, I have two possible outcomes.
All right.
It finished for here or it finished naturally from here and there should check if counter is less or
equal to K, that means that it finished naturally.
Right so right became M and the valid Southbury.
The marks are of the current left is going to be AB, which means also that for each new value of left,
right is going to always stay at position.
And so I know that every valid summary that I'm going to find next is going to have a level that is
definitely not greater than this one from left to F, so I just return makes of ends all the sub areas
I found by now and this one and minus left plus one if not and it finished from here.
That means the last right is not good.
So I should delete it from the frequence first.
Again I have about here because again I forgot to update the counter.
Alright so I'm just going to keep this box in the Vidor's because first of all is harder to write code
on the whiteboard and you always should be by your code.
And second of all box and.
Mistakes are natural, right?
Don't you ever get in panic or get mad at yourself if you have mistakes?
Because trust me, I have probably solved and taught this problem for 10, 20, 30 times.
And I still make mistakes when writing the code.
This is very cool.
That's why you should always debug.
That's why you should always take a lot of handmade examples and test and see if your program is right
or not.
So now if minus minus frequence of this element became zero count or minus minus, then I am necromantic
the right pointer.
So now I am with the right pointer where I should be.
And I have all the elements in this sabera in the frequency array for this left.
The marks are is now right.
So Assar is next between answer and right, minus five plus one.
After finishing the main for loop we will just return the answer and that's all that I really hope you
understood.
And they can do this by.