 What's up guys, my name is Michael and welcome to my YouTube channel. Today we are going to go over codeforces round 667c yet another array restoration. Okay, so you have, they have, so there's a secret array. You don't know what this array is and you need to restore it. Okay, there's some facts you know about the array. All the elements are distinct positive. All right, they're greater than zero. They cannot be zero, by the way, they're greater than zero. They're distinct. So there's no repeating. Okay. The array contains two elements X and Y. And we know that they're given to you X and Y. And if you were to sort the array in increasing order, the difference between any two number is going to be the same. Okay. So basically this means it's an arithmetic sequence. Okay, because if we sort then all the numbers and subtract it, the differences are going to be the same. So that shows it's an arithmetic sequence actually. Okay, it can be proven that any array exists under the constraints given below. Yeah, that is true because if you just add one certain number of times, you're going to get X to Y, by the way, regardless what happens. So yeah, okay. So now what is the problem? We need to restore an array that has the minimum possible maximum element. So we want to minimize the maximum of A1, A2 up to An. Okay. So before we start this out, basically, we're given the arrays. We're given the, so here, we're given, we need to create distinct elements of arrays. Right. So these elements are all distinct. Okay. Basically, we need to create an arithmetic progression, arithmetic series, where we add a certain number of elements, certain number of times, and then we're going to get from X to Y. Right. So we're given X and we're given Y. These two are certain numbers in the array. And we need to create a sequence of n elements. Right. We have n elements. And basically, we want to minimize, minimize, yeah, minimize the maximum number, the maximum number of the elements. So the minimize the maximum number of elements of A1, A of n. Okay. Okay. So what does this mean? Basically, we want to minimize the maximum number of elements of the max number of A1 to An. Okay. Okay. So before you start this problem out, think about what you what does it mean by minimizing the max number of elements, the max element. They're basically saying is that based on the number of elements, the elements in our array, we want to keep the maximum element as small as possible. Right. So this, let's say Y is our, I don't want to say why. But basically, let's say we have the two elements X and Y. Okay. And let's say Y is our maximum element, we want to minimize Y as much as possible. Although you can't really change Y, right, because it's given to us. But we want to minimize this to make sure the maximum element possible is as small as possible. It's kind of like an oxymoron, but we want to minimize the maximum element. Okay. Because we could change the maximum element. So you could just change it anytime. But yeah, based on whatever array you do. Okay. So how do you do this problem? How do you do this problem? Okay. What did I do? First of all, first I looked at this. Minimize the maximum of A1 to An. Okay. Let's look at a first problem. In the first case, 2, 1, 49. Well, if you're given two elements, N, right, your two elements, our N is two, and we have 1 and 49, you can't really change anything about it, right? Your array is just going to be this itself, right? You can't change anything at this point. You're given two elements. There's two elements. So you just have to print the two elements, right? You just print, print both of these. So that's the first basic case they gave us. And that's actually good that they gave us, that's just 1 and 49. Alright. Here's the hard part. Let's say N is five and X is 20 and Y is 50. Alright. So in the second case, N is five, X is 20 and Y is 50. We won't remember. We want to minimize this. So we're given five elements. One, two, three, four, five. And we want to create a progression. Basically a progression. Starting where you, if you, an arithmetic progression where you add a certain number, number every time. Here, remember, based on here, if you sort it in decreasing order, right, you subtract them, they're all going to be the same. So it's an arithmetic progression, right? So if we're going to minimize, minimize, minimize, whatever, the maximum element of a one up to a N, what do I do? Well, if I want to minimize it, the maximum element, let's assume that the maximum element is going to be 50. Right. So we start at 50 here, and then we have 20 is our smallest element. Well, then how do I, how do I come up with the progression here? Well, it's simple. We just take 50 minus 20, which is 30, and we divide, right, the number of spaces that it takes. So in this case, it would be 50 minus 20, which would give us 30. And we just have to subtract divide number of spaces it would take. So in this case, there's how many spaces from 20 to 50, it'll be 1234. And 30 divided by four doesn't give you an even number, right? What is that? That's like 2048. I can't do my math right now. 728 to 7.5, right? So in this case, you would have to add 7.5, 7.5, 7.5. And we can't do that, right? That's not possible. We can't do that, right? Because our sequence is arithmetic. The sequence is arithmetic, right? Remember, it has to be an integer. We're subtracting them. It has to be equal. So what can you do now? Now you think about it. What can you do now? How about we move our maximum element of Y down by one? So instead of being all these numbers, right, 7.5, how about I just clear it? And as five, remember here, we have 20 and 50. So X is 20, Y is 50. So now how about how about I decrease it down by one? So now we have 20, 30, 40, 1, 2, 3, 4, 5. So remember last time we set our Y as our maximum element on the far side, and we couldn't get a progression there. How about you move it down by one? I'm going to set my Y here now, okay? Let's see if we could subtract by it, find if we could make a progression out of this, right? If we move our Y down by this in case a maximum element does increase, right? Y is not our maximum element anymore, but A, we want to see if we could get a progression here, right? So now let's look at this. 50 minus 20. 50 minus 20 is equal to 30. And how many spaces are there between 20 and 50? 1, 2, 3. Ah, 30 divided by 3 is 10. Huh, this is an even number that we could add. I'm not even, this is an integer that we could add. We could add by 10 every time. 20 plus 10 would give us 30. 30 plus 10 would give us 40. 40 plus 10 would give us 50. 50 plus, then at the end 50 plus 10 would give us 60. Hmm. Do you guys see what I'm doing now here? If we were to simply just set, assume Y is our maximum element, right? In here. And then later we decrease the position that Y is set as, right? Then we could, once we figure out a progression that we could take up to Y, we just add the number of times, add the increment of the progression, the number it would take each time and print that out once we get to Y. And then in the end we just print out the maximum element. We just print 50 plus another 10. 50 plus another 10 would give us 60. So this would actually minimize the maximum element. This would actually minimize the maximum element because we're taking, we're assuming that Y, right? The larger element here that we're given is the maximum, right? And if it's not, well, then we can't create a progression. We're going to decrease Y by the number of spaces it is, right? We decrease, right? We started at, remember we started as the fifth space here. Then we decrease it. So now it's at the fourth space. If it still doesn't work, then we decrease again, third space, second space. And then if it's first space, then that means that we know we just have to add that number of times increment. But yeah, this basically minimizes the maximum number of times it would take because the maximum element here, 60, is as small as it could be here, right? If we're decreasing this number of the Y, the maximum Y, if we assume that Y is the maximum possible here, and then we decrease it until we get an arithmetic progression, the maximum value is not, is going to be the minimum possible, right? You guys want to know what I mean? Because like if you, let's say like if you started Y as in the beginning, then you just add Y number of times, right? So like if I had like 20, and I just add Y, 50, that's 70, I keep going that number of times. This would be the maximum element, right? So in the case where Y gets moved down to the first position that you're at, that would be the maximum that our maximum element could be, right? So if we're trying to minimize the maximum element of this arithmetic progression, we want to make our Y as far this as possible from the beginning of X, right? We want to make Y as far this as possible, right? That would give us an actual arithmetic progression. Do you guys understand what I'm saying? I hope you guys understand what I'm saying. Because like if Y gets closer and closer, right? Like in this case, this case is when Y is like as close as possible because at this point, we're just adding 50, right? We're just adding Y. So this is actually would give us the maximum element, the maximum of max, because arithmetic progression at the end is just going to get super duper large. It's like 20 plus, I don't know, 50 times some number of times of 5, right? That would give us 20, 220, 225, 250, right? That would give us like 270, right? This end at this end would get too large, 270. So we want to make sure our Y is as far this way as possible when we create our arithmetic progression, arithmetic sequence. And then once we get to Y by printing out this number of times of incrementing, we just have to increment Y that number of times, okay? Until we get to N. So we just increment Y 10 times. At this point, we just add 10 to get to here. So I hope I explained this pretty well here. So I'm just going to explain the code, what I did here now. So what do I do here? So whenever it's 2, I just print out X and Y because that's the least possible we could do. Then I take the difference of Y and X, right? I take the difference of 50 minus X, so 50 minus 20 would give us 30. So that's what this was for, right? And then I start the maximum space you could be as possible. It's going to be N minus 1, so that would be here, yeah, at this point, right? And then I just basically just keep decreasing space while it's not divisible by, right? So I keep decreasing where my current location is at. So if I'm at like 50, if I'm at, let's clear it again, 20 here. So we have 20 and then Y is 50. 1, 2, 3, 4, 5, right? So I start at the end first. So I set 50 here and then 20 is there. And then what I do here is I keep checking if the difference is divisible by the number of spaces. So the number of spaces that you could do from here is actually just N minus 1, right? Because you start here at the end, this is N, right? The number of spaces between here and X is going to be, since there's 5 spaces here, right? It'll just be 4, right? 1, 2, 3, 4, right? That would be a number of times it would take if we're adding an arithmetic progression to N. So that's why I start the spaces equal to N minus 1. And then I keep subtracting it when it's not divisible by it, when the difference is not divisible by it, right? So like here I get 30, 50 minus 20 is 30, right? And I'm going to divide by 1, 2, 3, 4. And this is not divisible by it, right? So what I do is I subtract my space again by 1. So then at this point 50 is going to get put down to, 50 is going to get placed here. And then I do 50 minus 20 gives me 30. And then I divide by 1, 2, 3, right? Which gives me 10, which is divisible by it. And then at that point, I just loop from, I loop from X to Y and I just keep adding 10 that number of times. So I just add by 10, which would give me 30, 40. And again, once I get to 50, once I get to 50, I keep track of how many elements that I printed out, right? And at this point, I just print out the rest, right? I just print out the rest here. So yeah, I just keep printing out the rest of the number of elements it takes. So in this case, I would just add by 10 to get to 60, right? Because there's only one element left, right? So yeah, that's what this while loop here is for. So here I kept the number of times of number of elements I printed out. And then while the number is not equal to N, I'm going to increment Y plus by 10. So here, what I did was in this if same as I check if the decrement is less than zero. So the reason why I check decrement is less than zero, because there's two ways you could do it. You could add 10 by the last element here, or you could subtract 10 from the smallest element, right? So normally, it's actually a good idea to subtract 10 from the smallest element. So then you don't have to print out, you could minimize this max element, right? That 50 would still be remain as your max element. So then yeah, you would actually subtract 10 from here and you get like 10 here. But yeah, but if we hit zero, right? If we hit zero, remember we can't have zero here. So if we hit zero, you have to add your increment add by 10 to your largest element of Y. So that's what I do here, right? So while the number is not equal to N, if I subtract the smallest value of X, remember we assume X is the smallest, and if it's less than or equal to zero, which gets negative, then I just keep adding by Y by 10 and print out Y, right? I keep adding the last element of Y by 10, so in this case I would add by 10 to get 60. Otherwise, if it's not, then I'm going to subtract the beginning value of X of here by 10, right? And then I just keep incrementing up here. So yeah, that would actually get you your answer, right? Because we're trying to minimize the maximum number of elements, okay? In this case here in the beginning, if increment is equal to zero, so we get like division by zero. So let's say our space is get too much, right? So let's say it's not divisible by anything, number of spaces. So like let's say we get, let's say this is 20 and then this is like, I don't know, 21, right? And no matter how many times you move 21 down, it's still not going to be divisible, right? One's not going to be divisible by it, it's not going to be divisible. It's not going to be divisible by two, right? There's no arithmetic progression from 20 to 21 here. So at this point, you get 21 is just right next to it. And your incrementation is actually just going to be, excuse me, you're going to get a divisible by zero, right? Because your spaces are going to end up being zero. So I just said an increment to equal x. You could set it equal to y, it doesn't really matter, but basically you're just adding a number, that number of times in the end, right? You're going to add x by like 21 or 22, or you just add 20 by like 21 and keep going in your arithmetic progression, right? Because at this point, you exhausted all possibilities of making 21 the largest value, right? So yeah, that's what I do here. And yeah, I just print it out. And yeah, that's basically the solution. Hope you guys enjoyed this video. Very calm subscribe. This was a little hard to explain, but basically you're creating arithmetic progression and you want to make set your largest, you minimize the largest value as possible, right? And the largest value is possible. You could do that. Do that. You just set your why as a large possible value possible and see if you create an arithmetic progression. If you can't, you just keep moving it downward, downward and downward. If it reaches right next to each other, you're just going to have to add y, that number of times, right? You just have to add 21 and 22 and stuff like that. And then that'll get you your maximum number of elements. But yeah, that's basically how you do this problem. I know Algorithm Thread created another video where he brute-forced it and he didn't do it mathematically. But yeah, if you wanted brute-forced it, you could also do that. But I wanted to just do it mathematically, try to create an arithmetic progression because that's basically what I got from this problem. But yeah, very calm subscribe. I'll check you guys later. And yeah, I hope you guys enjoy this video. Peace. Thank you.