Hi, everyone, welcome back.
So in this video, we are going to write the code for the function flip, so let's start writing the
code.
So first of all, we need to return one vector and this vector will contain two values left and right.
So let's create one vector vector of integers.
Let's answer.
So it will be Offsiders two and let's say the initial values, let's initialize to minus one.
Minus one is an invalid index.
Right.
So what this line will do so this line will create one array of size two and initialize all the values
that is initialized to values by minus one.
Right.
Very simple.
So now what we discussed, we need to find out the value of left and right.
So let's take two variables left, which is initially zero, obviously, and let's take one variable
rate zero.
So we need to find out the values of left and right.
Let's also find out the number of elements the size of the string.
So which is a size
and for goodness algorithm.
We discussed that we need one variable maximum sum, which will be initially minus infinity.
We need one variable, Göransson, which is initially zero.
And then if you remember, we need to iterate over the area
and what we used to do.
So we used to write like this current sum plus equals to a of a.
But now this line will change how this line will change.
So basically we discussed that if this is my string zero zero one one one, then what I am going to
do, I will convert zero into one and I will convert one end to minus one.
So that means my new string will be one one minus one minus one minus one.
And I need to apply Caden's algorithm on this new string, newly created string.
Right.
So one way is basically you are ideato with the string and you manually change a zero to one and one,
two minus one.
So that's the one way.
But there's no need to do that y because whenever we will encounter zero, what we will do.
So ultimately what we will do, we will find out the maximum sum summary for this.
Right.
So whenever we will encounter zero, we will add one.
Whenever we will encounter one, we will add minus one.
Right.
So no need to change the original string.
So what we will do.
Whenever we will encounter, whenever we encounter zero, what we will do, I will add, plus one,
right?
So current some place close to one.
So whenever I encounter zero hour and in the ls part, whenever we encounter one, we will add minus
one to current.
Some plus equals two minus one or reconnoitering some minus close to one.
Right.
Whenever you encounter zero and one whenever you encounter one at minus one.
So that is what I am doing here late.
So no need to write this line and then it's very simple now.
So what we need to do first of all, let's remove this line.
OK, so what we need to do if the current sum is greater than the maximum sum, so we need to update
our make some some this is standard guidance algorithm.
So let's update the maximum sum, which is equal to the current sum.
Fine.
And we also need to find out we also need to update the right so right will be the current index.
We are updating the maximum sum.
So what we will do, we will update our answer also to answer of zero that is left.
So what be the value of left?
It will be left plus one.
Remember, we need to return our answer as one index.
So that's why I'm adding one and similarly answer of one that will contain.
Right.
So the right will be the value of right.
Plus one.
We need to report our answer by adding one.
And similarly, the next part of them is if current sum becomes zero, then you need to make some equals
zero.
And also you will update your left.
So your left will be a plus one.
Simple.
And finally, what do.
Finally, we will return our answer and that's it.
So will the scorebook.
So I think there is a very tiny problem with this code.
And what is that problem?
So if my string is containing all the ones when one and when.
So what is my vector, my what is my area?
So my area is containing two values.
The value of left is minus one.
The value of is minus one.
So when will apply cadenced algorithm.
So this will be string minus one, minus one and minus one.
Right.
So my Caden's algorithm, what it does, it returns the maximum sum.
Sabeti And what is the maximum Zanzibari minus one is the maximum sum.
Sabeti Right.
So whatever array will contain it will contain the value of left plus one and the value of right plus
one.
So that means this index rate when Gormogon.
So you will report your answer is when Gormogon because this is the maximum Saberi.
So the value of left will with this value of right will be this.
So is this answer correct?
No, we discussed that of string is containing all the ones.
Then your answer array must be empty because in that case you will not apply flip operation.
If you will not apply flip operation, then the value of left and right will not exist and your area
will be empty.
Right.
But in this code, according to this code, what this code will return.
It will return one.
So what we can do, we need to handle this case and how we can handle this case.
Very, very simple.
So before returning the answer, we will have one check that if the maximum sum is minus one.
So in this case, what is the maximum?
Minus one is the maximum sum.
So if the maximum sum is minus one, that means all the values in the string are one, all the values
in the string I want.
And in that case, you will basically clear your area.
You will delete both these entries and you will then empty adding.
Right.
So before returning your answer, you need to have one check that if the value of maximalism is equal
to minus one, that means your string is containing all ones.
And if your string is containing all ones, then you need to return ampitheater.
And for returning ampitheater, we have a function on Sadaat Clear.
So what this clear function does, it will remove all the elements from the error rate.
So we will clear our area and then we will return our answer.
So let's see whether our code passes the basic test cases or not.
OK, so
I brought this by mistake.
Yep, so our goal is passing basic discuses now let's submit our code.
Yeah, so basically our court has passed all the test cases and it was just the variation of cadenced
algorithm.
So the code is very simple and very easy to understand.
So I will share the code with you.
And if you face any problem, so do let me know.
I will be more than happy to help you out.
So this is all about this free device.
I will see you with more interesting question in our next videos so that the guys that.
Bye bye.
Thank you.