 What's up guys my name is Michael and welcome to my YouTube channel today We're gonna do another lead code challenge is called hamming distance hamming distance between two integers Is the number of positions at which the corresponding bits are different so given two integers x and y calculate the hamming distance So what does this mean by bits is that well if you convert the number one into its corresponding? binary 0 0 0 1 and Convert for it is corresponding binary 0 1 0 0 The distance between the difference and bits as you can see here This distance is 1 and this distance which that is different Right 1 0. This is in the second this second bit that is different That's a distance from here is 2 right My bad. It's also 1 so in total. It's 2 see Distance between the here and here to the different bits is 2 so I'm gonna give you guys a few minutes to do it and then Come back here when you can't figure it out after one to two hours All right guys, so I'm gonna explain how to do it now. Well, what I did was I Used to to pass means I went over it twice in order to count difference in bits But you could do however the way you like it as long as you are able to count the how many Corresponding bits are different. So first what I did was I created a bit set and Oh wait, hold up. Let me just see. This is how you create a bit set, right? Let me just let's just show it to you guys All right a bit set is basically just a an array But it tells you the number It's only filled with bits. This is more efficient than having an array of integers because then For an integer it would have more size if you understand what I'm saying So if I just use a bit set Bit sets only have zeros and ones so it makes storing the binary of each integer much easier So what I did was whoops, okay, that was my solution, but Here so first of all we have to see that our maximum possible Number is 2 to the 31. So I filled my bit set with a length of 32 and this is going to be my x in binary And I create another bit set 32 y in binary Reason is because our maximum is 31. So 2 to the 31 So I created the bits of 32 because you know just to make sure that Nothing is too Over the top because I have to include the zero bit also, you know Okay, so what do I do? So I'm gonna loop from I equals to zero I'm gonna go 32 bits. Okay Go all 32 bits and I'm gonna fill up my bit set So how do I fill up my bit set? I do x binary at I and what am I gonna do? I need to get the Corresponding bit All right That I'm currently at so this is a way how do you to convert a number into its binary equivalent Into a bit set. So what do I have to do? Well, I need to shift it left by I Okay, this is going to shift the value by Whatever Number it is right So here it's gonna go zero one two three four five six seven eight. That's 11 12 30 40 up to a 31 Last time we're gonna be 31 and I'm going to shift it that many times In order to get Each place for each bit, right? So in this case, let's say I had one and this was the binary Well, if I want to fill up Each value here. I have to shift this By the certain number of places bit shifting so by should be it shifting. Essentially, this is equivalent to Multiplying by two I think or dividing by two I let me see bit shifting Equivalent I'll just show you what this does just to make sure that you know what you're doing So if you were to let's see, where's multiply multiply multiply No shift this way logical shift right here. So in terms of logical shifting it is the equivalent of If you shift left by one You move the bits into its certain location, which is essentially the same effect Shifting left by n bits on a number has effect of multiplying it by two to the end Okay, so if I were to shift it the Shifting a left by whatever number of bits for each one, I will basically get whatever the Certain number bits I'm at and then if I end it by one That will basically get me the least significant bit. Do you guys understand? so in this case If I want this number to be I want the zero to store and the zero Index right if I shift it one two three four five four times I will be able to get whatever the zero is and if you keep shifting it How many number of times you would be able to get? These values for each Each bit in order to convert it to its equivalent binary Okay, so this is this is the code how you would do it adding it by one basically take that tells me the last significant bit all right, so it's essentially just trying to find this one and then Once it shifts it, then I won't have this anymore like this would be gone One would be gone, then I shift again. Then we'll have the zero and zero zero. This is the equivalent of Converting this number into its equivalent binary essentially, okay, so that's a basic gist of it You could put it this into your compiler and then see what it does. That's that's another way to go how to learn this Okay And you could print out Which whatever value it is and it would tell you okay? So now once I have all my numbers converted to a binary into my equivalent bit form. I'm going to go from The last bits 31. I Is greater than equal to zero I minus minus. What am I going to do? I am going to count Create a count Equal zero Okay, and what am I going to do? I am going to check if x binary at I is not equal to y binary at I I am going to increase my counter because I want to check the corresponding bits are different Count the corresponding bits are different. So this is how you would do it. I go through every bit I check if it's not equal and I increase my counter Once that's done. I am going to return my count So, yeah, let's run the code. Let's see if there's any compile errors. Oh Compiler, okay Cannot be restricted to an int. Hmm. Let's see By 30 places cannot be strict. So what did let's see Let's just see what my pre-solution did. Oh, I shifted right. Whoops I'm supposed to shift right my bad because okay. So if you shift right Shifting left means you get rid of the whatever the left most Okay, so basically five number a shift right Okay, okay. I'm going to explain this again Okay, so the number one It's equivalent binary zero zero zero one right if I shift right by I Which is like zero than one than two than three than four five up to 31 Basically, I am and I ended by one. I fill my array up my bit array With the corresponding value that it's at okay. So here if I shift it right Let's say I shift this right. So I would and I ended by one. I get the last significant bit So here I'm gonna and it by let's I shift it right and get ended by one So that's going to fill this array at this position with one then I shift it right then this one is going to be gone So my new array my new number of bits would be this and Once I ended by one, this is going to be gone again, and I fill that up in here then after the next pass You have zero zero I Shifted right again. I get this and it by one I get that the when you ended by one you get the least significant bit, right? So then after this I fill that here Shifted to get right again and Then I have zero then I fill that here and then once I shift it right again This is gone and that would fill your array up with these number of values So let me just explain it one more time just in case if you guys don't understand What shifting left and right does so let's say I have well that is really small. Okay, so I have this In the equivalent form in binary, which is how all these numbers are represented is this I want to fill this up in my new array Okay, every time you shift a number right by how many How many spaces of n you get rid of the last Significant bit alright, so if I shift it right by one and it gets rid of that should I do to I get rid of these two so once if I shift it right by one and And it by one so if I shift it Right by I which is zero or one and two and then and I buy one I get the least Snooker picking a bit So here I get the least significant bit would be one Then when once you shift it right every every bit gets shift right right so then this Come zero zero zero because this last one's gone when you shift it right We end up by one. I get this significant bit. I Sorry here, then I shift it right again This this this would become gone So then I have zero zero shift it right again and it by one get to my least significant bit zero shift it right This is gonna become zero Shift it right. Yeah shift right again, and if I one get this least significant bit This will become nothing and I have that my bit has my binary number. So that's basically what this is doing Once I do that I have a counter and then I check if the ones that are not equal I count plus plus and I return count Now it should work Yeah, submit it and It gets accepted. So yeah, this is how you would do this is my way of doing it I had to do is two for loops But there is an easier way to do this that I found another solution that this guy did online Okay, his way is basically he still has a counter. What he's gonna do is he's gonna while Numbers aren't nothing right. He's gonna and it by one to get its least significant bit and Why is one and it by one you least significant bit if they're not equal you count increases this counter Then he's gonna shift right by one and That would get rid of the values again And then if he once he keeps doing that he maintains a counter the X and Y bits for number bits will be gone And then he returns the counter bar This is much easier because then you're not filling the array of having a bit set filling it with your binary values This is basically just doing Having your counter while it's doing it. Okay, so this is much more efficient So I'm gonna type that solution with you guys just in case if you don't remember you have a counter this might represent the number of Bits that are different while X and Y Are still having that like Still not equal to zero while it's still have some number bits What we're gonna do is we are going to check if X and Y this gets my leasing if can bit is not equal to Y and one Then we're gonna increase our counter After that We're gonna shift shift X is gonna equal to X shift right by one Y is gonna equal to Y shift right by one Then after we return count run the code it passed this and It got accepted. So if you understand what I'm doing, I'm shifting right The last and even bit I ending it by when I get the last thing if you can bit and then I Increase my count if the bits are not the same the last and even bits are not the same Shifting it right by one essentially means gets rid of the bit that I was checking the last bit I was checking and then I have the next continue in the next bit. So yeah, that's how you do it Right calm subscribe. Hope you guys understood. I was doing