I think it would have been much better to use n&(n-1) to count set bits, because if we're doing this, this is no different than doing count += (start&(1
Well I used a bit of different approach and just compared the bits linearly class Solution { public: int minBitFlips(int start, int goal) { int a = start; int b = goal; int ct=0; while(a>0 || b>0){ if((a&1)!=(b&1)) ct++; a=a>>1; b=b>>1; } return ct; } }; not so creative but still works. Have a good day!!
std::string num2Bin(int n) { if (n == 0) return "0"; std::string res = ""; while (n > 0) { res += (n % 2 == 1) ? '1' : '0'; n = n / 2; } std::reverse(res.begin(), res.end()); return res; } class Solution { public: int minBitFlips(int start, int goal) { string x = num2Bin(start); string y = num2Bin(goal); while (x.length() < y.length()) x = '0' + x; while (y.length() < x.length()) y = '0' + y; int g = x.length(); int cnt = 0; for (int i = 0; i < g; i++) { if (x[i] != y[i]) { cnt++; } } return cnt; } };
class Solution { public: int numOfBits(int n) { int result = 0; for(int i = 0; i < 32; i++) { result += (n >> i) & 1; } return result; } int minBitFlips(int start, int goal) { int y = (start ^ goal); return numOfBits(y); } };
here is the python3 solution for this problem num1=int(input("Enter number : ")) num2=int(input("Enter Desired number : ")) diff=num1^num2 ans=0 while diff: diff=diff&(diff-1) ans+=1 print("You need",ans,"bit flips to convert",num1,"to",num2)
Easy pisy❤😅
Int num = (start xor goal)
Int cnt = 0;
while(num) {
num &= (num - 1) ;
cnt++;
}
return cnt;
int differ = start^end;
return __builtin_popcount(differ);
; )
@@ashurajput6916😂😂 op
int bitmask=(1
Most anticipated one❤
I think it would have been much better to use n&(n-1) to count set bits, because if we're doing this, this is no different than doing count += (start&(1
Code readibility😢
Yup
long awaited video finally ❤
keep it up bhaiya and thanku so much🙏🙏🙏🙏
The way striver blushes when calling *" 13 "*....
May it's our bhabhi favourite 😅
Understood
UNDERSTOOD;
int minBitFlips(int start, int goal) {
int count=0;
while(start!=goal){
if(start%2 != goal%2){
count++;
}
start=start>>1;
goal=goal>>1;
}
return count;
}
Understood++++ 😁
Thank you 😊
done and dusted😃❤
Well I used a bit of different approach and just compared the bits linearly
class Solution {
public:
int minBitFlips(int start, int goal) {
int a = start;
int b = goal;
int ct=0;
while(a>0 || b>0){
if((a&1)!=(b&1)) ct++;
a=a>>1;
b=b>>1;
}
return ct;
}
};
not so creative but still works. Have a good day!!
i also did the same bro
Same pinch! But why did u create new variables a and b?
@@shubhrajit2117 actually striver once said never mess with the original variables just a good practice of mine
@@shubhrajit2117 its good practice to not alter the data given as the test case
I also done same
Make more lectures 🙏🥺
Python one liner
return bin(start^goal)[2:].count('1')
Very helpful .
Thanks a lot Bhaiya
understood
Understood🤗🤗🤗🤗
thank you Bhaiya
understood ❤
Thank you
Understood!
great!
public int minimumBitFlip(int start,int goal){
int val=start^goal,cnt=0;
while(val>0){
val&=val-1;
cnt++;
}
return cnt;
std::string num2Bin(int n) {
if (n == 0)
return "0";
std::string res = "";
while (n > 0) {
res += (n % 2 == 1) ? '1' : '0';
n = n / 2;
}
std::reverse(res.begin(), res.end());
return res;
}
class Solution {
public:
int minBitFlips(int start, int goal) {
string x = num2Bin(start);
string y = num2Bin(goal);
while (x.length() < y.length())
x = '0' + x;
while (y.length() < x.length())
y = '0' + y;
int g = x.length();
int cnt = 0;
for (int i = 0; i < g; i++) {
if (x[i] != y[i]) {
cnt++;
}
}
return cnt;
}
};
class Solution {
public:
int numOfBits(int n)
{
int result = 0;
for(int i = 0; i < 32; i++)
{
result += (n >> i) & 1;
}
return result;
}
int minBitFlips(int start, int goal) {
int y = (start ^ goal);
return numOfBits(y);
}
};
God 🙏
One line solution would be
return __builtin_popcount(start^goal);
😅
here is the python3 solution for this problem
num1=int(input("Enter number : "))
num2=int(input("Enter Desired number : "))
diff=num1^num2
ans=0
while diff:
diff=diff&(diff-1)
ans+=1
print("You need",ans,"bit flips to convert",num1,"to",num2)
print(bin(start^goal)[2:].count('1'))
but if start is 1 0 10 xor with 0 1 1 1 then ans is 1101 as written by striver but 1 and 0 cant give 1 na after fliiping it should be 0 as per start
🙌
Can someone plz tell where to get this code
❤❤❤❤❤❤❤❤❤
UnderStood
Understood
understood
int minBitFlips(int start, int goal) {
int ans = start ^ goal;
int cnt = 0;
while(ans != 0){
ans = ans & (ans -1);
cnt = cnt +1;
}
return cnt;
}
understood
understood
understood