 What's up guys Mike the Coder here today? We're gonna go over the next problem of two sets. So basically we have a List of numbers from numbers one to n and we need to divide this list of numbers into two sets of equal sum, okay? so Yeah, if it's possible print out yes, and then print out the two sums if it's not possible print out no Okay, all right. The first thing you have to realize is that So let's say let's say n is equal to five. Okay, that's a n is equal to five. So we need to divide The numbers one to n into two sets of equal sum so let's just Let's just just print out all the numbers one and so one two three four five, okay So these two sets have to have the same equal sum, right? So I'm gonna split this into like two two sets This is the set and then the set and they have to have equal sum Okay, so before we do that Let's just add up all the numbers from one to five because that'll be our total sum and we need to split These numbers into two sets of equal sum So if I add them up one plus two plus three plus four plus five What is that gonna give us that's gonna give us three plus three six plus four is to 15, okay? So now What you realize is that if our total sum is 15 and We need to split this into two equal sums. This is impossible right because Let's say that's the first one was like three And then let's I put three in the first one and then I put like I don't know four in the I don't know two in the two in the first one also three in the first one two in the first one And I put one and four in the second one I have to put five somewhere right So I put five here No matter how I try to arrange these two numbers It's not gonna be possible because this number our total sum of 15 Is not divisible by two Right, so if it's not because like if our total sum has to be able to split into two parts right like if I had like If I like two four six eight or something This total sum has to be able to split into two to two different sums, right? So like this has to be divisible by two in order for this to work Otherwise, it's not possible Right if I want to make this sum this set add these values up to equal to this set Right our total must be divisible by two. Otherwise, it's not possible. Okay So Yeah So our total sum from one to n has to be divisible by two in order to afford to be possible Otherwise, it's not possible. Okay, so yeah, so Now what we're gonna think about is How do I get the total sum? So let's say I have Numbers from one to n so let's say I have them one two three four Okay, let's go up to four at this time because This total sum is definitely divisible by two. Okay So if I want to add all the numbers from one to end, there's a formula to do it, right? So in this case n is equal to four, right? This is the formulas n times n plus one over two Right, this is the sum of all the numbers from one to n so one plus two up to n, okay, so this is what it is. Okay, so then What you could do now is you could do four times Four plus one is five divided by two. This is gonna give you Ten so this would be this would be the sum of Numbers from one to n and then you just have to check if this is divisible by two and then Yeah, if it's divisible by two then you could split into two sums. Okay, so now we need to Now that we have this we need to think about okay If it's divisible by two how are we gonna create or to create or to Sets that we could add up to the value of ten. Okay, so yeah, let's do that now Okay, so the first case what you could think about is that let's say our n is even right? So n is equal to four If you were to just add up all the numbers the first and last term so if I do one plus four one plus four, this is gonna equal to five right and Two plus three the next two terms first and last term is also equal to five right and If you think about this, this is the same thing no matter what from any numbers, right? so because of this Because they're always the same values When you add the first and last terms over and over again even for any even number of n What you could do is you could always just pair the first and last term You know one set and then alternate the sets so what you could do you could do So let's say our we have two sets here I just always put I'll put first and last term before in this and Then I'll just put two and three in the second one and just keep alternating right and then in the end The sums of this side is going to go to five The sum of this side is getting equal to five no matter what there are both these sums are going to always equal be equal in the end because of Because adding the first and last term is always the same for When n is even right if you pair the first and last term and add them up There's all we're going to they're always going to have the same number so yeah That's one way to do this Just always pair with the first and last term put in one set and then put the second Second to last term put another set and I just keep alternating it and then sooner or later you're gonna have the same Same two sets that have the same sum. Okay, so that's the first case What's different is if n is odd and that's the case where it's a little tricky Okay, so that was the case when n is even so let's go to the case when n is odd where it's a little tricky So as you can see here if I do n is when n is odd And I try to pair the first and last terms right and put them in a set So I do try to put one and seven into one Right, so I put one and seven into one set here And I try to put two and six in the other set So I try to do two and six in the other set alternating it right see they saw the same sum currently I Try to do three and five into one set So I'm gonna put three and five into this set again, so three and five In the end we're gonna have an odd number and the last value is four So I can't just put four here because then the sums are not the same right here We would have 16 if I add up one plus seven plus three plus five. This would be 16 here I would have 12 so yeah, that doesn't work so what could you do in this case of When there's an odd number What you could do is that if you were to look at a pattern you see that This last term is seven right? I could take one plus six and this equals to seven I could take one plus six right one plus six One plus six is equal to seven right, but not just that two plus five also equals to seven five also equals seven right two plus five is equal to seven and not just that three plus four is Equal to seven three plus four is equal to seven so because of this We have all the numbers and we are all pairs all already in all these cases And they all add up to seven right and then we use all the pairs already currently besides the last term right We paired everything besides the last term, so I'm just gonna put this here besides last term So what you can do is that we could put the first and last term besides Seven right besides n we'll get a pair of the first and last terms all these values except for n and put them into Alternating sets so what I could do is Here I could put one plus six is equal to seven right so I could put here are my two sets right to my suit you sets So I put one plus six is equal to seven right so I put one plus six here So I got sort of this I alternate I'm gonna put two plus five. It's also equal to seven, so I'm gonna put two and five here Right, so I just got rid of these and then three plus four is equal to seven right So I put back into the first set three plus four, and that's also equal to seven so now they're all gone right they're all gone and We just have this last term left Right, we have this last term left to seven and because all these this sum of one plus six is equal to seven right? This is equal to seven These two three plus four is also equal to seven and two plus five is also good seven and the only thing left that is not equal to seven is The smaller list the small list here right to five and if we want to Just get this smaller list equal to seven also We just we could just add the last number n into our small list Into our smaller list of seven so I'm gonna put this in here and then in the end We have this group is equal to seven this group of sums equal seven this group is equal to seven And this last number is also equal to seven So in the end both of our two sums of our all our sets all our sets of seven plus seven is equal to 14 and Two plus five plus seven is also equal to 14 so this is Also equal to 14 so then both of our sets in the end are going to equal to The answer so essentially is is that whichever we could just pair all the numbers from Para the first and last terms all the numbers except for n Right except for n in case when it is odd and then put them into alternating sets And then whichever is a smaller one We could just put this last term of n into it and then they'll make sure both of our two sets are Have the same sum okay, so that's essentially the gist of it So I could show you guys a code now and I'll be on my way all right guys So now I'm going to explain the code to you guys. It's not super difficult Although I didn't really comment it out enough and it's not like super I guess I'll just explain it to you guys like what I'm doing okay, so the first thing is that reread in the number n and We have this if the sum is not divisible by two so if it's not even Right if it's not even We print out no because that means that it's impossible to split it into two groups Okay, where the sums are the same Right, they're just from the first part of the video Otherwise it is possible. So I'm going to print out. Yes. Okay. Now. I need to create my two groups Right right my two sets that I have to print out So first I'm going to create a vector of long longs of F is going to represent my first set And then s is going to represent my second set right so I have two sets right f and s okay All right, if n is even if n mod by 2 is equal to 0 So if n is even I could just pair the first and last terms, right? I could just pair the first and last terms and I just alternate them right so What I do here is to pair the first and last terms This is basically the same using the same idea as reversing an array Where you loop only up to the middle and then you just subtract the length of the array from the index that you're looping In order to get your first and last terms. So if I were to dry it out With you guys, this is like a trick you could use So let's say I'm at Let's say I have one two three four Hopefully this works. It's working. Oh, it's not working Hold up give me a few give me a few seconds. Let me just have this in here Okay, so this is working now. I believe Yeah, okay, so let's say I have the number one two three four Right if I want the first and last term to pair them up and alternate them I could do is a I Could just loop so I have the indexes for zero one two three, right? So this is using the same idea as a reversing an array I could loop through to the middle to values to the middle of the array Or whatever it was so loop two to this middle and then I just take The first last term I just take so our value is equal to n right and is equal to four So I loop through to the middle of Up to up to two but not including two so I go to Well up to the middle here and then what I'm gonna do is I'm gonna take first and last term So what that is is that's the index? This is our index, right? This is our index our index is this and What I'm gonna do is I'm gonna pair this firm last term into one set and then two or three the other set So if you want to alternate Easiest ways is to check if the index is even or odd and They'll they'll just alternate you to put the values into one set in the second set now to get the first and last term It's pretty easy First term is just going to be Your index plus one right because if I'm looping from zero to two To have the halfway point of my array right Zero the index plus one it will get you your current number because in our array. It's just one to four right So it'll just be index plus one. So the first term Which index plus one so zero plus one would be one So that'll be our first term and if I want to get the last term using an index I you from zero to The halfway point. I just take the length of my array of N and subtract from my index So in this case, I just take n is a four minus index of zero which gets me four and that gets me my last term so I don't have to use like any of any crazy like ridiculous Counters and stuff like that. I don't want to use that So this is a little easier to to deal with right and then um, then if I want to alternate To add to the another set right all I have to do is just check if my index is odd or even and I'll alternate Which sets I could put it to so since now one is odd, right now I just do the same thing so I take my index of one plus one Which which will give me two right? I want this number two so the next one plus one will give me two so put two here And I want the last term also, so that'll just be four minus one like the value of n Right the length of n minus your current index of one, which will give me three and I'll give you my last term of three So I just add three into my second Second list, okay, so then I have two lists of one four and two three, okay So yeah, so the code what I'm doing here is um I'm looping through from zero to n over two, which is going to be the halfway point And then if it's even right if the index I'm looping is even I That puts all the pairs into one list of f Otherwise it put I'll put all the pairs into it the second list of s because this is alternating How I'm putting the two pairs, okay? so the The first part the pair of the first add the first and last terms is just going to be your current index plus one, right? I plus one Which is I plus one which is will give you the first value and then the last terms of what you're looping to is Just going to be n minus I right if I'm looping from I from zero to the halfway point. It'll be n minus I So yeah, so it would be this first first list I push back I plus one And then f a first list I push back and minus I which will be the last term and then I just alternate it So if it's if it's odd if it's even I put in the first list Otherwise if it's odd I put the values in the second list the two pairs in the second list, okay? So that's that's the case if n is Even Now if n is odd Remember what what happens when n is odd? We're going to ignore the We're going to ignore the last value right and then whichever smaller I put the last value event into that set So let's go back to when n is odd So back in our when in our case when n is odd. That's when That's when one two three four five six seven Right n is equal to seven So what I do is I need to ignore this last term so I'm just going to ignore this and to do that I need to now pair the values from The first and last term is ignoring the last term of seven, right? So pair one to six two to five three to four right three and four as the two sets So it's essentially the same thing that I'm doing here But now my loop is going to be n minus one over two right because I'm not going to loop through the halfway point of Of The array anymore. I'm not going to loop through the halfway point of this What I'm going to loop through halfway point is going to be here, right because I want to pair that one at six Two and five and three and four, right? And to get the halfway point of this number What is the halfway point of the values? Well, if our n is equal to seven and I want to get the halfway point not including but the last value of seven All I have to do is just take n minus one, which will give me six right n minus one Right seven minus one is equal to six right and that's one to give you six I just divided by two so that'll get me my halfway point that I'm going to loop to So then I would have a halfway point loop through right where we're just getting rid of the last value So if we have seven numbers, we get rid of the last value We'll be six numbers left and since I'm looping through the halfway point. I just take six divided by two It'll give me three so that'll go loop from zero to three and I'll get through like half of these numbers Right and pair them up So that's why I loop from i equals zero to n minus one over two, okay? That's what this value n minus one over two is okay, right? That's what that's for okay, and then it's the same thing Same thing. We're just going to add the first and last term and Alternate them into the two sets right so here The first term is going to be so when I is even we're alternating them right we're alternating them to the two sets so What we're going to do is the first term i is going to be i plus one So that's the same thing as the first term as the value so here our indexes are zero one two three four five six right and we're looping up to three right we're looping up to three All right, so the first term that we're going to add is going to be our index plus one that we're looping to Right because index is zero with plus one we get one right so index So our first term We're starting at zero to the halfway point of three right and then we do zero plus one which gives me one So this is our first term so I pair that up Put that in the first set right so I have two sets here So this is one is gone now now to get the To get the last value of six to pair them up Remember I want to pair one and six and then I want to pair two and five and I want to pair three and four right so to get the last value of six in order to do this it's will be Value of seven minus one which is six and then minus zero which would be Six right so it would be yeah seven minus one Minus the current index that we got and I'll give you six right I'll give you six because I'm not including this last term so yeah So it will be n minus a current index of I a zero of I zero Which is seven minus Zero which is seven then minus one which is six which will give us six Okay, so that'll be that here Now I becomes one Right, and then to get this one here. I need to alternate Alternate it and add it to us the different set and to do that I'd check I just check if I is even then I put a one set if I is odd I put in another set Okay, so yeah, that's a way to alternate the two sets every time so here if I is equal to So now I is odd. So now I'm gonna put in the second set So since now I is equal to one now. I'm gonna do to get the value of two I just do one plus one which it gets to so that put two in the second set. So this is gone Then I need to get this last term not six. I need to get this last term of five So to that I just take seven minus One of the next one which is six and then minus one again to get five So seven minus one six minus one it will get five so I put five into the second term Five to this one. So then this is gone now and then I do two and three right two three So to get this one I need to do Index of two plus one will just give me three so I put that here and then I need to do seven minus two Which is give you five minus one which would give me four. Okay, so I put that here also so yeah, then all the values here are In the first set and then we have values in the second set also We have first and second set and then whichever is smaller. I just put this seven in the last Smaller value last set of seven those last term seven So in all of the two sums of the two sets are going to be the same, right? It'll be 14 for both of them so to do that Here I loop through from Yeah, so the first set the first term was just going to be I plus one and then the last term will be n minus I minus one which would give you the last term and Yeah, and then we alternate right so if I is odd For index is odd. We add it to the first set. Otherwise we had it to the second set. Okay, so we just alternate Then whichever sets that's smaller if f dot size is greater than s dot size Whichever smaller we gonna add the last value of n our last term so s dot push back n because as This s size is smaller and then otherwise do f dot push back of n And then in the end we just print out our two sets All right our two sets that we have so I just print out f dot size And then I just print out each of the values loop from zero to the f dot size and then just to print print F of I and then I just put a new line and print out the second set Print out s dot size from zero to SS size and print out s So yeah, that's the gist of the code. Hope you guys enjoy this video I hope you guys understand what I'm doing ray com subscribe. I'll check you guys later. Peace