L7. Single Number III | Bit Manipulation

Поділитися
Вставка
  • Опубліковано 3 лют 2025

КОМЕНТАРІ • 77

  • @amanverma5912
    @amanverma5912 10 місяців тому +64

    The previous way when you code at the end of the video was great. Please continue like that .

    • @movieskingdom1748
      @movieskingdom1748 8 місяців тому +5

      It's ok. He wants to make it language independent ig.

    • @cosmicthor7330
      @cosmicthor7330 7 місяців тому

      @@movieskingdom1748 and he might not get c++ viewers also ,bcz most of the time people figure out approach but might face difficulty in coding

    • @xdjqye
      @xdjqye 2 місяці тому +1

      @@movieskingdom1748 that's okay but there are no codes available for this playlist either. i wish we at least had the codes accessible to us.

  • @taqimustafa7665
    @taqimustafa7665 10 місяців тому +9

    Great work again Striver! I wouldnt have ever tried learning DSA if i had never ran onto your channel. It is a request that you please make the string playlist next whenever you have free time(ik its hard to find time alongside doing a fulltime job,but you still do so much for us so thanks a lot).

  • @Lucifer-spam
    @Lucifer-spam 11 місяців тому +144

    There is a small mistake @19:04
    rightmost = (xor & (xor - 1)) ^ xor is the correct formula.
    Very great explanation! 🔥🙌

    • @shubhrajit2117
      @shubhrajit2117 8 місяців тому +5

      Alternatively u can use xor & -xor

    • @AkOp-bf9vm
      @AkOp-bf9vm 8 місяців тому

      @@shubhrajit2117 after using this formula did we still need to take XOR as long datatype?? or it will work in int

    • @siddheshpandey7905
      @siddheshpandey7905 7 місяців тому +1

      @@AkOp-bf9vm Yes, you need to use long because when xor is -2^31 then -xor=2^31 which is more than INT_MAX

    • @yutubegaming5340
      @yutubegaming5340 5 місяців тому

      Same I also thinking that

    • @aryansinha1818
      @aryansinha1818 2 місяці тому

      This should get pinned, thanks a tonne man.

  • @conor8687
    @conor8687 8 місяців тому +9

    The clearest explanation I've ever seen for 260 in bit manipulation. Thank you!

  • @Mojijansari0786
    @Mojijansari0786 5 місяців тому +10

    at 13:00 by first bit he meant bit at index 1 from back (If any one was confused here)

  • @shubhrajit2117
    @shubhrajit2117 8 місяців тому +12

    1:20: using hash map (brute force)
    6:05: bitwise approach (optimised)
    18:05: code for optimised approach

  • @playwithlinux
    @playwithlinux 7 місяців тому +1

    So nice of you Striver... THANK you so much for this type of content... Even words wouldn't be enough for that. Thanks a million.

  • @SuvradipDasPhotographyOfficial
    @SuvradipDasPhotographyOfficial 7 місяців тому

    The best explanation ever. The previous one's last solution blew me away... Amazing Striver, done with Bit manipulation today. Will start greedy from Monday. Hope you sgtart the string and stacks queues playlist soon.

  • @amitdahiya7425
    @amitdahiya7425 11 місяців тому +3

    C++ code with comments for better understanding
    class Solution {
    public:
    vector singleNumber(vector& nums) {
    // brute force using map
    // optimised using bit manipulation bucket
    // approach : we know that all duos xor will be 0 and the unique 2 elements xor will contain
    // nothing but a number which contain bits which are not same in both
    long long numXor=0;
    for(auto it:nums)numXor^=it;
    // now we need to distinguish both the numbers
    // and xor contains all the bits which are not same in both , we just need one different bit
    // taking the rightmost one is fine
    long long rightBit= numXor ^ (numXor &(numXor-1));
    // now take 2 bucket integer which store numbers based on this rightBit
    int a=0,b=0;
    for(auto it:nums){
    // if right bit is set
    if(it&rightBit)a^=it;
    else b^=it;
    }
    return {a,b};
    }
    };

  • @HarchitGulati
    @HarchitGulati 8 місяців тому +2

    finally felt like old videos of striver are back

  • @SURENDRANATHJAMBUGARI
    @SURENDRANATHJAMBUGARI 4 місяці тому +1

    Thank you bro for clearing the bits manipulation
    concept

  • @AS-gf3ci
    @AS-gf3ci 10 місяців тому +2

    vector singleNumber(vector& nums) {

    long xorr = 0; //Taking xorr as long for the case of INT_MIN in nums[i]
    for(auto &it : nums)
    xorr ^= it;

    int rightMostBitTurnedOne = xorr & (-xorr);
    int b1 = 0, b2 = 0; //b1 --> 1st bit is set || b2 --> 1st bit is 0
    for(auto &it : nums) {
    if(it & rightMostBitTurnedOne)
    b1 ^= it;

    else
    b2 ^= it;
    }
    return {b1, b2};
    }

    • @yaswanthmitta8983
      @yaswanthmitta8983 8 місяців тому

      but in the video he said to we need to xor it with intial value to get right most set bit

    • @bhushanthombare1540
      @bhushanthombare1540 7 місяців тому

      @@yaswanthmitta8983 yeah we have to take as rightMostBitTurnedOne=( (rightMostBitTurnedOne & rightMostBitTurnedOne-1)^rightMostBitTurnedOne )

  • @worthlessguy1621
    @worthlessguy1621 2 дні тому

    great video as always sir

  • @stith_pragya
    @stith_pragya 9 місяців тому +1

    Understood........Thank You So Much for this wonderful video.............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @ashmitgupta8039
    @ashmitgupta8039 8 місяців тому +1

    Legendary Explanation!!

  • @swagcoder
    @swagcoder 10 місяців тому

    Extremely helpful video. 5:54 just a small correction, there are two repeating numbers, hence m = n/2+2

  • @sakthivelR-hb9gr
    @sakthivelR-hb9gr 9 місяців тому +6

    There is an mistake @19:04
    it's not
    rightMost = (xor & (xor - 1)) & (xor); => rightMost = (xor & (xor - 1)) ^ (xor);
    or
    rightMost = xor & -xor is efficent way

  • @harshvyas8815
    @harshvyas8815 8 місяців тому +3

    class Solution {
    public:
    vector singleNumber(vector& nums) {
    long num =0;
    for(int i=0;i

  • @Gg69696
    @Gg69696 6 місяців тому

    U R the king striver !!

  • @UECAshutoshKumar
    @UECAshutoshKumar 10 місяців тому +1

    Thank you 😊

  • @akshithyadav4676
    @akshithyadav4676 11 місяців тому +2

    Keep code at last as in your old videos ❤🎉

    • @ce003abhimanyu9
      @ce003abhimanyu9 10 місяців тому

      yes that was very ncessary for us please striver sir

  • @DeadPoolx1712
    @DeadPoolx1712 4 місяці тому +1

    UNDERSTOOD;

  • @vishnupandey3390
    @vishnupandey3390 5 місяців тому +1

    here is my python3 code for the given problem 👇
    nums=list(map(int,input("Enter the list : ").split(" ")))
    xor=0
    for ele in nums:
    xor=xor^ele
    bitmask=xor&(xor-1)
    bitmask=bitmask^xor
    num1=0
    num2=0
    for ele in nums:
    if bitmask&ele:
    num1=num1^ele
    else:
    num2=num1^ele
    print(num1,"and",num2,"are the unique numbers out there in the list.")

  • @momentcoder
    @momentcoder 15 днів тому

    Understood 😀

  • @piyushverma7280
    @piyushverma7280 3 місяці тому

    finding rightmost set bit will give you error so use this
    int rightmost = 1;
    while ((xorr & rightmost) == 0) {
    rightmost

  • @hardikpatel352
    @hardikpatel352 7 місяців тому +2

    (xor & (xor - 1)) ^ xor correction in formula

  • @ManojkumarKatta
    @ManojkumarKatta 9 місяців тому

    mind blowing logic

  • @challasusmithachallasusmit9921
    @challasusmithachallasusmit9921 9 місяців тому

    UNDERSTOOD Thank you so much sir

  • @AyushKumar-h9u5t
    @AyushKumar-h9u5t Місяць тому +1

    Can we use bucket method here

  • @dipanshuraj7868
    @dipanshuraj7868 5 місяців тому +1

    Anyone is here that,
    it happens to me or others that the optimised approach is too tough using hashmap is simple, down and dusted.

  • @a.gcrazy555
    @a.gcrazy555 8 місяців тому +1

    How can we think off such solutions .
    There is only one method we have to learn this solution for this particular question ig.

  • @jjjyotijain
    @jjjyotijain 10 місяців тому

    understood
    🤩

  • @pardhi8959
    @pardhi8959 10 місяців тому

    understood.

  • @AanganwadiGuriya
    @AanganwadiGuriya 8 місяців тому

    for rightmost xor&(-xor) will do I think

  • @yunik_developer
    @yunik_developer 9 місяців тому

    19:06 there is a writing mistake the formula is rightmost= ((original-numb & (original-numb-1)^original-numb)

  • @shashankgsharma0901
    @shashankgsharma0901 6 місяців тому

    Understood!

  • @codingp110
    @codingp110 7 місяців тому

    Awesome!

  • @MeenakshiSharma-bd3bu
    @MeenakshiSharma-bd3bu 7 місяців тому +1

    what if we get 4,8 both as unique no. appearing ones only , than they will go to the same bucket ...4^8 in bucket 2

    • @someshchincholkar6501
      @someshchincholkar6501 6 місяців тому

      Then rightmost will be 3rd bit(0-based indexing) means differentiating bit will be changed

  • @hardikpatel352
    @hardikpatel352 7 місяців тому

    Understood

  • @Rahulyadav-sd3oj
    @Rahulyadav-sd3oj 11 місяців тому

    Thank u sir

  • @AdiCode_Hub
    @AdiCode_Hub 7 місяців тому

    XOR them as you get (naa hindi word ), ❤❤

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 10 місяців тому

    thank you Bhaiya

  • @AanganwadiGuriya
    @AanganwadiGuriya 8 місяців тому

    thanks !

  • @pranavmisra5870
    @pranavmisra5870 9 місяців тому

    This is goldddd

  • @wojtekgaming7
    @wojtekgaming7 6 місяців тому

    Can someone explain me how 14 and 4 got seperated and how we are checking rightmost bit pls do explain

    • @madhu_mohanreddy
      @madhu_mohanreddy 6 місяців тому +1

      we dont seperate 14 and 4 .xorr = xorr^arr[i] 14 and 4 gets xor , and (xorr&xorr-1)^xorr

  • @suhelalam2686
    @suhelalam2686 28 днів тому

    there is a slight mistake , While writing code , rightmost will be (xorr & xorr -1 ) ^ xorr;

  • @tribhuvannath3614
    @tribhuvannath3614 3 місяці тому

    Java Solution
    public static int[] SingleNumber3(int arr[]) {
    int xor = 0;
    for (int i = 0; i < arr.length; i++) {
    xor ^= arr[i];
    }
    int rightMost = xor & -xor;
    int b1 = 0, b2 = 0;
    for (int i = 0; i < arr.length; i++) {
    if ((arr[i] & rightMost) != 0) {
    b1 ^= arr[i];
    } else {
    b2 ^= arr[i];
    }
    }
    return new int[] { b1, b2 };
    }

  • @shreyxnsh.14
    @shreyxnsh.14 10 місяців тому +2

    why are people asking for code?? It is literally the same as the pseudocode he has written.

  • @SAACHIPANDEY
    @SAACHIPANDEY 9 місяців тому +1

    9:40

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 11 місяців тому +1

    public int singleNumber (int[]nums){
    int xor=Arrays.stream(nums).reduce((a,b)->a^b).getAsInt();
    int mask=xor& ~(xor-1);
    int[]ans=new int[2];
    for(int num:nums){
    if((num&mask)>0)
    ans[0]^=num;
    else
    ans[1]^=num;
    }
    return ans;
    }
    🎉❤

  • @_The_-_worst_
    @_The_-_worst_ 11 місяців тому +1

    Once Again
    Make playlist or videos for Competitive programming because I have completed your dsa series 😊

    • @NitinSharma-qx4ff
      @NitinSharma-qx4ff 11 місяців тому +18

      once again, the world doesn't revolve around you bro

  • @santasanthosh2113
    @santasanthosh2113 3 місяці тому

    peak

  • @varun1017
    @varun1017 11 місяців тому

    1st

  • @vidhanpatni8338
    @vidhanpatni8338 4 місяці тому

    13:28 "FUCK"

  • @zaffarzeshan1308
    @zaffarzeshan1308 Місяць тому

    java code
    class Solution
    {
    public int[] twoOddNum(int Arr[], int N)
    {
    // code here
    int xor=0;
    for(int i=0;i

  • @SitaRam-m1i
    @SitaRam-m1i 3 місяці тому +2

    Understood

  • @Shivi32590
    @Shivi32590 6 місяців тому

    understood

  • @animeshkumar2683
    @animeshkumar2683 5 місяців тому

    understood