 What's up guys, my name is Michael and welcome to my YouTube channel today. We're gonna go over code forces round 649 Which is this is surprisingly because this tutorial this editorial actually came out today right after the contest was over So this is actually really cool. And also I could not solve this problem This I actually just Looked at that solution just now and submitted, but I could not couldn't actually solve this problem in the contest, but Now I look at it You probably can so I Understand how why the solution worked. So now let's look at this problem. So pretty much There's a guy who loves number theory, but he hates the number x So you're given the array a and you want to find the length of the longest sub array Such that the sum of its elements isn't divisible by x. So if the sub array doesn't exist, then we're gonna print out negative one So we want to find the longest sub array such that the sum of its elements isn't divisible by x So the longest sub array such that the sum of the elements isn't divisible by x Okay, so what do I mean by sub array a sub array means that you could delete sir Several numbers from the beginning or the end or none at all. So let's look at this array one two three This array one two three is the array one two three. So what are the sub arrays of a one two three Well, I only could remove elements before or after it, right? I can't just remove an element in the middle So a sub array of one two three would be two three right two three is a sub array of the whole array one two three and the The reason why of for this is because I removed the element one from it from the array one two three So sub array means that you could only remove elements from the beginning or the end So also one two is a sub array of one two three reason why it is a sub array of one two three is because Three we removed the element three from the end, right? So what array one two is going to be a sub array of the total array of one two three, okay? So now Now what's also a sub array one two three is a sub array of one two three, right? The whole array itself is also a sub array of one two three reason Why is that you could remove zero elements from the beginning and zero elements from the end and you still have the same? Array of itself. So that's what a sub array means. Okay. I cannot remove a middle element. I cannot remove two Right, that's not a sub array like I look one three is not a sub array of the array one two three Because I cannot remove elements in the middle I only can remove elements from the beginning or the end or not remove any elements at all okay, so that's what a sub array is and We need to find the length of the longest sub array Shit that's such that the sum of the elements isn't divisible by x. So what does that mean? That means that in This sub array one two three right in this array one two three if I get a sub array two three I want to find the longest Sum of all the elements in that sub array So two plus three five Five we need to find that sum The longest sub array such that the sum is not divisible by x so two plus three is five five is not divisible by Three right or x is three in this case. So that's why the longest Longest length of that sub array is two. So that's why I'll put it the value two. Okay, so that's what this means Now if we try another sub array one two right This the sum of one plus two is three, but three is divisible by three so that's that doesn't count as a the longest sub array of sums that is Isn't divisible by x right one plus two is three three is divisible by three. So that doesn't count Right. So our job is to find the length of the longest sub array such at such that the sum of its elements isn't divisible by X which is three So how do you do this problem? So let's just go over the solution of what they did and then I'll explain why they did it. Okay So first of all We use two pointers The two pointers is going to be L and R Okay, L is going to point to the beginning of the array and R is going to point till end of the array. So What do we do for first of all? We're going to go through first of all We leave we read in all the test cases because that's what we have to do and then we read in and the length of the array and and the x the value that we're trying to divide by Okay, now we first going to set the Left pointer that we're pointing at for our position in our array is going to point to negative one This is going to serve as a placeholder Okay, or a sentinel how they call it in CLRS, but I digress, okay? R is going to point at the last element the last position of the element in our array So that's n minus one Then we have a Each of the elements that we're going to read in a Right, so we're not actually going to store in any of values We're just going to read in every single element and then we have a sum called total sum So now I'm going to go through the number of elements that I'm going to read in and I'm going to read in each element Okay, so here's the tricky part about this question Let's say I have this Array one two three right and I want to find the sum which is Longest sub sub array, which is not divisible by three Rx get value three So we like we have to think about what happens if I remove values So let's find the total sum in the beginning. So one plus two is three three plus three is six Okay, so my total sum is six What if I were to remove the last value three? Okay, if I remove three my total sum is going to be six minus three and that's going to be three So three is still divisible by three So what does that mean? That means that if I remove an element that is still divisible That is divisible by my x value three. It's still it's not going to help me But it's still going to be divisible by three In our case like six minus three the element that we're moving Three is still divisible by three. So if I remove elements at the end where it's not that is that is divisible by three It's not gonna help me Let's talk about this think about this Here in this case One two three What if I remove the element one the beginning value of one for my array So what does that mean? So our total sum is six and I'm going to remove the value one from the array six minus one is five Five is not divisible by three So what does that tell us that tells us is that if I remove an element that is not divisible by x My sub array total sum is still not going to be divisible by x Right. So if sick like my total sum six, I removed the beginning of ray one Element one. So my sub array sum is now two two plus three is five Since one was not divisible by three in the beginning and I removed the one The rest of the array sums two three is not going to be divisible by three So that helps us a lot. So here's what we're going to do We are going to Read in every single each element of our array. We are going to check if that certain element The number that we're reading is not divisible by x Okay, so if it's not divisible by x We are going to get point The left most value that is not divisible by x L is going to point to that location that position in the array and Then R is going to point to the right most value that is not divisible by x Right, so in this in this in this this if statement This is checking if a is not divisible by x So if my each element is my current element that I just read in is not divisible by x my left pointer L is only is going is the left side is going to point to that if is the If my left side is the starting value. So the L is just a centinel. So this this doesn't matter, right? My L is going to point to it Okay, so L is going to point to the first element that is not divisible by x That's what this L is doing. Okay, that's what it is doing This is this this negative one is just sent in out. It doesn't matter This is just indicating that it's the first element. That's what this is indicating, right? Because we set our L value To point to negative one in the beginning. So this is just going to point to the first value This if statement is going to point to the first value that is not divisible by x. Okay That's what this if statement is doing. So in our case, this is going to point to the point to one Right the position of one the position of the element one, right? Because one is the first element that is not divisible by x of three. Okay so that's what this does now my Right pointer R is going to point to the Right most value that is not divisible by X so then in this case R is going to point to One as well Right R is going to point to one as well. Okay, then reread in the second value Now we're going to read in the next value, right the next value is going to be two So I read in two Right, I'm not going to change my left Pointer right my left pointer is only going to point to the first element that's not divisible by it So that's going to remain as it added as it is. Okay My R is going to point to the next one So my R is now going to point to the position that of the next value, which is two. Okay, so that's what it does So my in this kid after this iteration in this case L is going to point to the position of one R is going to point to the right most position of two Okay, so now now three three gets put in reads in three. So now it reads three So what does this do if I read in three? Three is divisible by X because three or X current value is three. Three is divisible by three So I'm not going to update anything So at this end of this for loop What is my L and R? My L and R is going to be L is going to point to one R is going to point to two. Okay L is going to point to the position position of one R is going to point to the position of two. Okay, so now During this whole time. We're keeping track of the total sum. Okay, so now what is this this? What is this if statement do we are going to check if the total sum is Not divisible by X If the total sum is not divisible by X that means that the whole array Whole array one two and three If I whole original ray the sum of all the elements of that total sum one plus two for three Three, so yeah, if it's not divisible by X That's just gonna be the size of the whole array, right? Right because That's gonna be the longest sub array Because no other sub array that's of that the original ray is going to be Less than it right the longest sub array is gonna be just the the whole array because all the sum of all the elements in that But in the original ray is not divisible by X So the longest sum is just going to be the length of the whole array Okay, so that's this that's what this case is testing for right if this is just testing if the whole original array It's some was not divisible by X and then we're just printing out the whole array the length of the whole array, which is n So that's where we print out Now here. What is this testing case if L is equal to negative one? Why do we test this? Why do we check this L is going to be the first value that is not divisible by X Right, we pointed to that to point to that right the first element is not visible by X We set L to point to that If my L was still pointing to negative one, what does that mean? That means that no element in the whole array Isn't divisible by X. So that means that all the elements in the array are divisible by X So if all the elements are divisible by X That means that no possible sub array could occur Because that like all the sums of all the elements are divisible by X no possible Subarray can occur. So that's why we print out negative one Because no possible sub array some of all the of any parts of the sub array is possible So that's where we have negative one Now here's a thing. Here's a tricky part This equation is a tricky tricky very tricky. Okay, so at this point We have two pointers one pointing to the first starting element that is not divisible by X R is going to point to the last element the furthest last element that is not divisible by X So what does that mean now? I want the longest sub array whose sum isn't divisible by X That means that I'm going to subtract by a minimum value Right because I'm going to subtract the total length of the array by some small value that I'm going to remove right From the left side, I'm going to remove a certain number of elements on the left side right side I'm going to remove a certain number elements on the right side, right? So if I minimize the the elements and we're moving from the left or the right That's going to be the longest sub array whose sum isn't divisible by X Okay So What am I doing here? Well, I got the left most Number that's not divisible by X Right the left most L is the left most element that is not divisible by X And I am going to add one to it Why because that's the number of elements From the left side that I'm removing that are not divisible by X Right if I locate, okay, let's let's look back at this Let's look at this case one two three And let's look at the the number X that I'm That that the second case let's say X is four Four is the number that we're checking that's not divisible by right So one two three What is the left most element that is? Not divisible by four That's one. Okay. So that's going to be one Okay Now what is the right most element that is not divisible by four? Three Okay, so my L is my L is pointing the position of one Three is pointing at position of our other r is going to point at position three So if I'm removing the elements on the left side And then I'm removing It's not divisible by one. So that's going to be L plus one Right because that's the number of elements on the left side that are not divisible by By four by X right L is pointing to the first position of the the first position of the the um Element that is not divisible by X which is four right So the first position that's not divisible by is one right one So one plus one Uh, no no my bad. Um Yeah, so the first position that we're pointing at is at location zero So remember this is this is index from zero one this is index from starting from uh index from zero, right So one's position is at position zero One's position as that is at position zero And three's position is at position two Right because we're indexing from starting from zero. So that's zero one two. So three is at position two One is at position zero Okay, so if I'm removing the leftmost side Of the number of elements that I'm moving to the left side That's going to be L plus one right because I'm indexing at zero Okay, so that's that's the number of elements I'm moving from the left side. So that's why we have L plus one L plus one, okay now L plus one is So the my starting my L value is at position one and that's Is that location The index of the position the index of the element one Which is zero, right? So zero plus one is one So the number of elements we're removing from the left side is going to be one, okay So now we'll about this part and minus r. So r is pointing to The position of the rightmost which is three Right and that's two the position of This element is zero one two and then The total number of elements n minus Zero one two. So three minus two is going to be one So I'm removing one element on the right side Right one element from the right side three minus zero one two is one element from the right side And that is the That's the number of elements I'm removing from the right side So now if I do a minimum of that that's the minimum number of elements I'm removing from the left side or the right side Right That's going to be the minimum element I'm removing and then if I take my total number of elements Subtract by the minimum number of elements I'm removing from the left side or the right side Then I get The longest subarray So yeah, that's that's what I'm doing and that's That's basically how you solve this problem You take and then you take n minus total of these minimum number of elements you're removing from the left and the right and then that's it Yeah, so that's how you do this problem. I hope I explain the solution well to you guys Ray com subscribe. I'll check you guys later. Peace