This video provides solutions to Codeforces Round 1095 problems A-E, demonstrating key algorithmic techniques including greedy strategies for array manipulation (Problem A), mathematical properties of GCD and array operations (Problem B), binary search on answer with greedy verification (Problem C), parity-based array sorting (Problem D), and segment tree optimization for prefix queries (Problem E).
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Codeforces Round 1095 Solution | ABCDEAdded:
I'm live now and confirm So overall in the contest I was able to solve a b c and e was a bit tricky. The editorial is out. So I went ahead and editorial of the how to solve. I think able to e as for a bit more discuss I have enjoyed it video would be available as a v on my channel also consider So this area let's wait for a couple of minutes for more folks to like people in the chat like how many guys problems were you guys able to solve that will give fair set of idea of where most people are where we can move a bit faster.
Hey, hi.
Let's get started now.
So the first problem says you are given an array a a1 up to a n. You wish to make array empty by performing the following operations as many number of times. Select any sequence of indexes.
The element should be in increasing order.
remove the elements from the array and the cost of this operation is equal to the product of all the numbers you have chosen the minimum total cost required to remove all the elements from the array A note that total cost is equal to sum of cost for all the operation actually it also says that as the answer can be very large reported modulus 76 some given number so this is where we are let's first try to analy Let's try to explore some of the elements. What are the causes?
or actually like samples can actually be a good hint as well.
So like we have let's say we have few numbers like let's analyze what is the cost if you choose all of the elements right now cost would be 2 into 3 into 1 into 4 which is around 24 like one initive idea is like what's the other way to look at it? Can we just keep all the elements separately and try to count cost for them?
Like let's say if you choose this in the first operation, this in the second operation.
For example, let's just choose only two in the first operation, only three in the next operation, only one in the operation after it, and only four in the operation after.
Then our cost would be like it's around 10.
So this gives us an intuitive idea like maybe it's always better to choose as much small segments as possible and overall in general actually it will hold true if for any two numbers x and y if both of them are greater than equal to 1 greater than one x is greater than 1 and y is greater than 1 then x * y would be more than = x + y.
The idea over here is like let's say m is the maximum of both of these elements.
Then x + y would be less than equal to m + m.
and x into y would be greater than equal to 2 into x like let's say or let's keep it a bit simple we have two numbers x and y then let's try to see what x + y is and which is also greater than equal to 2 If there are two numbers both of them greater than equal to 2 then x + y would be greater than equal to y + 1 and this should be less than equal to 2 into y and because we have this equation 2 * x this will also be equal to x * y.
Any questions over here? Like if you have two numbers both of them greater than equal to two then sum of that would be less than equal to product of those numbers and like once you have this condition the solution becomes as follows.
Try to group one with other numbers because like if you have 1 and x some number x then if you take them individually their cost would be 1 + x.
But if you take both of them together 1 * x is equal to x. Essentially you kind of got one free over here.
So like in an ideal world our answer would be let's say we have two three 1 4 5 1 here and something. In an ideal world we can kind of assume that we can combine all the ones with one of the numbers and all we have to do is we have to just print the numbers that are not equal to 1. 2 + 3 + 4 + 5 is equal to around 14. Like the idea is like you can keep pairing all the ones with the number next number over here.
Like for example, if your array looks like 1 1 2 1 1 3 4 11 1 5 1 6 something like that. Then what you can do is you can start from the first element two three things like this. Okay.
Is my screen not visible?
Okay.
Sorry. Did not realize that screen is not visible.
Thanks Chris.
So I think let's go over the problem once again.
So the main idea over here was we were trying to figure out like when like like because if you have to multiply some number the product will always keep on increasing.
So the idea was like try to keep segments as small as possible and then we were trying to analyze when the product of two numbers would be more than their sum and when sum of the numbers would be more than their product. And our main claim was if there are two numbers both of them greater than one then their product would be more than their sum.
The reasoning here was let's assume that x is less than y less than equal to y and since both of them are greater than equal to 2 is less than equal to x is less than equal to y. Now if you have x + y then we can make it like that x + y would definitely be more than y + y because y is the bigger number which makes it equal to 2 y.
And now because x is greater than equal to 2, if you put a larger number or here, then you will again get x multiplied by y. So overall sum of two numbers is less than product of two numbers. If both of them numbers are greater than two and if any of the number is one then it's better you take their product and combine them into one segment rather than taking them individually because taking them individually the sum would be 1 + x but if you take them together you kind of got one for free.
Now with this the idea we have came up was like let's say this is how our array looks like. Then what we can try to do is we can start our segment and we can choose it unless we get one element which is not equal to zero. So we can keep this as our first segment.
We can keep this as our second segment.
Now we start taking one. We have one. So let's go to the next element. Let's keep all of the segments with one. Let's also take this one. Let's also take this one.
Now we have gotten two. So let's just take this segment.
So the cost here would again be two.
Here we have one. So let's take next segment as next element as well. One.
Let's take next element as three. Let's take this.
Now we have four. Let's just keep it. We have again one. Let's take the next segment as well. One. Let's take the next element as well. And this is what we will end up with.
Now we can have again one. Then let's take next element as we have one again.
Let's take next element as if we choose segments like this. Our cost should essentially be equal to all the number sum of numbers which are not equal to one. 3 2 3 4 5 6 and that is the overall idea. Like the only catch is like if your last element is also one then you cannot pair this one with any number after it. So in this case you will have some segment where the cost would be one. For example the numbers may end like this one one one one. In that case you have to we'll have to take this segment as well because like notice that we will have to choose the numbers in an increasing order.
So if you choose any number more than one before this then you won't be able to choose it. So that's the overall idea.
Sum of elements greater than one and add one if last element is one.
Any questions anyone?
So like the overall idea comes from the fact that if you have two numbers both of them greater than two and product of those numbers should be always be more than sum of those two numbers.
on all the numbers and if the number is greater than equal to two insert plus and if the last element is one then you do answer plus+ the overall idea for the problem.
Any questions anyone?
It looks like system testing is going on.
We cannot submit it.
Okay, let's wait for a minute or two then we will move on to problems.
So let's move to the next problem.
An array is called good if the difference between maximum and minimum in the array is equal to the greatest common divisor of all the numbers in the note that an empty array is considered to be good. More formly an array is good if maximum minus minimum is equal to the gcd of the element.
You are given a permutation p of length n.
Determine the number of good arrays in the given permutation.
So what we need over here is we are given an array.
You have to count number of goods sub arrays.
For example, it can and so on.
And we can have a few more arrays here like these are some of the examples.
And definition of good is maximum minus minimum is equal to the GCD of the sub array.
Like in problems like this one problem that often helps is you try to solve for first GCD equal to 1 and then try to generalize it.
If let's say we have GCD equal to 1 then how does our problem looks like?
we would have x - mean = 1 which essentially translates to x = 1 + m.
So if the GCD of all the numbers is one then like you can have only two elements in the array some X and some X + 1.
If GCD is one because the moment you take X + 2 then your maximum minus minimum would be more than one.
Does that makes any sense?
So like the only possible way when we can have GCD equal to one is like when we take two consecutive numbers like the element set should be two consecutive numbers. That's what I'm saying. Now if we have to take let's say GCD equal to 10 then the one all the numbers would be multiples of 10 right so we can write it as 10 * I 10 * J 10 * K and so on like that's how the numbers would look like there there would be multiples of 10.
Let's say this is our maximum and this is our minimum.
So then the order of elements we have would be 10 * k less than equal to 10 * j less than equal to 10 * i.
Again this gives us k less than j less than i.
And if I have to look at the equation, this was my maximum.
This was my minimum.
And this was my GCD.
If I divide everything by 10, it again becomes I minus K = 1.
So like again it becomes like one of the element would be X I equ= to K + 1.
So this gives us the set again like the difference between this and this should be 10 in this case or something like that like difference between K and I should be one. So that when you have JCD the difference becomes 10 or if I have to look at it on a broader picture what I can make a bold claim is I can only have two distinct elements if this equation needs to be satisfied.
If the array can be good, I'm not saying every pair of two distinct elements will give you a good array. But what I am saying is if you have more than if you have three different elements in your array then your array cannot be good.
And the claim is like this like if you have the GCD of array G then one of your number would be G * I another number would be G * A and another number would be G * K.
And whatever maximum minus minimum you have. If this is your maximum and if this is our minimum then g * k - * i = g maximum minus and which gives us k - i = 1.
Any questions so far?
So like now if this is our claim then one thing you can notice is that in the input array we are given the permutation.
Permutation means all the elements are different.
So like we are given a permutation and this implies every element is different because permutation means all the numbers from 1 to n appears at least once.
So what we can do is we can check every pair of consecutive element and call it a day. We can check A1, A2. We can check A2, A3. We can check A3, A4. And what are good pairs we find? We will do it.
Okay. And oh wow, this is proof is good way. I thought of this question is maximum minus minimum is a non-dereasing function. Why?
over and error keeps gd keeps on decreasing and maximum minus minimum keeps on decreasing. So yeah like in the contest I did not really try to prove anything. I just ran two for loop and validated it. This proof is actually from the editorial not something that I invented.
So this is where we are in context. I was like the answer must be something small as long as you find good pair you keep it otherwise you want.
So like overall our problem becomes like this. We need to check all the pairs all the consecutive pairs.
If maximum of pairs minus minimum of pairs is equal to GCD of pairs then we do answer plus and essentially that this ends up being the solution for the second.
Any questions?
So like so in context I knew like this is problem B. So you need to just check couple of answers left and right and call it a day. I would have tried proving it like if I have to prove it.
This is how I would have proved it. I would have assumed that the GCD of the numbers is G. Then maximum is some multiple of G and minimum is some multiple of G and then I would have tried to played around it. So actually this is what I would have tried doing it if I was trying to prove it in the box for me like it was just be so one WA was worth it. So I just end up implementing something because in the problems like this if you have an array of elements with GCD then it makes sense to convert the array into G into some multiple and then try to work on that remaining part.
So like essentially whenever you have something with respect to GCD of an array let's just try to convert the GCD equal to one by dividing all the elements by GCD and see what nice properties you can get there.
So this was problem B.
Any questions anyone before jump on to problem Yeah. So let's move to problem C and mountain easy version. So this is an easy version and E is the hard version of it.
And most of my contacts did when trying to solve E.
It says this is an easy version. The difference is we have to only find the value of f of a. Now we have defined some f of a.
We are given an we are given some array c1 up to cm. We have to choose an array of inteious b1 into bm and take mod of each element with the corresponding other element and then we have to find the maximum possible max of the resulting array.
So to put it this way we are given some array A1 A2 A3 A4 and so on.
Now we have to take mod of each element with whatever numbers you choose.
We are free to choose whatever number we want. Take mod.
You will get some new array.
Let's call it D1, D2, D3, D4. And we have to find the max of this.
And this max should be maximum possible.
We can make maximum we can make the flexibility we have is we decide what number to take mod with and what we have to ensure is that max is maximum possible and max is also defined as max of an array A is defined as smallest number not present smallest positive number present in the any questions anyone on the problem statement.
So now the idea is as follows.
Let's say if we want to make max= to 5 then we need to insert like in our new array all the elements from 0 to 4 are present.
Similarly, if we want to make x greater than equal to 10, some number more than that, then we must have all the numbers from 0 to 9 present in this array.
like max equal to 10 means 0 to 9 are present 10 is missing. What I am saying is if we can somehow insert that all the numbers from 0 to 9 are present then our answer is at least 10. Any questions so far?
So with this idea what I can claim is my answer is would be kind of or if I ask you this question can we make all the can we insert Or is it possible to have all the numbers from 0 to 10 at least once?
If you say the answer is yes, then I can also say that the question answer to this question.
Is it possible to have 0 to 9 at least once?
If I say that you can definitely have all the numbers from 0 to 10 by doing some by choosing the given operation in some way then what would be the answer to this question? Is it possible to have numbers 0 to 9 at least once after doing the operation?
This would also be yes, right?
this will also be yes.
Similarly, I if I ask you this question, is it possible to have all the numbers from 0 to 20 at least once?
And if you say the answer to this is no, then what would be the answer to this question.
Is it possible to have all the numbers from 0 to 21 at least once?
What would be the answer to this question?
like this would be no because if can you cannot have 0 to 20 all the numbers together then for 0 to 21 also you have to make 0 to 20 together and some additional elements. So if you cannot have some small set of numbers together then if you add more numbers to it the answer will remain wrong.
Similarly here if we say that we can make all the numbers from 0 to 10 together in the array then if you remove for example 10 also then the same would hold true for 0 to 9 as well.
Now whenever you have problem like this for example if you have some function which says for some question for x if it is true yes for x it is yes this means for x - one also is yes like this problem has this property if you know the answer is yes for some larger number then you know that any number smaller than that will also give you yes and for some number y if the answer is no then if you choose some number more than that also the answer is no then like the problem here would look like this you will have some yes and then you will start having no like your array from The numbers from zero to all the numbers would look like this.
All like is it fair to say like this that you will have some number X some number up to which the answer to this question will always be yes and after that you will start receiving no.
And now because 0 to 5 is the largest element where the answer is yes then this is the best we can choose and the max here would be six.
Any questions anyone?
Now how do we find answer to that question? We are yet to go over there.
So now if we can sum off and one more idea over here is like now we can always bind the search for this array for point where yes changes to So now if we can somehow solve our original problem which says can you somehow make all the elements from zero to 10 in the array and if we can somehow return that yes and no then we can binary search on the complete array to figure out our required answer.
Yes. Now we will binary search on what is the max we can get. Essentially we are trying to binary search this point where the answer for this question changes from yes to no.
And that point will also give us what is the maximum number from zero up to what you can make. And the max is usually plus one of that.
Because if you have an array where you say you can make all the elements from 0 to 5 then your max would be more than six at least. And because you cannot make 0 to six together so you will end up in a situation where your answer cannot be Like for example, if we can make all the elements from 0 to 5 together, but if we cannot make 0 to six together, then this is the best answer we should make.
And in this case, the max of whatever more elements is there, that would be your max because you will have all the numbers from 0 to 5. Six would not be there. And then you will have numbers more than six. And our definition of max is the smallest positive number that is not present in the array. We have every number from 0 to 5. Six is not there.
And we care less about the elements more than six now because we have found six.
So now probably first I will try to jump on board for this.
Now this is my checker function. I'm not going into the details yet but what it says is can I make all the numbers from 0 to m minus one in this array and it will return me a true and false.
So I start with zero like left end point can be zero and your max is more you if you n numbers then you cannot have zero to n plus one all the numbers together. I look at mid. If the mid is true, if I can make all the numbers from 0 to m minus one in the array, then I change my left hand point to mid. Otherwise, I change my right right hand point to mid and whatever maximum number I could find. Now here the constraint would be for L the answer would be true. For R the answer would be false. So I have to print L here. So like you see how like the original problem of max divisor and all we have changed into one simple question.
If I give you a number 10, can you give me a yes and no answer? If you can make all the numbers from 0 to 9 in your answer.
Now let's try to observe how we try to answer this question.
Like let's say we have 10.
If we take mod x, what are the possible values we can get?
10 mod 10 is 0 or let me put it this way.
10 mod 1 is 0.
10 mod 2 is 2 is again zero. 10 mod 3 is 1.
10 4 is 2.
10 mod 5 is 0.
10 mod 6 is 10 mod 7 is 3.
10 mod 8 is 2.
10 mod 9 is 1. 10 mod 10 is 0.
10 mod 11 is 10 mod 12 is 10 so on and if I look at all of these values the possible values I could get was 0 1 2 3 4 and 10 these are the only possible values I can get by taking more because everything else would become start becoming similarly if I try to inspect more for example if I try to look at 19.
What you will notice is you can get all the numbers from 0 up to 9 and you can get 19.
These are the only values you would be able to get by taking some number as you want.
This you can try to write down numbers and you will see if I have to prove it then I can prove it like this.
If I have to take 19 mod m, if m is less than equal to 9 and so a mod b is always less than equal to b is always less than b assuming b is not zero because the remainder when you divide something if you divide a number by five then the remainder is always between 0 and four.
So this number is less than 9 less than equal to less than 9.
If my m is less than equal to and if my m is less than equal to 10 and less than 19 then I can notice is 19 mod m 19 would look something like this.
m + r because when you divide that number by 19 the quotient would be one only.
So this gives me my r would be 19 - 1.
And because m is between 10 and 19, this value if my m is between 10 and 10 10 and less than 19.
10 - 19.
less thanus. If I add 19 here, this greater than m greater than and if my m is 19 then 19 mod.
This is problem C. So what I'm trying to prove is if I take 19 mod m this value is always less than equal to 9. I cannot have it more than the proof is if this value is less than 10 then anything mod it would always be less than 9 and if this value is greater than 10 then my m would be like this. The remainder would be one and the question would be one and there would be a remainder and the equation would like this like usually it would be like this and for this case Q would be one.
So what I have end up proving is or in general if I try to do it for odd numbers odd numbers the value is n / 2 always less than equal to n / for 19 it was 9. Similarly, you can try to prove for even numbers as well and the value would be n / 2.
Like for example, if you take 20 like for 0 up to 9, the remainder would be less than= to for 1 up to 10 the remainder would be less than equal to 9.
And from 11 up to 20 again like this would here the term would be 20 - 11 20 - 12 and this would also be less than I proved it for odd you can try to similarly prove it for even.
So for any number we have two choices.
If you have a number x, we can either keep it x or I can map it to the number x / 2, x / anything between x and x / 0 to x / you need to take care of plus - 1 here based on order.
So this is the overall idea. So for every number what you know is you can either keep it as it is by choosing a larger number for division or if you have to divide it then you can you will always have number which is less than the half of the original.
Now if I have to answer question like can I make all the numbers from zero up to 10.
What I should do is all the numbers which are between 0 to 10.
So for example if my array looks like this 2 19 4 25 let's say this is the array and the question is can I have all the numbers from 0 to 10 in that case I should keep this two and four as it is and all the remaining elements I should try to make them closer to as possible so I can try to make this 19 equal to zero.
is 25 = 1, 30 = 2 and so on.
Or alternatively, if I have to put it in a different That's If this is our array and the question is can I make all the numbers from 0 up to three.
I should keep one copy of two as it is.
This is free. I am free to make anything. I should keep one copy of three as it is.
And in the remaining array I should try to pick the smallest element and I should try to make it equal to zero which is possible. You can take two mod 2.
Then I should look at the second smallest element. I should try to make it equal to one.
And you cannot make okay you can actually take 3 mod 2 and make it one.
So 0 1 2 3 is possible. Now I should try to look at if I can make four over here or four I would possibly able to make but that gives me the idea.
No like I simply choose like now I am binary searching on the answer. So my question is can I make element from 0 to three if the answer is yes then I will now binary search with my L as three and move further. So the answer for 0 to 3 is yes. So now I will try to binary search a bit more like right now I'm choosing like if I have a question can I make all the numbers from 0 to x how I'm going to find an yes or no to it once I have an algorithm for it I will go ahead and use my binary search for the answer now if someone asked me can I make all the numbers from 0 to five with this given array what I will do is I will keep one copy of one copy of three. Then I will choose the smallest element like that zero is not yet here. So I will try to make it zero.
Then I will try to choose second smallest copy.
One is not here and I can actually make one here. Then I will try to choose third smallest number. 0 1 2 3 is there.
Can I make four using six? The answer is no.
Can I make four using this?
Again the answer would be no. Can I make four using this? The answer is again no.
So for 0 to 5 the answer overall answer would become no.
So this is what I try to do.
If I to check if I can make all the numbers from zero up to m, I keep one one copy of all the numbers that are already there. And for remaining element I try to match it with one larger element.
I checked on all the numbers here. I'm checking If my current element is less than M and if I have not yet taken then I will take it otherwise I keep in Now I start for all the J from 0 to M.
If I have already taken one element for this, I skip it.
Otherwise I try to find one element which is my given answer.
What we know is for odd numbers the answer is simply divid by two. For even numbers the answer is vidide by two.
As long as I cannot make I from the current element, I will skip it.
Now if I have reached the end of the B, then I cannot definitely make J.
So I keep it and I skip further. And if I was able to make all the numbers from zero up to m minus one then I will return.
So this was the overall idea for this problem.
Any questions ideas? Any the crucial idea was okay problem totally destroyed my model.
I try to just it x through 0 to n since n is the greatest max you can get and try to make any element in the array equal to x by doing lower bound or x itself. If not possible then I print X as an answer. Could not think of one.
Actually for this there is a very good article on top.
this I would strongly recommend you to go over there like these were the article I actually read probably 5 years ago or so and that's where the intention of binary search came for me. I very strongly recommend you guys to go ahead and read this out.
a binary guide. I don't know if I remember it or not.
I very strongly recommend everyone to go ahead and read this article.
Like this will completely change how you think about binary search and it would not be possible for you to forget this binary search idea. Like if you have some number x then x - 1 is also true. You have y then y - 1 is also known then you can binary search on that.
You will simply never be able to forget this if you just go ahead and read this article and this is actually the article I read probably like you can try to solve it but the problem here is like top coder is no longer there so I don't I really doubt you would be able to submit these problems and try to probably search for code forces problem or USAS SEO guide for binary search like you cannot submit problems on top is m assumed is m assumed based on nx2 logic like I'm not sure if I understood it so like I the I took the left most range for the binary search as minus not zero like if you read article you will get a sense of why I took minus one not zero one or something and my right end point is n plus one because max of n numbers cannot be more than n so I just choose one number more than what max we can get and minus one because max is at least zero so I simply choose minus one so for l it's guaranteed to be positive for r it's guaranteed to be no and then a binary search based on yes I either change my lower bound or I change my upper and this is simply the mid for the bin.
So this was the idea for the problem.
And like you just read this probably it won't take more than 5 10 minutes but the moment you do it will this idea you will always get in the problem.
So this was our problem C. Any questions anyone on problem C we will move to problem D and problem is harder version of this problem. So we will have to do a things bit differently there without a binary Okay. Like whether what is the feedback for Twitch? Should I stream both on Twitch or YouTube or mostly YouTube?
Any feedback? I do see a couple of folks who have created an account just before this stream. So, thanks a lot for that.
Yeah, like I was mostly experimenting with different platforms and that's why right now I'm mostly streaming on Twitch.
So in the context after doing problem C I directly went to problem E because that was the harder version. Here the difference is now we have to find this maximum for every possible prefix of the answer and like this needs to be done a bit differently but let's go ahead and body I mostly read the editorial because I did not think about it in the content and editorial was already so it says you are given an array a cons string of n positive integers For any segment of the array starting at position L and ending at position R that small M L R denotes the minimum value in the segment and large M R denotes maximum value in the segment. We can perform following operation any number of times. If the sum of minimum and maximum is odd.
This means they have different parity.
One of them is odd, one of them is even then we can reverse that segment.
Now we have to determine if we can make the original area non-dereasing by performing a some set of operation.
So this is where we are.
So our problem statement is like this.
You have an array.
and so on.
If we have a sub array where minimum plus maximum all then we can reverse it. So this becomes a6 becomes a5 this becomes a4 this becomes minimum means some odd means this is either even plus odd or this is odd plus.
Now can we sort the complete array?
That's the question to me.
So this is where we are and we have to print an yes or no and an array is here.
So for few observations if we have two numbers we have this array then we have an odd number then we have an even number and then we have few more elements then we can choose these two elements and apply a reverse operation on it it will become even this will become odd so is it so what I'm saying is I then two nearby odd and even number if the nearby combination is odd and now editorial boldly claims if you look at all the odd numbers if we can somehow sort them and if you look at all the even numbers we can somehow sort them then the answer would be yes. So with this claim what I'm trying to do is I will first move all odd numbers in starting stop and move all odd numbers at even at n I'm not changing the order of the element like for example If my array is like this 2 4 1 3 6 I can first move this one before here and then one over and I can choose this three move it before and move it before and I can do the same for move it before move it before move it before. My array would become like this. 1 5 2 4.
This is one simplification that I am doing first because I know that I can swap two nearby equal elements.
Now my idea is let's try to sort these elements.
In this case it's already sorted and let's try to sort these elements also separately with some jugard or something.
And let's say after sorting I end up being 1 3 5 2 4 6 like in this case it happened to be the sorted one but if it was not then I can again move all the even elements to its required place and I would end up with 1 2 3 5 4 3 4 5 6 because if this is sorted I can first try to move it to its correct this then I can choose to then exit because you don't have to cross two and four again.
So this is what actually the editorial is also trying to do.
Now it says like this. If I'm only focus on the even elements.
If I have some alternative pair for example looks like this.
6 then I should try to make this a sorted array using inserts and sort of or things like this. So first I can keep this two as it is.
Then I look at the next number. Six here is at the right position. So I keep it as it is. Then I look at the next element. 8 is also right. So I keep it as it is. Now I have a zero here.
So I should figure out some magic using which I can move the zero here to here.
So my current pair become 0 six.
Then I have four here. I should try to sub four with eight and four with 8 zero.
I should try to get array like this.
And is there a way I can make an array like this? For example, here I was 2 6 8 were already sorted. Now I somehow ended up with a zero.
Now I have to move this zero over here.
Can I somehow do it?
And I have to try bracket always like this.
The only time when I can try to do it like this is for example if I have a line here and then some more elements. I am right now not trying to look at it but if I have a nine here then what I can try to do is I'm not saying half But let's say if I bring this 9 magically over here then my array would become 9 to 6 8.
I can first apply reverse operation on my array would become 0 8 6 9.
Then I can also apply a reverse operation again here.
It will become zero 9 to 6.
And then I can again move 9 over here and then again to a seven. Here I can remove nine anywhere between this because all the other numbers are odd.
So minimum maximum if you can somehow have it between that.
So this is what editorial is trying to prove. So now you maintain a sorted list of even numbers. You try to add more elements to the back of it.
And if the sorted array is not maintained, can you somehow make it sorted by choosing some element from the or part and I believe we possibly need the maximum and minimum of the odd even elements.
But let me just 41 are the minimum maximum.
So now if I have to I have some sorted element of even numbers then what I can choose it either I can choose maximum odd from here and use it to move the numbers around or minimum odd because what I really need is or another way to put at it is Like let's if you have 2 6 8 0.
Let's try to first move this zero before 8.
You will end up with 2608.
Then try to move this zero before six.
You will get two 08.
Then try to move this zero with two here. You will get 0 to 6 here. Like one at a time. looks like some bubble sort or something. So now I have two nearby elements. Let's say 8 and zero. Then I swap them using some another element some more numbers.
If I can do it then we are good. So the way I would do is like I have some set of even numbers then 8 then 10 then zero and then few more numbers.
I will first move some odd number from here. We can freely move it among the even numbers.
So I now have odd 8 0.
If I can apply an operation on this then I can make it zero 8 odd and then I can take odd back to its original position.
So for me to apply this operation here we have odd 8 and zero. If my minimum is odd you remember like Minimum plus max equal to odd means either my minimum is odd and maximum is even or my minimum is even maximum is odd.
If this is the case then maximum should be eight and the minimum odd should be less than zero. Or if this is the case then this is zero and my odd should be greater than it.
So I should have one odd number either less than zero or odd number greater than only I can swap odd zero and it array has elements like this then I'm good.
And if I'm trying to satisfy this condition then I can just choose odd equal to minimum odd element.
And if I'm trying to satisfy this condition I can choose odd equal to maximum odd element.
And this is where and I believe this also.
Essentially, this was the idea for this work and I mostly looked at the editorial and editorial simply says this ing I believe a thousand people solved it in the contest but I doubt more than 500 would be able to solve this. I will probably rate it around 1,800 1900 very high.
Now let's try to find simply making it as two separate array because odd can mean there something even can And you don't have to do it.
Please.
or menu as both of these elements.
then it's possible to sort it because the maximum would be both of them otherwise x is more than both of these elements and also we can say we cannot shimly I check All right.
This was actually the problem of Mostly the idea I implemented whatever was in the editorial mostly right now. Any questions otherwise we will move on to problem E and then wrap up.
Wait one second.
simplif one.
The order is already then you don't need Any questions? Anyone on problem D? We will move on to problem A and that would be last problem Okay. So, problem is same as problem C where we tried to do binary search and all the fancy stuff. Now, here it ask us to find the answer for all the prefixes of the answer, not just the whole level.
This changes dynamics a bit.
The key takeaway that we need to remember from the previous problem is the observation that either you keep the numbers same or you decrease it by at least half and you can get every number from half to the remaining element.
The thing that we need to remember from the previous time is for example if we have 19 then we can make it any number from 0 to 9 or 19 or if we have 30 then we can make it any number from 0 to 14 or 30.
Overall idea would be as follows.
I will try to form some pairs already.
Some numbers would remain fixed just like the last time where I keep them same and some number would be divided into half.
I will maintain these two arrays fixed that remains same small number that become half the idea would be as follows I would let's say I have a1 up to a and let's say the answer for this is the max for this is M.
Then I will try to put AJ + one in my matching set and then try to check if I can get M + one or not. X has M + one. Basically, can I get numbers from 1 to N? If it's there, then I will do M++. Then I will check again. Can I increase answer by one more? If yes, then I will do M++ again and I will keep doing this.
So that's the overall idea.
Now what can we do over here?
Let's say we are checking. We know that the answer up to 1 to m minus one is possible.
And the question is can you make it zero up to n or let's say we have this or let's try to take some running example uh let's say from the fix I got this from two four and I got zero from six.
I got one from 10.
I got five from let's say 90 and then there is no way I can make seven or something like that.
Like the crucial ideas if I have unmatched number let's say two my max is seven so far so 2 4 and six are my fixed numbers that I have kept them as it is and all the numbers other than seven are like this I have kept one copy of the numbers less than seven and let's say the remaining number which I want to change That was my B array earlier or this.
So what I can try to do is I can try to make this smallest number a zero. Second smallest number a one.
Third smallest number two is there.
Three.
Four is already there. Fourth smallest number of five. six is there and if I have 12 then I cannot make seven or something like that. So in my sorted order I will try to fill my numbers as it is and if I let's say I had let's say my unfixed numbers were like this 10 8 12 10 and 12 and these are my again fixed numbers.
I can get zero from two. Eight from one.
0 is there. One is there. Two is there.
Three I can get again from 8. Four is there. I cannot get five. So this is a waste. No use at all.
I cannot get five from 10. So this is also a waste. I can get five from 12.
I have six over here.
And let's say the remaining numbers are 13.
So six is there. I cannot get seven. So this number is also a waste.
So what I will try to do is I will maintain a list of fixed numbers.
Like if I have to make numbers from 0 to m then they will contain one copy of the numbers from 0 to m.
And for the remaining elements I will try to fill them in as small as possible.
And here my set count is 2 8 8 8 10 12 13 1 2 3 total seven numbers among them. If I can somehow figure out what numbers were a waste, for example, in this case 8, 10 and 13 were waste, then I would say I can make four four smallest numbers from the missing s.
If three numbers have been wasted, this means I can make smallest four numbers.
and how do I try to match numbers like this? So the idea is I will have a number line.
This is my number line.
All the numbers that I already matched.
I will put a zero there which are 2 4 6 from the original part and I will try to put one on all the remaining elements.
and we thank like not sure you are getting what I'm doing or not. But this is some magical stuff I'm trying to do. My overall idea was I tried to I'm trying to find the numbers that I cannot match with everyone and then my remaining elements are like this using two. If I have to make it smaller then I can only make a zero.
I cannot make one using it.
So I will put a minus one here for two for eight. I can make numbers from 0 up to three if I have to make it smaller.
So I will put a minus one for that eight. And I actually have 38.
So I will add three minus ones here.
Then I add 10. Using 10 I can make numbers up to 0 to four.
So I will put 1 - 1 here.
From two I can make numbers up to 0 to 5.
So I will put 1 minus one here.
And 13 also I can make numbers up to 1 to 5. Then I can make one more minus one.
The numbers that are already fixed, I place zero on their behalf.
Numbers that I could not get from the fixed area, I kept one one everywhere.
And for all the numbers that I have to now decrease by half. So like this zero denotes if my array had numbers, my array had these numbers 2 4 6 2 8 8 8 10 12 13. These were the original numbers from my array. What I knew was I can make max between 0 to 6 or basically I can make all the numbers from 0 to 5 and I'm trying to see if I can make number equal to six or not using that binary search property as well because if you add more numbers into the array your max can only increase.
Now the idea here was We have to now somehow pair all the numbers. The earliest one with the earliest minus one.
Then the next one with the earliest minus one you can get.
Then the next one with the earliest minus one you can get.
Now this the earliest number before it would be this and you will essentially have end up with three minus one which are not available anywhere and if you look at my original part as well I had three numbers that were not paired with anyone. So if my this array 1 2 3 four total there were seven numbers and I could not pair three numbers with some numbers then I know that I can have the smallest four numbers and the fifth number would be the missing number and one way to count pairing like this is we start looking at the prefix sums here if I put one and minus one over here the value here would be zero here the value would be one here the value would be zero.
Here the value would be here it was 1 -1 -1 -1 it would be -2.
Here the value is -1.
Here the value is minus one.
And now what we need is the minimum prefix sum over here. Like for example it's possible that here you will have zero.
This is how each of the values are.
And what we need to find is Minimum element in the prefix sum of this array like like overall idea would be like if I'm looking at movie look at the numbers that I have if I have unmatched pair I increment my counter by one.
Whenever I receive the maximum number I can get, I decrease my counter by minus one and I keep moving it. And whenever I am decreasing my counter, if I cannot get a matching pair, then that one element was not matched with anyone and like finding the minimum sum prefix sum using a segment has appeared quite a few times.
You can possibly use a lazy segment tree or a normal tree with maintaining some prefix subarray sum and minimum over there. So this is the part that I'm mostly skipping keeping it you as an homework access. Essentially you have to solve this problem.
You have an error.
You have updates of the following form.
You are given I comm X.
You do a plus = X. X can be positive and negative.
Update query gives you find minimum prefect minimum element in prefect sum of this array.
And my claim was if the minimum value is let's say minus 10 then I have 10 numbers which I could not pair with anyone like I haven't explicitly proved it but overall this was the idea any questions anyone so far like this 1 - 10 kind of looks a bit magical but it has appeared quite a few times.
So I add a segment tree using which I'm trying to figure out the minimum prefix sum that's maintained in the first element on the second I have filled each of the element with one for initially because everything is one this is my update query if I have to add a value at a particular index Now I start with my max as the zero.
I somehow push my number and then somehow increase max by one. If I was able to do it, I increment it by one and I keep doing it. And at last whatever value I could not increment, I put it into the answer. Kind of like you are doing two pointers. sometimes adding value from the array sometimes increasing a max and you have two different value. Now if I have to put the current element somewhere I have two variables here fixed number and the small number which numbers that I have to make smaller and I could not pay this is the limit up to which I can decrease. I have kept it n by two because n is my some number n by 5 because if your 10 ^ 6 also like you don't need to take anything if the current element is less than max and the current value is not in the fixed array I add it into my fixed array and I make that value zero every value was initially one so I decrement that particular value by minus one otherwise I put it in my smaller array and Whatever the maximum value I can get by division I have added a minus one for that. So for example for 10 this minus one would be at four and here I am changing that zero to one.
Now if I have to expand it if my current max is cur if I have not made it from the fixed array like basically if there exist and then I'm checking does there exist any element equal to my current element like here I'm you can possibly use a multi Okay.
Then I can make it in the fixed array. I again change it that value from 1 to zero.
I erase the current element and I'm reverting the operation because earlier I was doing minus one. Now I have to remove it.
Now I try to figure out the minimum prefix minimum element in the prefix sum of the elements that I had with my magical timeline. Negative of the that would be the maximum element that I would end up facing.
So the size of small minus the element that were wasted is actually the number of elements I can get by decreasing and pair it with something. And the new elements I need would be like this. This is the total number of element. These were formed using the fixed.
So this should technically be equal to also also. So if both of them are equal then I can get the requirements.
Keeping it is fine because I'm taking product on all the elements not just zero.
Any questions? Anyone on the problem?
The idea of like counting minimum maximum you can probably try to solve the problems here. For example, how do you count the minimum segment?
This is one more problem that you can try to solve for.
I'm not sure if there are more problems over here, but that's the overall idea we have.
Any questions anyone? Otherwise, I intend to wrap up this stream. I rushed a bit fast on the last problem. It's already almost 2 hours now.
And thanks everyone who all are here so far.
And if you have any questions related to this contest or anything else in general then feel free to ask it. I would try to take answers for 5 minutes and then well did get to learn a lot with stream.
Thank you.
Okay, currently I am 1100. How can I improve my actually the advices I have are quite generic or if you have your code forces username or something then I can try to take a look at that.
And the most generic address is you can join in my discord server and you can look at this road map to add like my ideas are not going to change advice I'm going to give are pretty much but yeah like idea is like the current rating is around 1100 and you have solved 26 260 problems so the advice for you would be like participate in adoda beginner contests and try to absolve the problem for example you learn something new C today right and were you able to solve C in the contest or then yeah like I would strongly advise you to actually go back and try to code the idea for C at times when you listen someone's idea you do things like okay this is something that sounds fair but only when you go back and code you will remember it for the next time so that's the overall idea And yeah, thanks a lot for joining today.
I definitely you should try to code at least one more problem from what you could solve into the content and that's how you improve. So thanks everyone and I will wrap up today's stream.
Related Videos
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
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
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
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











