Learn these 10 Bitwise Tricks Or Regret Later | Competitive Programming Tricks Part 2

Поділитися
Вставка
  • Опубліковано 27 лис 2024

КОМЕНТАРІ • 88

  • @llarn.i5103
    @llarn.i5103 2 дні тому

    literally the best bitwise video, clear and straight to the point, thanks a lot.

  • @Josuke217
    @Josuke217 4 місяці тому +19

    This is extremely helpful. One of the best videos on bit manipulation.

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

    This one is fun:
    Find the unique number in an array. For example arr = [2, 4, 3, 5, 1, 4, 2, 1, 5] where 3 is the unique number.
    Solution:
    arr.reduce((a, b) => a^b)
    This works if there are an odd number of occurrences of the unique number and an even number of occurrences of all other numbers. When you XOR all the numbers in the array, the even occurrences of the repeated numbers will cancel each other out (since x ^ x = 0), and you will be left with the unique number (since it only occurs once).

  • @PRIYANSHUYADAV-oh1pu
    @PRIYANSHUYADAV-oh1pu 8 місяців тому +5

    I was in problem about the bitwise operator but now I found the solution
    Thankyou very much

  • @adityarupda1795
    @adityarupda1795 Рік тому +11

    For 10th trick in java there is a similar function to count setbit
    For integer x it is Integer.bitCount(x) and for long it is Long.bitCount(x).

  • @varshabadda
    @varshabadda 5 днів тому

    This is really insane.Really helpful.

  • @kapilpatel9379
    @kapilpatel9379 Рік тому +1

    Watched half of it and liked it, let me come back when I finish the basic DSA part

  • @anshulsinghtomar780
    @anshulsinghtomar780 Рік тому +5

    I have seen the complete lecture & it was amazing .
    The way of delivering concepts was top Notch , video & audio quality .
    Your content deserves more reach.
    Thanks for the Concepts brother.
    Respect++;

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

    That was good info in a short and concise video . Loved it !

  • @kaylee-xie
    @kaylee-xie Рік тому +7

    Good video! The lecture is straightforward and easy to understand!

  • @aarushiagarwal8938
    @aarushiagarwal8938 Рік тому +12

    It would be really helpful if you would make a video on how to apply binary search technique to solve hard cp problems sometimes it happens that we might not even know that the problem involves binary search.
    Btw this one is again awsome and great video on bits👍

    • @shri9229
      @shri9229 Рік тому +10

      Knowing the problem involves binary search is the hardest part, generally look for clues like minimize maximize, maybe this helps, recent div3 E question involved binary search, and I was so happy it clicked to me. dopamine.

    • @snehil3209
      @snehil3209 Рік тому +1

      That interview problem right🤔

    • @aarushiagarwal8938
      @aarushiagarwal8938 Рік тому +2

      @@shri9229 thanks for the tip hoping to apply soon

  • @ayushmishra6575
    @ayushmishra6575 Рік тому +2

    I knew the first one ,but others are also cool as hell. Really like this video.

    • @farj3946
      @farj3946 Рік тому

      hell is not cool btw

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

    Very useful video..I felt bitwise operations as tough topic. You have made it easy now. Thank u

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

    I just now saw this video . I am regretting why I didn't see this before 😢. It's really useful. Thank you .

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

    Great explanation, in simple examples

  • @sanooosai
    @sanooosai 24 дні тому

    great sir thank you

  • @shreyashpatil4262
    @shreyashpatil4262 Рік тому +2

    Please make more videos like DIFFERENCE ARRAY TECHNIQUE, (yesterday's vid), i have stuck many times on same type of questions/ questions which use similar concepts and we are just unable to optimise the code... also thanks for everything

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

    awesome video, thanks!

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

    It was really helpful. Thank you for such a video and good explanation👍👍.

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

    thank you for this video sir....

  • @aviraljoshi-vx9kq
    @aviraljoshi-vx9kq 4 місяці тому

    very nice explanation

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

    Nothing to say about you,,you are awesome

  • @krsnasameer
    @krsnasameer Рік тому

    Really great video, mad respect for you!

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

    Thanks a lot! this was very helpful.

  • @HarshitGupta-i7s
    @HarshitGupta-i7s 3 місяці тому

    Great video !

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

    Thanks. Super good!

  • @HarsithR-j6t
    @HarsithR-j6t 11 місяців тому

    Awesome tricks!

  • @aveermukherjee6419
    @aveermukherjee6419 Рік тому +31

    the bin function looks like sin function 😂

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

      🤣🤣 And, & looks like a yoga position 😉

    • @Its.abhi..
      @Its.abhi.. 9 місяців тому

      Bc meri toh fatt gayi thi , sin kabse agya

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

    NICE SUPER EXCELLENT MOTIVATED

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

    Guys in trick 06 use XOR instead od AND beacuse it gives you a wrong swape here is an example: Initial values:
    X = 2 (binary: 10)
    Y = 3 (binary: 11)
    X = X AND Y (10 AND 11):
    X = 10 AND 11 = 10 (decimal: 2)
    Y = X AND Y (10 AND 11):
    Y = 10 AND 11 = 10 (decimal: 2)
    X = X AND Y (10 AND 10):
    X = 10 AND 10 = 10 (decimal: 2)
    see its not swaping correctly! but when you use XOR it will swape the values perfectly!

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

      bro watch the video once again where he told you to use AND. he is only using XOR in all three steps

  • @priyamagarwal3469
    @priyamagarwal3469 Рік тому +2

    Last few tricks are amazing.
    Can we have a video on Number theory tricks that can be very useful. I stuck a lot on basic Number Theory problems.

    • @aayushanand8285
      @aayushanand8285 Рік тому +2

      Trick 9 is garbage. Use this identity only if you encounter rhs part somewhere, to transform back to A + B.
      How can a simple arithmetic operation which even has a cpu instruction for it (ie. ADD instruction), be slower than writing some hob nob like (A^B)+2*(A&B) or (A|B)+(A&B). Priyansh is trying to say us that we should opt for an operation consisting of a bitwise or, and, along with an "add" to replace a simple ADD operation. How idiotic isn't it. For the first identity, it also needs you to multiply with two as well. Simply slow because instead of doing one operation we're doing 3-4 extra operations.

    • @aayushanand8285
      @aayushanand8285 Рік тому

      Just if something works mathematically, doesn't mean that going that alternative route is going to be faster. This is only learned through practice, and people who are aware of this, won't give out such tips without a 'gotcha'. Giving this information is good. But it should also be accompanied by a disclaimer that using this would make your code slow instead of fast. He says this in the intro of this video that bitwise operations are faster. People are going to take his word for it due his immense influence and popularity, but this is wrong.

    • @aayushanand8285
      @aayushanand8285 Рік тому

      According to my benchmarks
      The first version is the most slow, (140796ns)
      2nd version is a bit better than the first, but still slow, (1146ns)
      3rd version is the fastest of all of them (108ns)

    • @aayushanand8285
      @aayushanand8285 Рік тому

      Here is the benchmark code

    • @aayushanand8285
      @aayushanand8285 Рік тому +1

      #include
      #include
      int Add1(int a, int b);
      int Add2(int a, int b);
      int AddNormal(int a, int b);
      constexpr int numIters = (int) 1e9;
      class Timer
      {
      public:
      using NanoSeconds = std::chrono::nanoseconds;
      public:
      Timer(std::chrono::nanoseconds& runtimeRef, bool shouldAlsoPrint = false)
      :m_RuntimeRefToReturn(runtimeRef)
      ,m_ShouldPrint(shouldAlsoPrint)
      {
      start = std::chrono::system_clock::now();
      }
      ~Timer()
      {
      m_RuntimeRefToReturn = std::chrono::system_clock::now() - start;

      if (m_ShouldPrint)
      std::cout

  • @shashwatyadav09
    @shashwatyadav09 Рік тому +12

    Please upload videos like this, a few tips and tricks in competitive programming and related techniques.

  • @World-Of-Mr-Motivater
    @World-Of-Mr-Motivater 11 місяців тому

    Very useful one

  • @harshitgupta2909
    @harshitgupta2909 Рік тому

    bro i am your big fan i love you bro i like your coding skills and i follow you too

  • @MMNayem-dq4kd
    @MMNayem-dq4kd Рік тому

    Thank you vaiya.

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

    Everything😊😊

  • @thulthula_diddy
    @thulthula_diddy Рік тому

    Thank you

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

    please post pt2 !!

  • @abhinavgupta4778
    @abhinavgupta4778 Рік тому +2

    17:08 actually, can swap two numbers without using temporary variable without xor.
    suppose two numbers x and y
    now let x = x-y
    y = y+x ( y + x-y = x)
    x = y - x = (x - x +y)
    x and y are now swapped.

    • @adityarupda1795
      @adityarupda1795 Рік тому +1

      You can also write
      X = X+Y;
      Y = X-Y;
      X = X-Y;

    • @abhinavgupta4778
      @abhinavgupta4778 Рік тому +1

      @@adityarupda1795 yes 👍

    • @codecorn8030
      @codecorn8030 Рік тому +3

      see the problem with this is overflow issue
      X+Y can sometimes be more than "long long int "
      say we needed to swap
      X = 1e18 and Y = 1e18 (yeah ik its same...but think user input was this and u dint knw both are same)
      now ur code fails
      ^ code works fine for all
      there are * and / to do the same thing u did
      but ^ way is best and can handel all the cases

  • @dreamer707
    @dreamer707 Рік тому

    awesome man

  • @KARTHIKEYANS-x9p
    @KARTHIKEYANS-x9p Рік тому

    Man the last one was new to me found it more usefull.Try to make more usefull videos ilke this.

  • @Lucy92735
    @Lucy92735 19 днів тому

    thanks:)

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

    I liked trick 8 more than others

  • @kirtisingh-p4q
    @kirtisingh-p4q 10 місяців тому

    Hi, a very popular problem with bitwise is when all the numbers appear 3 times except 1, can you please make a detailed video for the same.
    Thanks!

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

    A+b trick again you are doing addition after or and & operations

  • @ayushjaiswal2020
    @ayushjaiswal2020 Рік тому

    xor swap trick ques was asked in ICPC Amritapuri Qualifiers.

    • @codecorn8030
      @codecorn8030 Рік тому

      really ?
      which year ??
      if u are talking abt XOR sort of 2022
      nah bro...that question was completely diff 😂😂

    • @ayushjaiswal2020
      @ayushjaiswal2020 Рік тому

      @@codecorn8030 i did it my xor swap approach only.

  • @shri9229
    @shri9229 Рік тому

    23:02 eureka moment for me.

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

    Which note taking apps do you use???

  • @kiranc-dc2wc
    @kiranc-dc2wc Місяць тому

    very good explanation easy to understand but inside video is annoying

  • @depressd-boy.
    @depressd-boy. 28 днів тому

    i need 1st vedio link

  • @Sandy-px1fz
    @Sandy-px1fz 11 місяців тому

    in the third trick the last one i didnt understand what you said and how it write you mean not of all the bits but how?

  • @ChotuSingh-em4qm
    @ChotuSingh-em4qm 7 місяців тому +1

    bro try to run one program on each trick as an example

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

    same video with animation on Log2base2.

  • @icyesports803
    @icyesports803 Рік тому

    When will tle eliminator next batch is going to start???

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

    I think I might actually win my competition now! best trick were: odd or even, and divide and multiply with 2^k

  • @phoenix_1_3
    @phoenix_1_3 Рік тому +1

    When does TLE eliminators batch start after September? I'd like to join after my placements
    Checking if a number is a power of 2
    Swapping two numbers
    Manipulating kth bit
    Counting set bits
    These were super useful to me. Thanks a lot vro !
    Btw, whenever I give contests, I feel really hard to solve Bit manipulation problems. I can't even build up the intuition. Can you post long video about Bit manips covering the stuff needed for CP other than basics? It'd be really helpful

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

    8:04 The formula to toggle the Kth bit is x^(1

  • @vishnu4647
    @vishnu4647 Рік тому +1

    where is part1??

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

    Trick 8 is not exactly correct, and wont be good to use for some problems because it will return unexpected result if x is neither a nor b.

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

    Sin😂😂😂bin

  • @devlove-c3f
    @devlove-c3f 3 місяці тому

    Wan zero wan hahahhaa

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

    Why can't I understand anything

    • @RISHABH-VERMA
      @RISHABH-VERMA 4 місяці тому

      Maybe you should start using bit operations a lil more in your code

  • @AbdulMoeed-h1t
    @AbdulMoeed-h1t Місяць тому

    can you just slow down a bit? smh

  • @harshitgupta2909
    @harshitgupta2909 Рік тому

    bro i am your big fan i love you bro i like your coding skills and i follow you too