Datta provides a masterfully clear reduction of swap constraints to graph connectivity, making a complex problem feel intuitive. It is an essential lesson in using structural insights to simplify algorithmic complexity.
Inmersión profunda
Prerrequisito
- No hay datos disponibles.
Próximos pasos
- No hay datos disponibles.
Inmersión profunda
Leetcode 1722. Minimize Hamming Distance After Swap Operations | Daily ChallengeAñadido:
Hello everyone. Welcome back to the channel. I hope you all are doing pretty fine and we will do today's daily challenge on lead code and we will try to solve it together. Okay, so let's see the problem statement and try to understand it in detail.
So minimize Hamming distance after swap operations. You are given two integer arrays source and target both of length n. You are also given an array allowed swaps. Okay, where each allowed swaps tells you the index number that you can swap uh at the source array. Okay, so you you are given two arrays source and target and you are allowed to make some swaps between the source target index and between which two indices you will make a swap that is given in the allowed swaps. Okay.
Uh so it is a vector of pairs.
So note you can swap elements at a specific pair of indices multiple times in any order. Hamming distance of two arrays of same length source and target is the number of position at which the elements are different. Okay. So number of indices I where source I is not equal to target I. So your objective should be to make this source array as equal as possible with this target array. Okay, by performing these allowed swaps. So this is your this is your whole motive for this problem. So and you want to see that minimize this Hamming distance. So if you make as many as elements of source array and target array equal, then the number of such positions where it is not equal should be minimum, right? And that will be only the minimum Hamming distance of the source and target after performing any amount of swap operation. Okay. So yeah, I hope at least the problem statement is very very much clear to you. And if you see uh this is the source array 1 2 3 4 target array is 2 1 4 5 and you are allowed to make a swap between index 0 and 1. So if you do it do that, you a 1 and 2 will become 2 and 1. You see that will match with the target array. Okay.
And uh when you do a swap between 2 and 3, that will make that uh 3 4 to be 4 3 and 4 and 4 will match. 4 at this position also will become the 4 at this position.
Only the 3 and 5 will not match and that that's why at one position we have an unmatching element. That is why uh we have the output to be one. Okay, I hope you do understand the problem statement very clearly as well as this example and there is no uh doubt in the question now at least.
Okay.
So uh what you need to do basically is you have this source array.
Okay, you want to make as many corresponding elements of this source array to be equal to this target array. Okay.
So how you will do it? So let's say you if you have one number one over here.
Okay. And another number for target is one over here. Now you can make changes in the source array. By changes I mean that you can make swaps in the source.
Okay. So let's say you swap this element and some element here. You swap between them and one becomes here. One comes here. Then again you swap between this and this and one will come here.
Okay.
Uh then again you make a swap and you see that one will come over here and uh it is the corresponding position of the target array also where one is present.
So you are successfully able to bring this one all the way to this by a series of swaps.
Okay. So uh if I ask you the question if I ask you a simple question that uh can you bring this one? So can you bring the source position one source array source array one this in this particular example if can you bring this one to the corresponding position here? Can you swap and bring this one or is it not possible or to swap and bring at all? So can you bring the source array one to the corresponding target array position?
Corresponding target array position. So this is my question which I will try to answer. Okay.
So the question the answer to this will lie in the allowed swaps array. Which which elements we are allowed to make a swap.
Okay. So let's say in the allowed swaps there is a pair 0 {comma} 2.
Then there is again a pair 2 {comma} 4. Then there is again a pair 4 {comma} 5. What does this mean? So let's say uh there is an element at position one of this uh so let's say mm the source of zero. These are indices, right? These are the nothing but index numbers. So source of zero is nothing but this element one.
This example only we are dealing with.
Okay.
And let's say this is six.
This is index number six and this is index number zero. So uh not six. Let's say this is five.
So uh if you have a one over here at the source of zero, we can swap this position one zero with position two. So it mean it means that one can go with this. So if you have a zero and one you can swap a one which is present over here to this. Okay. And then you see two can also be swapped with four. Okay.
So then again now one is present at the index two. So after swapping after swapping source of zero was one, now source of two is one.
Now when you swap two and four, source of four will become one.
Okay.
Source of four will become one.
Okay. So you are able to bring this one all the way till four and if four and five can also be swapped, then we see that we can bring the one all the way till five. So we can see that this get an idea of a graph over here.
This allowed swaps is nothing but the edges of the graph. And if a one is present at two or at four or at zero, it can be brought to five.
Or if it is a present at four, it can be brought at zero.
Can be brought at two. It can be brought at five. So you want to see that if So answer to what?
Answer to this question. Can you bring the source array to the corresponding target array to the target position? Okay.
If the one lies in the same component of the graph in the same component of the graph formed by allowed swaps array. Okay.
Formed by uh graph which is formed by this uh allowed swaps. Whatever that array is. If you form a graph graph with that and if one lies in the same connected component of the graph as the source and target positions source and target positions, then they can be definitely brought. Okay. So I might mean to say that if this zero which is in the source target source position.
Okay.
This zero which is in the source position is also and the five which is in the target position. This zero and five both of them belong to the same component in the graph then definitely they can uh the any anything can be moved within any pair. Okay. And let's say your graph looks complicated something like this.
So let's say zero. Let's say two. Let's say some seven comes over here.
Can be a four over here. Can be a five over here. Can be a eight over here. So eight can also be brought to uh zero. Seven can also be brought to eight. Seven you swap with two, then you swap with four, then you swap with eight. So an an element present at the position seven. Let's say that element is 100. So that 100 can be brought all the way back to any position in this component of the graph. So I hope that is very clear. So uh what you need to check what you need to check is that another way of saying it is that uh in a in a connected component of graph, okay, in a connected component of graph, you need to check that out of these indices, how many So they they belong to a particular group. So let's say I will call them group one.
Or I will call them these zeros group.
Okay. I will call them a zeros group or I will name the group anyway. Okay.
So in a particular group, I will check that see and now in this particular group, which elements are present? 0 2 4 5 7 8.
So in this particular group number one or zeros group or whatever, this all of these elements are present. Okay.
Now these these elements are nothing but the index index numbers. Okay. Now we know that within these index number, we can swap anything with anywhere. Okay.
Now I want to check that how many uh numbers which are present in these indices are also present in the source and target both of them, okay? So, let's say at position zero we have in the source array we have a five 50, let's say. At position zero we have a 50 which is present. At position five, let's say in the target array we have a 50. So, that five and zero can be brought to each other, okay? So, this is all I want to check that in a particular group in a particular group uh how many equal elements are there within the source and the target in a particular group? If those elements are there, they can be brought to the same index, no matter what. And some elements which are not there uh some things some elements which are not matching. So, let's say you have a 49 in a source added, okay? Let's say in the source of four okay? You have a 49 present.
And that 49 is not present anywhere in the target array. So, that 49 will only contribute to the Hamming distance, okay? Because they cannot match match with each other. Or let's say this 49 is present in some different component. So, let's say it is present in three, which is three four, let's say another component is there of the same graph.
Uh not four.
Three six. Because four is over here.
So three and six, if they are in a different component, they cannot be brought into this component as they're not connected by any edges. Okay, guys?
So, I hope you the approach is very much clear to you now and you can also try to solve this on your own first.
Yeah. Um this can be solved with definitely in a better manner with DSU, but yeah, I do not want to cover the background of DSU right now. So, I will try to go with uh simple solution of normal graph. So, I would like to change these variable names as these are very long.
Right? So, I will call them just source target.
And allowed swaps, I will call them AS, okay?
So, this will be nothing but S.size.
Uh This is nothing S.size is nothing but the number of components in the graph.
Not not the components, number of nodes in the graph.
And we will try to make the graph, which is nothing but an adjacency list. I hope you all at least know how to make a graph. And that much I'm assuming, okay?
So, while this adjacency list, it shouldn't be not at all difficult to make this graph, okay?
So you can just cover through all the edges. So, these are the edges of the graph. So, you just go through all the edges.
So uh int i equals to zero, i less than edges.size, i plus plus, and adjacency list of source to uh see these are the pairs which are there in AS, okay?
So, AS of i zero is one element uh So, this push back of uh AS i one, okay? So, I hope you do understand what I mean to do.
Uh the u and v I'm just pushing them.
So for better convenience you could have also made this as the first edge So, u {comma} v, let u {comma} v be the edge of the graph.
And you just want to connect u with v and v with u. That's all you need to do.
So, yeah.
And do it together.
Uh it's nothing but v. And this whole thing will be u.
Okay?
So, yeah. That is all That is all.
Uh the graph is just made. And definitely you need a visited array when you are traversing through a particular uh graph you need to see that which elements you have already visited or not. I hope these things are pretty standard. And uh as I told you you need to see which group, okay? So, this like say this is group number one.
And you see that in group number one these elements are present. So, I definitely I will not use some different marker. I will pick the first edge uh as the name of the group. This is zero group, I will tell, okay?
So this is the group number uh So uh what I'm making is basically a map.
So, each element will map that which group I'm part of. So uh zero will say that I'm belonging to zero's group. Two will say that I'm in zero's group. Four will say that I'm in zero's group. So, if this group is zero every element will map to zero, okay?
So, so group number I will get very easily from each and every element. That is why I'm making this group mapping.
Group mapping element to group number.
Okay? This is the map which I'm making in this.
So, initially I will say that each element is mapping to itself only.
Or anything you might do, right? You can say that this is mapping to all are mapping to zero or all are mapping to one, whatever whatever you can do. Initially I will do that uh every group is mapping to itself only, so this thing I will just do it. And uh I will perform a DFS traversal with uh the knowledge of component.
So, yeah.
I will iterate through all the nodes.
I'll see that if this node is not visited.
Okay?
So, if this node is not visited uh then I will call a DFS.
Now, in this DFS function only I will assign this group numbers and everything.
So, I need this starting vertex of my traversal. This is my starting vertex of the traversal.
Uh I have the adjacency list, that is the graph. I need the visited array. I need this group array because I need to change uh Another thing which I need is the group number. When I'm calling this, I'm telling that starting vertex is i. So, I will let's say pass this again. See, starting vertex will change again and again due to this recursive call.
So, uh I need to pass an i once again because the starting vertex will only be the group number. So, I will say that this is the i's group, okay?
So, last index I give i because this is the i's group.
And I just say that that element is my group name, okay? This is my group name.
So, I will while assigning the group name I will be using that.
So, let's look into the DFS function.
So we have uh void DFS and this is our starting vertex, okay?
And we have this vector of vector as the adjacency list.
Okay?
And then we have visited also, so vector of int uh we'll pass everything by reference.
And vector of group also we have. We even want to mark the group numbers, okay?
And let's say this let's call this as a top element, okay?
Group number or whatever. Let's call this as the top element, okay?
And uh what we want to do initially is that mark that the group of this starting vertex is nothing but uh this top element. This is what I want to mark. And visited of this starting vertex is also one uh which I must mark, okay? That I have visited this particular cell. And now I want to do is that I want to recursively uh traverse through all the connected components, okay?
So, this particular What are the adjacent vertices of this particular starting vertex? I first need to see.
So uh adjacent of this starting vertex.size. So, this is These are all the neighbors of the starting vertex, you can tell, okay? That particular node uh these are all the neighbors of them.
So, uh I want to iterate through the neighbors, okay? And I want to call them recursively.
So, let's say um neighbor is what? Adjacent to uh this particular starting vertex and i. So, this is my i'th neighbor, okay?
And if my i'th neighbor is not visited okay?
I will uh call a DFS function again. So, this DFS function is just to group all the elements to assign every element to its particular group uh so that we can easily access that whether these two components are belonging to the same group or not. So, this thing we can easily do it. So, uh this is nothing but neighbor and adjacency list visited and in this group we have also we have this top element. So, yeah.
That is all with the DFS function. Now, we once I have this group naming, I want to see that for the this zero, group number zero, these many elements are there. So, I can try to use a map for that. So, map the key value of the map will be zero.
I will tell that this is the group and these are the elements which are in the group, okay? So, zero contains zero, two, four, five, seven, eight.
Okay?
And if this is another graph, three, six, so we can mark that also.
So, we can call this three's group. So, three's group consists of three and six.
Something like this, okay? So, we need a map.
We need a map.
Integer {comma} vector of integer.
Okay, I hope you are not getting confused.
So, I need this particular map to see this, okay?
So, uh in order to do that, what should I use? Definitely, I will use an unordered map and not a normal map because unordered map generally gives a better time complexity.
It has an amortized of one. So, uh we have this map where we will store this integer {comma} vector of integer.
And how can I just group all the elements? So, we'll just iterate through all the index, okay? And I will just say that map of this group of I. So, whichever group number this I is belonging to, you just push back that in that particular group, you just push back I. Okay?
So, whichever group number, so let's say this I is belonging to group number zero, in corresponding to the zero, you push me, you push myself, okay? Push this particular I. That is what uh is happening here and yeah.
Once we Now, we are See, we are also having this map ready. Now, it is very easy. Now, everything becomes very easy.
I can just check uh what all is there with the source and what all is there in the destination, okay? Source and target. And whichever element is present in the source and not in the target, I can count that element towards my uh Hamming distance, okay?
So, So, now what I will do, traverse in each group, okay?
And we have the groups already, right?
So, we can simply traverse in each group and check how many elements are common, right? Are common in each uh connected components.
Traverse in each group and check how many elements are common to both this source and target. I hope that you understand. Uh I need connected components.
Rest of the non-common element form the rest of the non uh common element form the Hamming distance result.
Hamming distance result, okay? So, yeah, that is it. That is what I wanted to say. Uh this is an important thing, so I just wrote it down. This is the Hamming distance answer which we will return it after updating. And now, we want to iterate through this map, okay? So, what we will do is iterate through this map.
So, auto I on this map, okay? So, I Because I have group-wise iteration I need to do, okay?
So, uh I am extracting the second element of the map, okay? So, positions at which these elements are present. So, second element of the vector, as you know, is a vector of the map is a vector, right? And this is the group number, so in this group number, these many elements are there, these many positions of the elements are there because they are index numbers, right? So, I can do I.second, okay?
So, if you know, map stores as a pair of key {comma} values, okay? So, this is nothing but a value. So, uh we have this and we will keep another unordered map check, okay?
Now, why Why exactly is this map is that I will check in this map that uh this particular element which is present in the source and the target, okay? How many times it occurred? Now, there can be multiple elements also, right? So, let's say a two occurs.
So, in some positions, see See, the positions we are having, we are iterating according to the groups. That is all fine. But in the source array, let's say there are two In in in actual array I'm telling, okay? In the actual source array, in some positions, there are two twos and in other positions, there are let's say three twos. And by grouping and all, you see that they all belong to the same group somehow. So, they can be swapped with each other, okay? So, you can bring two of the twos into this. And the And the remaining two will still be contributing to the Hamming distance, So, this thing we need to take care of this.
That is why this check becomes important. I need to check that how many times this two occurs in this in this source array. So, this check value uh will store check map will store that two occurs two times here and two occurs three times here.
So, that is what, right? So, if it if it was three times here, you you will store three over here.
And yeah, that I hope you can understand and we will Whenever See, we got this two and we will try to decrement this value of two because one time we have used and then again, we will decrement and we will see that if it is zero or not. If it is zero, it means that we have all matched or it is not present, so uh that is basically the use of check. So, I hope that you do get it.
So, this check thing we try to utilize, so J value will be nothing but pos.size and J plus plus. We are iterating now to these elements. So, for a particular group, we are now iterating through the these elements, okay?
And uh I just store all of them in this check map. So, source of position of J, I will just increment the count, plus plus, okay? Uh means that I just checked whether the source array has this particular and I just incremented the count for this.
While checking the target, I will decrement the count, okay? Same thing, I will same elements I will go through, iterate through for target also.
And uh I will just check that if pos this check of this position of J is greater than zero, okay?
So, not source, this will be target. So, now I'm entering to the target, so I will again go to the position A and just check that if it this thing. So, what does this check store? That this element already occurs in the source array. Now, for the target array, I'm seeing that is this element actually occurring a finite number of times in the source array? If it occurs, I want to use it up. I want to swap it up, okay? I want to swap it to my current position and target of pos J minus minus means that I just now swapped it, so uh the decrement the count by one. And otherwise, answer plus plus. What does this mean? So, if if else if it goes in the else, it means that this check of target of pos J is zero, right? If any if it is zero, it means that either you have used all of them or it is not present in the source only. So, something which is not present in the source, but is present in the target, it means that that is that element is not becoming equal and that is contributing to the Hamming distance and that is why we increment an answer plus plus in this case and return answer. So, that is all you see. That is all for this particular problem.
If you submit this, I hope this will work fine. So, yeah. Uh Uh I changed the source to S and T, but used this source source everywhere, so yeah, let me change that back again and uh source, target.
So, in order to make it small only I did, but I used this source and target everywhere, so that is why.
Let's see if any more bugs are there or not. Yeah, we get the accepted thing. If you try to submit it now and see, I hope it will work just fine.
So, yeah, it does work fine. It gives you an accepted value. Uh this is not a very good run time complexity, but yeah.
Uh I guess you do understand the method.
It is optimal only in one DFS path only.
You are just uh dividing all the graph nodes into groups, okay? And for each node, you are making it remember which group you belong to. With that, you created the uh map which tells you that this group has these these these elements and you iterate through those elements and do uh store that that value is present in the source, okay? And if it is present in the target and you just try to decrement that and if it is not present you just increment the Hamming distance answer.
So, that is all that is all we do. If you know uh disjoint set union DSU, you can definitely try this problem. You don't have to do anything of these at all.
Okay, that needs a little bit of background. That is why I did not show that, but it will the code will become much much shorter than this. You do not need to even create the graph.
Uh What you need to do is that you have the edges, you iterate through the edges, and you just union keep union the edges, and your group number will be your ultimate parent, okay? So, you can just do find of I. That will be your ultimate parent and push back of I.
Directly this step you can write.
So, while iterating through this as.size, we just do union of So, basically uh instead of all this, you just do union by find or union by rank, whatever you do, union of U and V.
Okay?
And uh directly you can jump to this step and write that something like this.
Map of find of I.
It means that find the ultimate parent of this I and dot push back this I. So, the ultimate parent only you can use as a marker for the group name. That is why uh the disjoint set union option becomes a very good thing here, and this thing will be this this loop will be the same as it is.
So, this whole DFS function also you don't need to write.
These things visited and all you do not need to write at all. So, yeah, that will become a very good alternative for that. So, you should definitely go with the DSU approach also. So, yeah, that is all for this video. I hope uh you are able to understand this solution at least. If you have any doubt, you can ask me in the comment section also. So, I will meet you in the next video.
That's all for this problem.
Uh yeah, that that is all.
Thank you.
Videos Relacionados
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











