 What's up guys, Mike the Coder here. Today we're going to go over another problem in a CSES problem set is called permutations. So a permutation of integers of 1 to n is called beautiful if there are no adjacent elements whose difference is 1. So now given n construct a beautiful permutation if a permutation exists. So what do they mean by this problem? This problem is actually not that hard and I'll explain to you guys what they mean by this. Essentially is is that let's take this example, let's take this example. Okay, so essentially is that a permutation is like a set of an ordering of numbers from 1n and so essentially what that means is like if I have numbers 1, 2, 3, 4, 5, this is a permutation. The reason why it's a permutation is because all the numbers from 1 to 5 are in this permutation. So when n is equal to 5, all the numbers from 1 to 5 are in here, so 1 to 5, they're all in here. So this is a permutation and another permutation that you could have which is like another ordering of numbers from 1 to 5 would be like 3, 2, 1, 4, 5. And the reason why this is a permutation also is because all the numbers from 1 to 5 are in here. So all the numbers 1, 2, 3, 4, 5 are in here. So that's essentially what a permutation is. It's like an ordering of numbers but all of them have to be there. So like 5, 5, 5, 5, 5, this is not a permutation of 1 to 5 because we don't have the numbers 1 to 4. This permutation, this ordering only has like the only single number of 5. So the values of 1 to 4 are not in here. And we need to have all the values from 1 to 5 in order to have this as a permutation. So these two are permutations. So now what they're saying is that a permutation is beautiful if there are no adjacent elements whose difference is 1. So what that means is that let's say in this input that we have, let's go back to this input. So our input here is 4, 2, 5, 3, 1, right? And if we were to take the difference between all these values, so if I take 2 minus 4, this would be like minus 2, and then if I take the difference between 5 minus 2 would be 3, and if I take a difference between 3 minus 5, that would be minus 2, and if I take 1 minus 3, it would be minus 2. So all these differences between all these, we don't see any of them are equal to 1. None of them are equal to 1. So that's why it's beautiful. So this is beautiful, because none of these differences are equal to 1. If you subtract any of these, we don't see any difference that is equal to 1. None of them are equal to 1. So this is a beautiful permutation. So now what the problem wants us is that given this number n, so given 5, we want to construct a beautiful permutation. So what we want to is we want to have an ordering of all the numbers from 1 to 5, and we have to make sure each of the adjacent differences are not 1. So all these differences are not 1. All these differences are not 1, and that's what we want. We want to make sure that all the adjacent differences are not 1. So how do you do this problem? The easiest way to do this is pretty easy. This is the most basic way to do this. What you could do is first you could create an array of all your values, let's say all the values here, of size n of equal to 5, so n is equal to 5. So I create my values of n equal to 5. What I'm going to do is I'm just going to put all the even numbers first, and then I'll put all the odd numbers. So here I would have like 2, 4, that's the even number. Six is greater than 5, so I stop, and then I start back from 1 for the odd numbers, 1, 3, and then 5. The reason why this works is because since all the even numbers, all the even numbers, if I just print all the even numbers from least to greatest, their difference is always going to be 2. So if I do 4 minus 2, this is going to be 2, plus 2. And if we had more even numbers, let's say that n was equal to 6, we'd have another plus 2, plus 2, plus 2, until we get down to a number that's greater than n. So that would be like the even numbers, right? And then it's the same thing for the odd numbers. Odd numbers are always going to be like, start from 1 here, plus 2. It would be a plus 2 difference between here. 3 to 5 would be plus 2 also, and so on and so forth. And we know this is true because there's only one location where it's not plus 2, and that's like the intersection when it's like going from even numbers to odd numbers. So in this case, it would be like 1 minus 4, which is minus 3. So no matter how many, no matter what, if we just have all the even numbers first and then all the odd numbers, all our differences here would never be 1, right? So this would be a beautiful permutation. So the answer to what we need to do is we need to have, just print all the even numbers first and then print all the odd numbers, okay? Now in the end, once we do that in our array, we should also have a check value to check if any of the differences are equal to 1, right? So if we do have a difference is equal to 1, then we just print out it's not possible because at that point, yeah, it's definitely not possible, right? But in this case, it is possible because all our differences are not equal to 1, right? So that's good. So how does the code look like? I'll show you guys real quick. So just what we have to do is we have an index loop to the half waypoint and just put all the even numbers first and then put all the odd numbers. So do that. I'll just show you guys right here. It's not that hard. Let's go here, okay? So like down here, oh yeah, yeah. So here we created, we read in our value n, which is our number of the input and then let's skip for the first part for now. But yeah, what we're going to do here is we're going to create an array or a vector. In my case, it was vector, but you can use an array and Java of size n. It's going to be called d, okay? So d is going to be the array that we're having. Then what I'm going to do is I'm going to create a variable called start. And then I'm just going to print all the even numbers. So to do that, I'll just print 2, right? So here I'm going to fill up my array to the half waypoint of all the even numbers first and then all the odd numbers afterwards. So filling up all the even numbers first, I just do j equals to zero, right? This is my index from j equals zero and I go up to half waypoint because there's only half even numbers and then half odd numbers, right? And I just do d of j is going to equal to start, right? So start is equal to 2 and that's just going to set each array value to equal to that. Then to ensure that it's an even number, I'm just going to do start plus equal to 2. So what this does is it's going to have filled the array up to the half waypoint of 2, then 4, then 6, then 8, then 10, then 12, so on and so forth, okay? And to make sure it's I only go to the half waypoint. So I stop halfway for all the even numbers. Then what I do is I just set start now is going to equal to 1 and when start is equal to 1, yeah, start is going to equal to 1 and I'm going to fill up all the odd numbers now. So to do that I set j is equal to n over 2 now because this is the second half waypoint right that I'm looping through to fill up my array. So j is going to equal to n over 2, which is the location where we stopped at, right? And then I'm going to go to the end of the array, j is less than n, j plus plus. And then I'm going to do d of j is going to equal to start. So this is just filling up my array and then I'm going to do start plus equal to 2. So it's going to be 1 plus 2 which is going to be 3 and then plus 2 would be 5. So it would be 1, 3, 5, 7, 9, 11, 13, so on and so forth. So that's going to fill up all the odd numbers, okay? So at the end what we're going to have is we're going to have all the even numbers first to the half waypoint and then all the odd numbers. So all the even numbers here, 2, 4, 6, 8, whatever, to the half waypoint and then all the odd numbers. Okay, now what we're going to do is we need to check if it is possible. Okay, so for that I'm going to see if it is possible. So this is just the last test case in case there's any test scenario that makes it impossible. So for that I created a function called find and this is what the function does. Find is just going to subtract the, it's going to start from 1 and go to the end of the array and I'm just going to subtract the current value from the previous value and get the absolute value, okay? And if any of them is less than or equal to 1 or, well yeah, if any of them is less than or equal to 1. To be honest, if any of them is not equal to 1, well technically then it's false, right? Because we don't want any of them to have adjacent values of, I believe, yeah. Any of the adjacent values are equal to 1 and it's not possible. So if the absolute value of any of these are less than or equal to 1, we're going to return false, okay? Otherwise we're going to return true. So then once after this checkpoint, if it is true, then we're just going to print out all the values in our array and otherwise we're just going to print no solution and that's just the code. That's all we need to do. So I realize in a few test cases, if n is equal to 1, which means that we just have one value, it's always going to be 1, right, because it's just going to be 1. So that's just like a simple case that you could do. So it just returns 0. Yeah, that's a simple case you could do. If n is equal to 2 or n is equal to 3, I realize there's no solution and the reason why for that is that if you print out the even numbers for, if n is equal to 2 and you print out the even and then the odd, right, if I print out the even numbers, so if n is equal to 2, so let's say n is equal to 2 and I print out the even numbers up to n and then all the odd numbers will just be 2 and then 1 and then if you realize the difference between these two, it could never be possible, right, because this would just be plus 1 or minus 1, right, whatever, whatever value put here is always going to be plus 1 or minus 1. So yeah, that's definitely not possible. If n is equal to 2, the same thing if n is equal to 3, because if you have like, I don't know, 2 and all the even numbers up to 3, so 2, then you can't print 4 because 4 would be greater than 3, then you have like 1 than 3, sooner or later you're going to have like a difference of 1 here because 2 minus 1 would be 1 or plus or minus 1, whatever way you subtract it, so it's not possible here, so that's why it's not possible if it is equal to 3. But yeah, that's the gist of it, that's essentially if n is equal to 3, it's also no solution. If n is equal to 2, no solution, that is a return. But yeah, I hope you guys enjoyed this video, that's all that matters, and this is the problem set. For you to come subscribe, I'll check you guys later, peace.