L3. Minimum Bit Flips to Convert Number | Bit Manipulation

Поделиться
HTML-код
  • Опубликовано: 27 окт 2024

Комментарии • 55

  • @faizanalam8823
    @faizanalam8823 7 месяцев назад +41

    Easy pisy❤😅
    Int num = (start xor goal)
    Int cnt = 0;
    while(num) {
    num &= (num - 1) ;
    cnt++;
    }
    return cnt;

    • @ashurajput6916
      @ashurajput6916 2 месяца назад +5

      int differ = start^end;
      return __builtin_popcount(differ);
      ; )

    • @ThePROestRedn99
      @ThePROestRedn99 Месяц назад +2

      ​@@ashurajput6916😂😂 op

  • @Shantisingh-tz5sr
    @Shantisingh-tz5sr 6 месяцев назад +3

    int bitmask=(1

  • @aswin6261
    @aswin6261 7 месяцев назад +6

    Most anticipated one❤

  • @rahulsangvikar7973
    @rahulsangvikar7973 7 месяцев назад +16

    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

  • @i8you
    @i8you 7 месяцев назад +2

    long awaited video finally ❤

  • @CodewithAnuragBassu
    @CodewithAnuragBassu 7 месяцев назад +3

    keep it up bhaiya and thanku so much🙏🙏🙏🙏

  • @ANKAMJANAKIRAM
    @ANKAMJANAKIRAM Месяц назад +1

    The way striver blushes when calling *" 13 "*....
    May it's our bhabhi favourite 😅

  • @adityapandey23
    @adityapandey23 Месяц назад +1

    Understood

  • @DeadPoolx1712
    @DeadPoolx1712 Месяц назад +1

    UNDERSTOOD;

  • @diyatoliya9067
    @diyatoliya9067 4 месяца назад +1

    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;
    }

  • @pratyushtripathi1728
    @pratyushtripathi1728 15 дней назад +1

    Understood++++ 😁

  • @UECAshutoshKumar
    @UECAshutoshKumar 7 месяцев назад +1

    Thank you 😊

  • @shahidullahmuffakir668
    @shahidullahmuffakir668 4 месяца назад +1

    done and dusted😃❤

  • @rohitchakraborty4619
    @rohitchakraborty4619 5 месяцев назад +7

    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!!

    • @HaamroNotes
      @HaamroNotes 5 месяцев назад

      i also did the same bro

    • @shubhrajit2117
      @shubhrajit2117 5 месяцев назад

      Same pinch! But why did u create new variables a and b?

    • @rohitchakraborty4619
      @rohitchakraborty4619 5 месяцев назад +1

      @@shubhrajit2117 actually striver once said never mess with the original variables just a good practice of mine

    • @HarshKumar-mx9nj
      @HarshKumar-mx9nj 4 месяца назад +1

      ​@@shubhrajit2117 its good practice to not alter the data given as the test case

    • @codesetter2497
      @codesetter2497 2 месяца назад

      I also done same

  • @riteshbisht94
    @riteshbisht94 6 месяцев назад +1

    Make more lectures 🙏🥺

  • @bthulasikrishna9597
    @bthulasikrishna9597 Месяц назад

    Python one liner
    return bin(start^goal)[2:].count('1')

  • @ishwarreddy8820
    @ishwarreddy8820 7 месяцев назад

    Very helpful .

  • @DeepakPatel-d5v
    @DeepakPatel-d5v 5 месяцев назад

    Thanks a lot Bhaiya

  • @navinchaudhary2812
    @navinchaudhary2812 7 месяцев назад +1

    understood

  • @SibiRanganathL
    @SibiRanganathL 2 месяца назад

    Understood🤗🤗🤗🤗

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 6 месяцев назад

    thank you Bhaiya

  • @khalasianiket816
    @khalasianiket816 3 месяца назад

    understood ❤

  • @hakunamatata-nl4js
    @hakunamatata-nl4js 4 месяца назад

    Thank you

  • @shashankgsharma0901
    @shashankgsharma0901 3 месяца назад

    Understood!

  • @shikher4559
    @shikher4559 2 месяца назад

    great!

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 7 месяцев назад

    public int minimumBitFlip(int start,int goal){
    int val=start^goal,cnt=0;
    while(val>0){
    val&=val-1;
    cnt++;
    }
    return cnt;

  • @sumitdon2004
    @sumitdon2004 2 месяца назад +1

    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;
    }
    };

  • @aviralgoel5709
    @aviralgoel5709 12 дней назад

    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);
    }
    };

  • @pratulyapratap9329
    @pratulyapratap9329 7 месяцев назад +1

    God 🙏

  • @vkgaming3316
    @vkgaming3316 3 месяца назад

    One line solution would be
    return __builtin_popcount(start^goal);
    😅

  • @vishnupandey3390
    @vishnupandey3390 2 месяца назад

    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)

  • @manyachandra3566
    @manyachandra3566 Месяц назад

    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

  • @tamannaverma4178
    @tamannaverma4178 7 месяцев назад

    🙌

  • @eshuthakur2982
    @eshuthakur2982 4 месяца назад

    Can someone plz tell where to get this code

  • @adityaIETian
    @adityaIETian 7 месяцев назад

    ❤❤❤❤❤❤❤❤❤

  • @anshsaxena7297
    @anshsaxena7297 Месяц назад

    UnderStood

  • @animeshkumar2683
    @animeshkumar2683 2 месяца назад

    Understood

  • @shaiksoofi3741
    @shaiksoofi3741 4 месяца назад

    understood

  • @saurabhdwivedi2831
    @saurabhdwivedi2831 7 месяцев назад +4

    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;
    }

  • @hardikpatel352
    @hardikpatel352 4 месяца назад

    understood

  • @pranavmisra5870
    @pranavmisra5870 6 месяцев назад

    understood

  • @chiragbansod8252
    @chiragbansod8252 7 месяцев назад

    understood