The video provides a sharp demonstration of algorithmic intuition, showing how a complex sum minimization problem can be elegantly solved through simple backward processing. It is a concise masterclass in the efficiency and focus required for high-level competitive programming.
Inmersión profunda
Prerrequisito
- No hay datos disponibles.
Próximos pasos
- No hay datos disponibles.
Inmersión profunda
Screencast || Spectral::Cup 2026 Round 2 (Codeforces Round 1100, Div. 1 + Div. 2)Añadido:
[clears throat] >> chicken Okay.
They didn't start in some line where I slime is at position A I on the line you have to perform the following operations a number of times select integer X for each J is the name.
If you have just this, then just do this or do this.
For each slime count the difference and then the minimum.
Is that the maximum?
minimum maximum max maximum Yeah, int n c n and right. It's just the maximum difference from each element in n c n vector int a n for I equals zero I less than n I plus plus c n a of I right answer is equal to I will initialize by int max What is the value of a of I? It can be up to 10 to the power Oh, it's up to 1,000 only, right?
for int I equal to zero I less than n plus plus for I uh int the value is equal to zero for int j equal to zero j less than n, j plus plus of val is equal to max of max of value comma absolute value of int of absolute value of a of i minus a of j uh answer is equal to min of answer comma value c out answer It's 2 0 I think n squared should pass because n is up to 1,000. Failed or what?
I Okay.
n is up to 1,000, no? i is equal to zero i less than equal to 1,000 I have to choose this, right? 1 to 1,000, right?
1 to 1,000 and then just do i minus this.
And then it should pass.
Slimes on a line.
I think they should pass, right?
Because a of i is up to 1,000 oof Wait a minute.
What if Huh, if sum of n doesn't exceed 1000, then it's good.
Hmm, then it's good. Then it's not T 80.
Ah, yeah, it's positive. Absolute cinema. You find yourself with two arrows of positive indices A and B both of length n. You are You are to perform the following operation a number of times. Select an integer i and swap A of i B of i. Determine the maximum value of max of A plus summation of B of i.
Okay, so you can just apply any number of times. You can swap A of i and B of i.
What is the maximum value that you can attain?
All right.
If let's suppose what because I need the maximum across B of i, right? So what I will do is A of i, B of i, whichever is maximum I'll update that to B.
And Yeah, sure. That is just it's like go I just update the maximum values to B.
That should work because all the values of B you will always take.
Ah. [sighs] A of n, B of n, B of n.
>> It should pass.
3 3 18 27 Why it should pass because see uh I will always take the maximum value in B.
Because if I don't, I won't include the value. But if I take I will always include that value. I can even for A I can have multiple maximum values.
Uh if I read the question right.
Then it has to be simple.
But if I read the questions right.
Just don't tell me key I read it wrong.
And made Greater than 9, right? It should pass.
What is my standings now?
Oh.
It's 1,000.
Oh, nice. There are people who are Oh, tourist and everybody is solving faster than me.
>> [snorts] >> This is the easy version of the problem.
The difference between the version is that in this version you must minimize the sum. You can have the version before all the You have an array of length n which contains non-zero negative integers.
But not non-zero.
>> [snorts] >> So, you can take an index i where a of i is greater than zero, and for each index j which is less than this, you can you will do it my say that equal to minus of a of i.
Ah, this is This is easy. What is the minimum now?
Should be minimum. And what do you need to output?
>> [clears throat] >> Okay. Okay, so you will do A of J to minus A of J. So, it's like you choose the positive element and then behind that multiply all the negative elements to negative one.
So, I look three.
So, let's suppose there are N indices and I choose to perform N operations.
So, what will happen is if I do first operation and I do second operation after that, so the effect of the first operation would be cancelled because let's suppose first operation made some elements negative.
It will make all the elements negative behind that. But the second operation will again make that all the elements positive and will make the between elements negative.
Right. So, let's suppose I only have one operation. If I have one operation, the elements all the elements behind that would be negative. Like multiplied by negative one.
The well, signum function will switch.
And if I take three indices, let's suppose A, B, C, what will happen? We will swap this.
Then it will we will when we do this, it will swap again and this will be swapped.
This will original swapped original and when we do this, this is swapped, this is original, this is swapped.
So, it's like you can do it possibly N times, right?
So, what you can do is Okay. So, what you will do is you will try to Okay. So, And what is the hard version?
This is the easy easy version.
Minimal a maximizes the sum at the end. All right, all right.
Hmm. So, what I'm saying is let's suppose I perform any m arbitrary operations on i1, i2, i3, i4, i5, which means what it will happen is if I do i4 i5, this will swap. This will remain original.
This will swap and this will remain original.
So, if I if I perform both of the operations, the last operation will decide though.
So, like for m operations, i of m i of m will decide for i of m minus one also, whether it's parity would change or not.
So, what is the we we need to minimize the sum.
>> [snorts] >> We need to minimize the value. So, which means what we need to do is we need to for every positive integer this.
So, let's suppose for i1 I swap, what will happen is either we will take this sum or we will take the So, okay. So, let's do this way.
We let's assume all the values of positive integers where are the occurring.
For the first set, we can calculate what is the sum. Second set, we can calculate the sum. Third set, fourth set, fifth set and so on until the last set we can calculate the sum.
Now, what we will do is we know if I flip this one, this will become negative.
But I flip this one, this will become negative, but this will become positive.
Right?
If I flip even this one, this will become negative.
This will become positive and this will become negative.
If I swap this one, so so it's just a positive negative positive negative sequence. And all the other sequences are stand alone different. So, let's suppose let's calculate all the values, store them in a vector.
If I is zero, there are zero numbers we can not add any operation.
Okay, we will just add at the end.
We'll count number of positive integers.
If count is zero, then see out zero, return. And if count is this, you will get multiple answers. What you can do is one value will be this val, which is the current value. Second will be minus this.
Or what if why do I want to do negative and if I'm doing this second one, why do I want to do this positive and this negative?
So so what I mean is let's suppose I have calculated all the segment values.
For all segment that is positive these values are positive, what I'll do is I'll just that at the end subtract the negative one.
What is the maximum such that So, it's like I I can take the segment sums.
And let's suppose this is positive, this is negative, this is positive, negative, negative, and positive.
This is positive.
Last segment I'll switch this, so this will become negative.
Since this becomes negative, now this all changes. So, I will just flip that to make it positive again.
To make it negative again.
So, this is done two times.
Which means it's still positive, so I'll swap this one to make this negative, but this will become positive.
I will swap this one again to make it negative.
>> [clears throat] >> So, just go till the back. If this is positive, swap this.
If this is positive, swap this.
If this is positive, I'll dissolve this group.
And 21 Sure, thank you.
Okay, so I'll just mix segments and I'll try to flip this such a way that I'll try to flip it positive become negative. Then if it's odd, this becomes positive. Oh, sure, that is the answer. N A of N, right?
I'll have to sequentially do it.
And I'll have to always do it from the back. Why from the back? Because the last operation determines what is the value of the previous operation, so it's better to start with the last one first.
>> I'm just going to do max. What I'll do is I'll just set up on count also.
I'll make a vector B.
And I'll make some.
If A of I And if A of I is positive, then what I'll do is I'll make every element including I negative, right?
Mhm.
If A of I greater than equal to zero, greater than zero, positive, then we don't push back some.
And okay, so some plus is equal to A of I. I need to add this to the sum first.
Add it to the sum. If A of I is positive, which means this sum will become push back to this.
And then we'll do count plus plus.
And if you do count plus plus, and then we'll then we'll set sum to zero again.
Else, we'll just only do some.
Else, we don't do anything. Because like if it's less than zero, it won't do anything. So, it's greater than zero. We will push back it to the sum, count plus plus, and sum this.
And if some if sum greater than if sum not equal to zero Even if this is negative, then I have to make it positive. Even if it's just not this v.push_back sum Right?
if count equal equal to zero c out zero end l return Okay, so I just need to return at this empty array and so let's see out zero end l Because if count is zero, then you cannot it and this, right?
Just print one end of line.
a zero end l c out end l end l return else vector int path Mhm. And vector int i n d indexes I will store this.
i n d.push_back i So, what I'm doing is Okay, what do we Why do we even need to store this?
We don't even need to store this as well because If there is no positive integer, if we can't even negate this out.
There has to be a positive integer. So, this is good. It has to be commented out. Index and I'll just index put back this path.
Okay, for int i equal to v.size i plus plus Oh.
i greater than equal to zero i minus minus.
I v.size minus 1.
int step is equal to 0. How many steps we have taken till now?
If steps mod 2 I got it's odd.
Which means I have swapped once.
So, let it let's suppose Okay, let's suppose it's even.
When it's even So, if v of I is greater than 0 then path plus is equal to path dot push back IND of I because index of I will have the same size as this.
x of I plus 1 and step plus plus.
I could see I'm this is positive then I need to make minimize this, right?
And if step is odd which means I have to negative this. If v of I is less than 0 path dot push back IND of I plus 1 and then step plus plus and for this, how many steps I have taken?
c out step endl for int I equal to 0 I less than path.size I plus plus c out path of I Just put in the bar and then See what I did.
So it should be zero, four, and then one four. Something like this.
I'll just just test it once more.
This should not be zero. Zero. Why? How?
Zero.
Okay.
Minus one, minus two. This is zero.
And I push back two.
Then it's minus one.
And I push back four.
Okay. So after this, in second test case, this is made. So this is the Oh, what the I did?
I just need to do first go from in find the first positive integer. Add that.
That will become negative.
And apart from that, everybody will become positive. So just go and do this, this, this.
Just add three.
Minus three, two, one.
This will become positive.
Oh, That is not the answer. I got it what I did wrong.
Mhm.
That is not required.
I can just do I will do this.
I'll do this. I'll do this. If count is equal equal to zero, then this.
Not push back.
I In index, I'll just push back the index of all positive elements. And I'll do count plus plus if this is this.
Step is zero.
Right? Path is zero. I will just tell for int I equal to zero I less than IND.size I plus plus Now what I'll do is if IND of I greater than zero see out >> [clears throat] >> Check if this is positive, add this to the path. This will become negative.
I think do do one thing.
If I indeed What it is?
Five.
I think this is I this was a easy all right. Did over thinking on this.
It has to be just this only. You go from back, I check if it's positive, it's positive then subtract by minus one and then add that value to the path because all the positive elements will become negative one. And then check if there is any negative, add it to positive. Oh, if there is any negative, don't do anything because it's already negative. It will minimize the sum.
I do make it positive, right? This was very easy. I overthink on this a lot.
Which means I'll be pretty bad on my ranking.
I'll have to make C and D fast.
Uh no.
I took a lot of time in solving this.
I need to make C2 even faster. So, C2 is like we have to maximize the sum. So Now, if you want to maximize the sum, what we do is we want to subtract the negative values.
And we will do only for this, right? If A if I greater than zero, then we will do for this. That is the constraint.
Now, I need to do subset counting, something of sort of like that. So I think that then now the sub sequence counting will work.
See.
I just need to maximize this and I just need to give this output.
So how will I maximize the sum? So, if I multiply this by negative one, so what means See, see. There's positive number. There are a lot of negative numbers behind it.
Then there's positive, then there is negative.
So, there is positive number, positive number, positive number, then there are negatives, negatives, negatives, and negatives. So, I can't touch these negatives. And there's positive one. I can't touch this positive, either. So, in this in this in this, I have some subsequence subsequence. If I choose I change this Let's let's suppose this value is positive, this is negative, this is negative. So, this become negative, this will become positive, this will become positive.
So if it's positive, I'll not take this.
I'll just definitely choose this.
Because if this becomes positive and it also becomes positive.
>> [clears throat] >> And this will also become negative, right? Then this is one segment. There are three segments.
And let's say this was this will be discarded. Now this is positive, this is negative, negative, this is positive.
I will not take this. I will change this one though.
If this is negative, so this will become positive.
Count will become one.
This will become positive, this will become negative. If this is positive, I won't change this either.
If this is negative it was positive.
And if this doesn't becomes this Okay.
So see how the solution looks like. So there are sub segments.
If the sum is negative for this segment, then I'll just change flip this to make this positive.
Which means now each and every value will flip.
So I can't do it sub segment wise. So it's like, okay, and n is dependent on the fact that it is positive number.
You decide to flip it or not.
So why would I even flip it? I will flip it just only because before the next positive So, if I Okay.
What will a positive flip do?
So, a positive flip will flip all positives to negatives and all negatives to positives.
Do I only flip once?
If I flip multiple times, what will happen?
See, after a flip, that particular index becomes negative.
So, there's no point to do multiple flips.
Right?
Um I think this is not that hard. This is not that hard.
There's no point to do multiple flips because each flip will turn that element to negative. So, I need to do a one flip for if I do a one flip, it will change all the positives to negatives and negatives to positives.
So, I'll just maintain two arrays, prefix sums array, of total positive and count what total positive sum encountered so far and total negative sum encountered so far.
If A of I is positive, and I check, okay, the total positive sum encountered so far is less than the total negative sum encountered. If I flip this bit, what it will do is it will in- make the positive sum equal to negative and negative equal to positive.
So, the positive sum encountered so far will be greater than the negative sum encountered so far, which will maximize this.
Now, it will still increase my step by one.
Now, I go back and see, okay, till here, if A of I is negative now, because it would have been become positive.
Up, positive negative or negative positive or negative If the total negative sum encountered so far which was the actual positive is greater than this. So, if it's odd if it's even steps are even if negative is greater than positive, swap.
Step plus plus.
Else, if positive is greater than negative swap.
S plus plus.
I think that should be the answer.
L vector int P1 n comma 0 if n is greater than 0 P 1 of 0 is equal to A of 0 else P2 of 0 is equal to A of 0.
Hm.
For int I equal to 1 I less than n I plus plus if A of I greater than 0, then uh P one second.
P1 of 0 is equal to P1 of Oh, P1 of I. What I'm doing?
is equal to P1 of I minus 1 P2 of I is equal to P2 of I minus 1.
Uh if A of I I can't hear you.
greater than zero.
Oh, damn.
If your voice greater than zero, pre one of I plus is equal to A of I.
else pre two of I plus is equal to A of I.
Okay, and uh Count is zero to hey. Count will be the steps.
Vector and indices is zero to hey.
Okay.
If count is equal to zero, Okay.
If I were greater than zero, the count plus plus. If count is equal to zero, so I have zero to done in the length path.
I'll give you a path for a minute.
Value is this. Step is this.
But I is equal to n minus one.
I minus one is ticket.
Now, A of I multiplied is equal to value.
Right?
If step mod two equal equal to zero. Which means it is the initial configuration to we have but if pre two If A of I is greater than Okay, shouldn't it be just DP?
Because I have two choices, right?
Either I take either I not take.
For each value, I have two choices.
I think I did I did I don't think.
I think it should be DP instead of greedy.
Is it DP? So, why is greedy not work?
So, if I'm saying it's positive, no, but steps will also increase.
If it's positive, don't just see.
What if you make sure this is positive, if I is greater, you check if I is greater than this.
And okay, so it's greater than zero and negative sum is way more greater, but negative sum was still here, was way high. Positive sum was way high. Here positive sum is way much, and here negative sum is very less. Like it's all positive, but then this is high. So, you do this.
And there's also one positive here.
Instead of doing this, you will do from start.
>> So there's one positive integer, two positive integer, three positive integer, and so on. So if it's one positive integer here somewhere here I can just choose to flip this.
I will choose check whether this satisfies or not. If it does, add this.
How will I check?
This sum if it's greater than Okay.
Okay. Okay. Okay. Let's suppose this maximum was very high.
>> [cough] [clears throat] >> This is a quite interesting problem, I suppose.
Hmm.
Goats was the solution to this. Oh, one second.
No.
How will I maximize this sum? I need to just need to maximize this sum, right?
Okay, well, let me just check the D problem as well.
Till then.
I'm solving C2. C2 is easy.
So, if I one positive it will always Okay.
What if What if I use C1 only easy one.
See there are negative positive numbers.
A lot of positive numbers.
Some negative negative negative. What if till this positive I consider this array only.
And in this I do this.
And I make sure this minimizes the sum.
And then I just swap this.
If this is positive.
If this is positive I leave it alone.
Else I swap this.
Else I swap that. That is good. That is That is a brilliant approach. So my approach is So let's suppose there are n elements.
The n elements out of which k are positive, right?
So I'll So I can break the array into one to like a k minus one at the positive number which means this array A dash will be a new array and I can use C1, the code that I wrote to minimize this.
Now if I minimize this minimize this, then this is always minimized. Then I can take the k-th positive number.
If I minimize this if all the negative sum plus K is Okay, but there's one more problem in this.
Even if I minimize till K minus one it and I have switch the bits through K.
What if the K at number exceeds?
I don't know.
And both the cases it's this.
And so see the code last sale.
Correct.
You can possibly use this to give positive number I have.
It's the picture of positive number for sure.
It's going to minimize the code.
>> This is cool.
chicken This.
One thing.
I think it's a solution. This is the solution key it's has to be something like he go from back.
I think you can build it up on this be okay till this this is minimum. Now I swap this this become maximum. All the negative elements here change and this becomes negative.
So this minimum plus this swaps this. But let's suppose this minimum plus this negative still is less than in the absolute value in terms of this positive number. So if I minimize negate this not maximize.
Okay.
Start from first.
If there exist any arbitrary sequence key.
I know those values will sum. So if there is there is something key there is existing arbitrary sequence key, so it will always start from I will always start to fill from back.
Right?
So let's suppose I decide to switch this positive number.
That's the only case when the previous positive here.
All right, now this will become negative.
So what I can do is for a number till a number I can make all the numbers positive.
All all the numbers negative I suppose.
I mean.
This all will become negative.
Till this positive.
That is one case.
I can make all of this negative until this positive and one more.
I can make this negative.
All the other elements positive.
I'm going to make This is the only case, right?
And So, it's like I can make this all elements positive. Only this element negative. And this all the behind elements will be negative only.
Or I can do the same for each positive number.
Positive.
>> So, let's suppose there are n positive numbers.
n positive numbers I can make for i k till i k all the values positive then I can make i k minus one negative.
And do I need to check for all values then?
That is the concern.
Do I need to check for all values then?
So, I just need to check for the last index we need to subtract this value.
So, if I apply an operation on like the last element of k, the last positive element will become negative one and all the positive elements before that can be made plus one.
So, what is the last element that I choose?
So, it's just either you choose the last, second last, third last, fourth last, fifth last, and so on.
And that is the answer, I think. So, there are M positive elements or K positive elements, then you can make till if you choose the last element of a subsequence like this was M elements subsequence of M moves, you can make all the elements till the M-th positive number negative and then a positive and then M can be negative. M will be negative.
Then M will be negative.
But in that, if you're applying a pressure, you're making all the negative only. So, you're doing this only.
So, you will just move prefix of this.
So, A of zero uh I will just do prefix.
Prefix of zero.
And I will do int n suff n comma zero.
Pre of zero is equal to absolute of A of zero.
Absolute value of A of I.
And moreover, uh suff of n minus one is equal to A of n minus one.
And for int i equal to n minus two i greater than equal to zero i minus minus suff of i is equal to suff of i plus one and suff of i plus is equal to a of i Right, now I just need to check what is the right count plus plus count plus plus and then I just do this right if count equal to zero count is equal to equal to n c out zero because if it's n you need not do anything hmm hmm and if your count is n minus one if this is negative now what I will do is I can choose any positive number turn it to negative and turn all the numbers here positive that is what I'll do count is zero path is this right? Now, for int I equal to zero I less than n I plus plus if a of I is greater than zero.
int val is equal to zero int i n d is equal to zero.
All right, path p m public int i n d val is equal to max of value comma p of i plus sub of i and sub of i p i minus two times minus three times a of i.
Right?
int f val is equal to this.
Okay.
So, prefix of i plus suffix of i this will be two times this minus three times a value i.
If if the value is greater than val val is equal to f val and i n d is equal to i.
I'll just go ahead with this.
Then I'll find the index of that positive number, okay? I'll make a change going to here. So, there will be some last positive integer that I'll change and I'll set this as this.
Now, still index, right?
int val is equal to one int val int mult multiplier is one.
for int i is equal to i n d i is equal to zero.
If a of i greater than zero.
>> [snorts] >> Oh, let's suppose there are multiple positive integers.
And it is part of the path now.
>> Got to do anything any of I prefix suffix absolute of zero.
Count is zero zero to count plus plus if count is equal to zero count is equal to n c out zero and then vector in path I and zero.
Yeah, this will work.
I think I couldn't think of the solution.
I think it should not give me this.
One wrong needed to change.
Wrong on the pretest two.
I think it's too awesome.
Big hit by a question.
Okay.
Why is this not working?
X second X second X second.
See one thing one thing here it is key in this I am always same.
First of all, val has not to be zero. It has to be a long min.
Because that can be the minimum value as well.
The minimum value can be Suppose it's very less very negative value and you just flipped one coin.
So, this can be done.
But, there can also be one value where no one needs to be flipped.
Okay, let's suppose all negatives There's one zero. All negative, there's one zero. There's all negatives.
If this current positive this one and it is minus one.
All right.
>> So, in this case I was always taking into consideration this can be such that you always I have to take this. So, this is the baseline value of summation of all the values.
And if this is greater than zero I'll have to run this again, right?
Okay, see what I've done is I have set the baseline value to be the summation of all the values. If there is no value that is greater than this which means IND will always be that value.
Skip this.
Which means in that case you don't need to take any value.
That would be a base case I'm missing.
Okay, if there is no value that needs to be flipped.
Okay, there can be one value that needs to be flipped, right?
If you flip this.
Don't do this. Just flip this.
Because if there is one value I'm flipping that value to because it's Let's see what happens.
Might be wrong.
I need to set the baseline value to this.
If I don't want to swap.
This can be wrong.
Flip this first. Come on, bhai. How can it be wrong, bhai? Many like this key on.
How can it be wrong?
Yeah, yeah.
>> [sighs] >> Problem D is also cool. Cutest could solve it in this much time. Let's try problem D.
Pretty bad rank, but take a chill.
You are given two arrays of positive integers A and B both of length N. All right. You'll perform the following operation exactly N minus one times.
Let M be the current length of A and B.
Oh, Tejas was able to make it. Okay, Tejas made Tejas did not able to make it. Codes was able to make it. Hmm.
Uh okay, you're given two positive integers A and two positive two arrays of positive integers.
You are given X second. You call it or no?
Hmm.
>> [snorts] >> You're given two arrays of positive integers A and B both of length N. You will perform the following operation exactly N minus one times. Okay. Let M be the current length of A and B. Note that length will always be equal. All right. Select an integer in Okay, so let's select any index.
Let S be the Okay, you can't select the last index though, but oh, okay.
Let S be the multi set A Okay. Just if select any index, these two elements and B K these two elements will form a multi will be belong to multi set S.
Sort the elements of S. All right.
Now replace AI AI plus one with S2 BI BI plus one with S3. More formally replace A with all good and B with this.
After performing all operations, there will be exactly one element [clears throat] remaining in both A and B.
Okay, so what you are doing is so you take two elements from A, two elements from B, you sort them.
You take Thus, out of those four, you take the second and third max element and then the second max you append to A, third max you append to B.
And you want to do this while Okay, so we want to minimize the values A1, B1, right? So, which means we want to minimize So, we can also consider it as this ways. So, we have NA elements and NB elements.
We can instead of considering them as different arrays, we can do A1, B1, A2, B2, A3, B3 and so on, ANBN. So, this is an array of 2N elements.
Now, if I choose any index I like from A like 0 2 4 any even index.
So, which means I'm choosing A3B3 and A4B4. So, I'm choosing four consecutive elements from this array of this array of 2N elements and then sorting them.
And just putting in back the two different elements.
And that is the answer. So, in So, I can tell what I'm doing is See.
So, there's some observations that I'm making on this.
Observations, right?
Don't consider both arrays separately.
See, >> [clears throat] >> what if let's suppose is an array from A1 and then you have A2, then you have A3 and so on till AN.
Simple Similarly, you have B1, B2, B3 and so on till BN.
Right? In one operation, you choose AI.
Like I will make an array of length 2N elements. Why I'm doing so? It will look like this, A1, B1, A2, B2, A3, B3. And I'll tell you how will this make my problem very simple.
And the last two elements will be AN, BN. So, how Why this makes my problem very simple is if I If I choose any two elements like I was choosing here, AI, I was choosing AI, A I + 1, I was choosing BI, B I + 1, right? So, in this way, I will be choosing just four elements from this, which means like this only, like you don't need to choose from two separate arrays. You will be choosing. So, the problem is boiled down to in this array, in this new array, I need to take substring I need to take substrings substrings of length four, then sort them and reduce them to length two and add back to the array.
And this has made my problem very simple.
Hmm. So, it's just like you have four elements to remove. Now, what would be the logic? If I go about Hmm. And what I need to do is I need to minimize this. So, up as this operation you have minus two elements to remove in every operation and so on.
And 2n minus two elements 2n So, at the end there will be four elements remaining.
Then last four elements will be this operation will be performed. Then this four two elements two elements will be remaining.
And these two are the minimum elements that has to be maximized.
All right. So, which means I need to in this array new array, I just need to it's simple. You just need to remove the for each substring you need to remove the minimum element and the maximum element. You will left you will be left with the two between elements.
And you need to do this operation as many times you can.
No, not as many times. Uh n minus one times you will be able to do.
And in the last operation you will be getting four elements and you will be resolving them to two last elements.
Okay. Now, let's make some observation more observations on this.
Hmm.
Wait.
Some Oh, they are able to make this now.
So, this is not that hard.
Hmm.
>> [clears throat] >> Okay. And what So, okay. And if I choose any two any four elements So, it's like you are moving the outer elements to move them out.
Then later elements are in.
I'm going to remove them again.
So, two will be in between. Min max will be out again.
>> So, if I perform these operations optimally n minus one times.
So, I will remove n minus one maximums.
All n minus one maximums not.
I will not be removing the maximum.
I'll remove local maximums.
Local maxima and local minima.
So, it's like you have both the elements and in one operation in all these four elements you are removing the local maxima and minima.
Okay. So, this is the problem.
Okay, so now I need to minimize the maximums, right?
I need to maximize the minimums, sorry.
So, I just need to remove all minimums and minimum elements that I can get as fast as I can.
So, let's suppose there are minimum elements, no?
So, also we will process in batches. So, like first batch will go zero to third index, both inclusive.
Zero comma three.
Then next batch will be of four comma seven.
Four comma seven inclusive.
The next batch will be of 8, 11.
Or there can be intermediate batches as well of like 2, 5 2 3 4 5 and let's say so on.
So, let's [clears throat] suppose I choose 2, 5.
I remove some two elements, then what will happen is now I will include the first element.
I remove these two elements.
So, it's like this 0 1 2 3 4 5 Yeah, I don't know.
And there was six.
And there are seven. There were eight elements. So, I took 2 3 4 5.
Then I got these two removed.
Now, I can take this element. I can now again have four two one choice two choice three choices.
Let's suppose these are the only elements. I need to maximize this. So, what I will do is you may now minimum to maximize it. So, I will just remove this first.
All right.
Can't I just do binary search?
Okay. And what is the order of integer elements of n?
A of I B of I are up to 2n.
Can't I do binary search? Okay.
See, if I can make mid to be the maximum value of the minimums, then I can make all values. No, no, I can't make all values because I'm already moving the minimum also in each step. I'm not just taking out the maximums.
I'm removing minimums as well.
So, for sure I will remove n minus one minimums.
This is for sure. I will remove n minus one minimums. Okay, I just Let's not take the positive and negative consideration like the maximum numbers.
In each operation, you're removing the minimum element. So, you will end up removing n minus one minimums like this only.
So And can these n minus one minimums will depend on indexes.
Let's assume the first minimum is with A1 B1 A2 if there are four indexes.
This is the first minimum.
This lies somewhere anywhere like somewhere in this.
It will always be removed.
>> [clears throat] >> So, you will remove n minus one minimums. So, these values can't be answers. So, like if it's 1 2, then 1 is the answer because there's no operation you can make. If it's 2 4 5 1 3 6, are you all right?
2 4 5 1 3 6 >> Mhm.
>> [snorts] >> Mhm. 24513682 It can be 21 4356. This is the array.
Now, I can remove one.
I can I will make two operations. I can remove one. I can remove two.
When I remove one, I will remove three. I will remove four as well.
And I will be removing five as well.
So, three and six will be remaining.
So, second answer will be three.
Third may 74564788 I'll remove I'll do three times, right?
Three operations. So, I will remove this four. I will remove this four. I will remove this five.
So, when I remove the first element I have two options.
Here, in this case.
Whereas I have multiple options, I have three options.
I will have multiple options, too.
Because there can be four indices where this minimum element can lie.
Mhm.
I will end up removing seven four.
Then, I'll take this one. I'll end up removing four eight.
Then I'll take seven four is done.
Four eight is gone.
I'll take this one and I'll remove five and eight. Six and seven will remain, so six will be the answer.
So, it's like last n minus one elements can't be there.
But, the just bigger limit than that will be there.
Mhm.
This people will be able to make it right. This is not that hard, I suppose.
I need to solve this.
So, it's like mark all the Oh, we minimums. Because n minus one minimums because you will be removing those.
Or else, what you can do is you Because we know only two elements will remain.
Let's forget about the two elements.
Just just see what minimum element will remain.
Let's suppose this is the array. Two two two two four.
What three elements will be remaining removing? One this, two this.
>> [clears throat] [clears throat] [clears throat] >> key segmentary variation Is it there segmentary variation on this?
Or is it a DP variation? What is N?
N is 10 to the power 5.
Is it some sort of DP variation? Okay, let me see.
DP Mhm.
Let's think from DP angle, no?
All right.
So, if I end up removing for I-th position, so I will remove I, I + 1, I + 2, I + 3. These four elements I'll remove.
Then DP of I will go directly to DP of I - 4 {comma} DP of I + 4 min of this.
No.
No.
Because you have removed two elements.
I - 2 I + 2 And so the DP angle is ignored. No, there's no DP angle in this.
Because DP of I depends on I - 1 as well.
And DP of I depends on I + 2 as well.
So, it's like it's dependent on both sides.
So, there is no base case that can be formed. So, like DP of I is dependent on both the forward and the negative uh on the backward states.
Or So, I will have to make some observations only, so N minus one minimums will be removed.
That is one observation.
Will be removed.
Mhm.
Okay, now what?
Can I do something like this? Can I mark this element?
Mark some element.
And I say >> [clears throat] >> Can't this be removed?
Can't this not be removed?
But even if I mark an element and I say this cannot be removed what about the second element, next element?
Can't you also say something similar? We are taking four elements and we are making a very good choice. See.
We are making a good choice here.
Mhm.
Certainly this observation is good that I made, but there should be something more.
There has to be some or more observations on this.
That method try to get minus one minimum to remove.
>> [snorts] >> It come guru.
2016 area.
Sorry, I mean it is a minimum smart guru.
Mhm.
>> They also have n minus one minimums marked up there.
>> [cough] >> Right, I will just mark all the n minus one minimums.
And then I will remove n minus two elements, no?
So, you first mark them or the last mark them.
And the each way I will check number of elements.
>> [clears throat] >> n minus two is already in the team.
Yeah, n minus two is already in the team.
See.
See, there are n minus one element.
Here, minimums.
So, let's say let's suppose the length is n minus two n minus two is better than putting up with it.
Either two n will be either two n minus one will be, either two n minus two will be.
I'm going to do or yeah.
Two n minus two n.
So, what I can do is is key under two sum of the gap.
Is going to be good to go.
Yes, I'm going to do this one too.
You don't have to do this one too. You don't have to do this one too. You don't have to do this one too.
Is key under >> Ooh, that is this is one more observation that I made. Okay, I'll go make way L or our range to remove color.
Getting me operations for cellular range calendar to make a pass for me show range car men or maximum budget.
What I think a lot of range calendar of a massage operations with a lot of it.
So, it's range car minimum or it's range car maximum element to me show budget of my pass.
So, I can do one thing.
See.
My pass the L or R equal range here.
It's car minimum minimum to me. But, how many have had to go?
Yeah, I had to go.
Yeah, I had to go. Yeah, I had to go.
Yeah, I had to go. So, L or million had to go with me each element to budget.
L and budget to occur.
I could [clears throat] be range to offside to end plus two size key.
I could be two and size key.
I could be two elements with income.
So, I take this two range.
So, what is this is up to what are the segments to break up even sizes?
>> [clears throat] >> So, it's like you have your pass going even segment what are the segments here?
Let's suppose I may put it to end size calendar cool as I make it to end one comma R one.
And to L2 comma R2, I will break them into two segments, only two. Of even sizes.
I got I got this is how you have.
I got let's suppose even in here. Let's suppose six inches.
Tell us what you do 80 minute and a half.
Okay. If I'm able to break into these two segments, to make it a little bit.
Don't know if you can reach the kitchen.
So.
Minimum.
I'm going to go with those segments and pick them up.
Of even length. Okay, go to the beach and watch again.
>> [clears throat] >> Nico.
I mean about those segments and I have.
Agla, oh, that is the solution. Yeah, I got my N.
I got two elements that I can get out of this one.
Two elements in there, okay.
Two elements in there, okay.
I said I got two elements in there, okay. Multiple segments got five minutes.
So.
In the element remove column.
Fair in the compare column. In the remove column.
>> [clears throat] >> So.
It's like he eat.
Operation consider them as subsequences.
Right.
Consider them as subsequences. Yeah, this subsequence is there present in this. So, if there is a very big subsequence that I'm you don't even speak about anything else.
Right?
Mhm.
So, it's very too simple now, okay?
Go through.
Will I always take the uh uh middle two elements?
Will it always work?
Will I always take the middle two elements?
And will it always work?
>> It's been a lot I can do.
By 1,500 people made this, either they are using Gemini or I am low-skilled, right?
The people in Rubik are not able to solve this fast, but Mhm.
I need to make the pretty fast.
Okay, so if I'm taking all the minimum ones, all the middle ones, then I'm taking the min- median ones, no?
Mhm.
Mhm.
What are the observations we can we make?
Is there any segment variation that we can use to solve it using segment trees?
Okay, for these two elements Oop.
Wait.
Let's create a segment tree.
Segment tree make out of three.
This is the node.
This will contain two elements.
A B I get two notes ready. Left child and right child.
Two left child over.
Okay. What do we call it the right side?
Right?
The left child over will previous two elements take up left side say.
Like A1 {comma} B1.
Or yeah two elements take up this side say A2 {comma} B2.
Mhm.
Or root function give up segment three got it will be You will get four elements. You can just sort them.
And then return.
A one A2 Like the first, second, and third element.
>> [clears throat] >> So which means And there will be two cases.
Yeah, I will share the minute I know.
The even is coming out and it's coming out. That's it.
This is my two three written here. This is four five written here.
>> [clears throat] [cough] >> But second case is key So, is me dekho.
Then my approach is wrong, but if my answer same to my approach is correct.
So, is me yeah or yeah element are there.
So, yeah are there.
Yeah to her jar there.
I think this is a segmentary variation.
Now I'm confirmed with this.
But I'm just thinking how to apply segmentary on this.
So, mera segmentary logic is good. Okay.
Two elements, two elements. two elements two elements into two two two two two elements.
Take max up then Add them to four.
two two two three elements here now or two key direct answer direct use code in the group or square root of two ways Take care.
I have two two ways here. In the two different answers I get or So for n is equal to three men two ways here.
>> [clears throat] >> n is equal to four It's a way here.
It's a final answer I got.
It's maybe two ways here.
It's maybe three ways here.
So n is equal to three men two ways here.
Take care take care for me.
Take give away It's maybe two ways here.
Okay, so this I think is the question.
Okay.
I need to make Oh, load and balancing.
Okay, I think we are good here.
E I don't know what I'm supposed to do with this.
Hmm, I need to solve that, but what I'm thinking is how do we solve this?
Yeah, this has to be something of this word.
>> [cough] [clears throat] >> X second.
I'm going to make first up >> [snorts] >> last random.
Or should I just say this thing key four cases are there.
First element, then you make a array of these elements or first two elements and then you make subarray of all the elements.
Or some array of these elements, last two elements, and or array of all the elements, which means okay.
Can we do something like this?
This can be put first and last element to ignore for something.
But such that I have another child element.
Then we do not call the number.
It's going to be a median.
Okay, let me check again.
N B N A N N A It will have to be a whole setup.
I was going to make a lot of other yard earlier, but I was looking at the setup over thinking is the gap.
Required as question two.
Why I'm confident in this approach is key C but that there's no another case where I can exclude two elements.
See, child elements may be after the emergency. In this map, two elements may choice to So, Let's suppose the element between me and you over I can take two elements.
Let's suppose two elements between me and you over I like except that I had a loser.
except that I had a a element there.
a element there. That was a element and so on.
together I don't know what kind of is me and you over.
I don't know what kind of is me and you over.
I don't know what kind of is me and you over. I don't know what kind of is me and you over. I don't know what kind of is me and you over. don't know what kind of is me up I don't know what kind of is me and you over.
Which means I will have to use this for this sub area.
Or I will have to use this.
But, I can only make choices if the elements are corner elements.
I don't know what kind of is me and you over. back B of I.
See that? Uh I will make case one.
Vector in C1.
Vector in C2.
Case four.
Case four with the beginning.
All n equal to zero. C4 is equal to C3.
is equal to C.
C3 is equal to C.
>> [sighs] >> C1 may I'm going to uh I'm saying minus one is already there. C2 may I'm going to I'm going to do that.
Two C and side on the C3 I'm going to C cable over there, but C3 pop back two more times.
>> C 2 C 1 C 2 up ASC C 1 C C 2 C 4 Case 1 now up case 1 here here C of 0 Given not PB C of N - 1 Okay, let's go.
If N is less than equal to 2 Hm.
>> No. I'll guess one case two, right? Even is this.
two You will not push back.
You will not push back.
C1 of eight You will not push back C1 of mid minus one Right?
ASC A1 Val is equal to min max of val comma A1 of one Clear.
Yep, vector in data in a little bit of vector int A2 A2 make out A2 EB C of zero A2.PBC of one A2.PB C2 of mid A2.PB C2 of mid minus one Mhm.
A S C A2 URL is equal to max of 12 comma A2 of one Right.
And similarly, I have to do A3 as well.
A3 last two element remove in a Mhm.
A3 may N minus one karna hai.
N minus two karna hai.
C3 of mid karna hai.
C3 of mid minus one karna hai.
C3 karna A3 karna hai. Idhar bhi A3 karna hai.
Idhar bhi four karna hai.
A4 Ye ho gaya.
Or C4 C4 C out val and del Pata nahi chalega ki nahi. Mereko lagta to hai chal jana chahiye.
Isiliye mereko lagta hai wrong bhi hai ye.
This is some segment problem so I'm not able to understand that the post.
Segment three I need to ask the machine segment three like I mean that's the most probably.
Mhm.
Mhm. What can be the answer for this?
I'm just replacing it with the medians.
>> [clears throat] >> Mhm.
Right, I'm replacing it with medians.
Always.
I think it's D9. It's not that easy.
I think I'll have to solve it.
Are you up?
Top standings we got to take the one one.
>> Bro, how come the order is just so done?
So, it's like you sequentially process them. Not like you sequentially process them.
There is a sequence in which I need to process these elements.
>> Mhm.
>> [clears throat] >> I think I'll stop this. I'll go go for dinner then.
>> [snorts] >> Bye-bye. This is still here only.
Videos Relacionados
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











