 All right guys, so I was doing wormholes and after solving wormholes, I learned this cool trick how to generate no duplicate pairs in an array. So when I mean by no duplicate pairs, I mean like, let's say we have like an array of numbers like one, two, three, four, five, six, okay? So then if you wanna generate pairs, our pairs of arrays would be like one, two, and then we have three, four, and five, six, right? And then the next pair would be probably like one, three, two, four, and then five, six. So these are like arrangement pairs. That's what I mean. And then we have one, four, two, five, two, five, and then three, six, and then we have one, five, and then two, four, two, three, and then four, six, and so on and so forth, okay? So this is what I mean by pairs. And then when we say a pair, we're basically saying that pair one, two is the same thing as pair two, one. So like pair one, two, this is the exact same thing as pair two, one, okay? So basically what we're trying to do is we're given an array of numbers and then we're trying to pair up every two number together except there's no duplicates between these two pairs, okay? So then yeah, so when you have pair one, two, and you pair three, four, then five, six, we're not duplicating pairs then two, one, and then four, three, and so on and so forth, okay? So originally what I thought to solve this problem in order to pair no duplicate pairs, I thought you could just like generate all permutations. So I was thinking like do one, two, three, four, five, six, and then do like one, three, four, two, six, five, six, so on and so forth. And then in order to be able to figure it out, then just pair those together and then remove the duplicates but that takes so long. So after doing wormholes, I actually learned how to do this and it's actually a pretty cool trick. So what you do essentially is that you need to create an array of pairs and then what we're gonna do is we're gonna like loop through the array of pairs and then we're just gonna pair each one with its corresponding one that is not paired yet and then so on and so forth until we reach the end and once we reach the end, we just add it to our array and then we unpair it, okay? So once we pair it, we have to unpair it as a backtracking and then at the end we'll be able to solve this problem. Oh yeah, so by the way, when we say pairs, we're saying like each of the like arrangement of pairs. So this would be counted as like one type of pairing, one type of arrangement and this would be another one type of arrangement and so on and so forth. We're not actually saying like just generate all pairs like one, two and then one, three and one, four, one, four. No, that's not what we're saying because that would be too easy because for that you just have two for loops and then you just pair I with J. So that's not what we want but yeah. What we want is like each of the arrangement pairs. Okay, so how do you do this problem? First of all, we need like an array of like pairs and the current index, we say the ith index pair it will be paired with the jth index. So whichever at the current number location we're gonna pair it with its current location. So what that means is that if we look back to paint at the zeroth index, if we have zero and one as a pair, at zeroth index we're gonna pair with one and then at the one value we're gonna have a value of zero. Okay, so that's what it means. And then like if we have three, two as a pair at the third one value we're gonna have value two and then at the second value we're gonna have the value three. So what we need here is actually just like a vector of integers called pairs. And for now let's actually just make our size six because I think that's what we had in this example. Right, we have a size of six. So we're gonna have six and then for all the values we're gonna just have it integer underscore max. And this just represents like a placeholder if it's not paired yet, then we're just gonna have it that. Okay, and then what we're gonna do is we're gonna recursively call it and then pass in like the current index that we're on and then like our pairs, okay? So yeah, now we're gonna have the current index and then our pairs of indices that are paired, okay? And we're gonna pass it by reference, okay? So how do you do this? So basically we're just gonna recursively go through like all the values here and then if it's paired, if we reached all the end of the array, right? If our index reached all the end of the array, that means it's already paired. That means we're like we're done, okay? So what we're gonna do is we're gonna, if our index reaches n, so n is the size of our, we're gonna create like a variable called n and it's gonna equal to the size of our pair. So if we reach the end of the array, that means that everything's paired up. So all we have to do is literally just print out the values. So let's just do that, pairs at i and then we do new line and then we just return, okay? So if we reached like the end of the values, we're just gonna print out all the values of our pairs or finish pairing with every value, we just return in the end. All right, now, I'm gonna check if the current index I'm on is already paired. So if it's already paired, then we don't need to do anything, right? Cause like what's the point? We just go to the next one. So how do you check if it's already paired? So remember what I said earlier in the video is that if it's unpaired, we have a place holder called inner score underscore max, integer underscore max, which we just filled it with like two million something. So if it's unpaired, so if our current pairs at our current index is unpaired, so that means it's, well, if it's not equal to inner integer underscore max, right, this means it's already paired, right? So if it's already paired, we could just go on to the next value, right? We don't have to pair it up anymore. So if it's unpaired, we just recursively call the next one and return. So what is our current index? Our current index is index. So if we're gonna recursively call the next one, we just do index plus one. So we go to the next one and then we pass in the same thing. So this is if it's unpaired. So if it's unpaired, we ignore it. So unpaired, ignore. Go to the next one. Okay, so now, here's the difference is like, now what we're gonna do is, we're actually, we're gonna pair it up with the other values. So how do we pair it up with another values? We need to search through the array, right? So what we're gonna do is, we're gonna search through the array and we're gonna search through from the next value. So if our current value, let's say we're currently, so let's say we're on index zero, right? Now I wanna pair index zero with something. So what I'm gonna do is I'm gonna search through from one to the end until I find another unpaired pair. So if I already find another integer, if I don't see, if I find another integer underscore max, then I'm gonna pair it up with zero, okay? So for that, we're gonna loop through from J is gonna equal to our current index plus one and then we're gonna go to the right. So J is gonna equal to the current index plus one and we're gonna go to the end and plus plus. And what we're gonna do is we're gonna check if this value is unpaired. So if it's integer underscore max, this means it's unpaired and what we're gonna do is we're gonna pair it. So to pair it, we're gonna do pairs at index is gonna equal to J and then pairs at J is gonna equal to index, okay? Then what we're gonna do is we're gonna recursively call it on the next values. So we're gonna just go to the recursively call this and just to pass in our current index plus one, okay? Now, once we pair them up, all of them of values are paired. We need to unpair them, right? Cause we wanna generate the next values. So this is called backtracking. So what I'm gonna do is I'm gonna do pairs at J since we just paired pairs at J is equal to integer underscore max. I mean pairs at J just paired with our current index. We're just gonna set pairs at J is gonna go back to integer underscore max, okay? So that means now it's unpaired, okay? So yeah, and that's the end of this cause once you pair it, we're gonna, there are situations where it's not paired. So we have to generate that one. And then at the end, I'm just gonna do pairs at index is gonna equal to integer underscore max because I wanna unpair my current pair. So the reasoning behind this is that, let's say we were at, so let's say where we paired up zero with one, right? So then zero becomes one, this is gonna be zero. And then we pair up two with three. So this is three and this is two. So this here is with three. And then we pair up four with five. So this is five and this is four. So now we have, yeah, four or five. Now there's situations where four or five was not paired and there's situations where two, three is not paired, right? So if we have to unpair two, three in order to get two, four, for instance, right? And we have to unpair four, five to get like three, five. Right? So like in order to do that, we actually have to unpair what we originally paired up so we could get the next values. And I think that's pretty much just the code. So if we run this, it should generate all possible pairs. Yeah, hold up. Bam. And then that's what it is, like, okay? So now we generate all the possible pairs. So one, zero, three, two, five, four. One, zero, four, five, two, three. One, zero, four, five, four, three, two. So yeah, in this case, it would be like zero is paired with one. Two is paired with three. Four is paired with five. And this one's like zero paired with one. Two is paired with four. And then three is paired with five. And then this one's like zero paired with one. Three is paired with five. Four is paired with, wait, no, zero, one. Two is paired with five. Three is paired with four, yeah. And each of these are completely different pairs and they're completely unique, okay? So yeah, that's the gist of this code. I hope you guys understand this video. Rate, com, and subscribe. The zero is paired with, like, each of the index is paired with this corresponding pair, but yeah. So yeah, this is how you recursively generate pairs of no duplicates and they're not duplicated pretty much. Yeah, rate, com, and subscribe. I'll check you guys later. Peace.