LeetCode Day 23 - Bitwise AND of Numbers Range

A coding interview problem for today is: return the bitwise AND of all numbers in range [L, R]. I first guessed the statement incorrectly and started explaining something unrelated :D
Consider watching my Bitwise Operations tutorial • Bitwise Operations tut...
Leetcode holds a 30-day Challenge in April with a new coding interview problem appearing each day, here's contest link: leetcode.com/explore/featured...
Subscribe for more educational videos on algorithms, coding interviews and competitive programming.
- Github repository: github.com/Errichto/youtube
- Live streams on 2nd YT channel and on Twitch: / errichto2 & / errichto
- FB and Twitter: / errichto & / errichto
- Frequently Asked Questions: github.com/Errichto/youtube/w...
#Coding #Programming

Пікірлер: 108

  • @AlgoHacks
    @AlgoHacks4 жыл бұрын

    I would love that Everytime you guess it wrong, we will end up having two problems in same video 😂

  • @Yogi79
    @Yogi794 жыл бұрын

    T-shirt is really cool!!

  • @BedoEbied
    @BedoEbied4 жыл бұрын

    I admit it, I'm surprised by this genius simplicity, Thank you!

  • @babyyoda4595
    @babyyoda45954 жыл бұрын

    7:24 This was the cutest part xD "I'm just saying random values"

  • @learnfromarandomguy7026
    @learnfromarandomguy70264 жыл бұрын

    I did this: if (m == n) return m; return rangeBitwiseAnd(m >> 1, n >> 1)

  • @Errichto

    @Errichto

    4 жыл бұрын

    Nice!

  • @koonhanong2267

    @koonhanong2267

    4 жыл бұрын

    Wow, recursive answer

  • @Paradise-kv7fn

    @Paradise-kv7fn

    4 жыл бұрын

    Recursive solution looks cooler and shorter but is always worse than their iterative versions

  • @NikhilRaghav23
    @NikhilRaghav234 жыл бұрын

    In every video something new i'm learning and it helps me to code well than before.

  • @karansagar7870
    @karansagar78704 жыл бұрын

    Never seen this approach , something next level

  • @rohankanade1997
    @rohankanade19974 жыл бұрын

    while m n -= (n & -n) return n (-n) will return 2's complement of n (n & -n) will return LSB of n

  • @jimwoodward7293
    @jimwoodward72934 жыл бұрын

    Educational and informative as always -- thank you!

  • @bhaskar08
    @bhaskar084 жыл бұрын

    For the problem in the beginning of the video We can also have a an array with prefix count of 0. And then p[L] - p[R] == 0 : return true Else : return false p is the prefix count array

  • @davisito70
    @davisito704 жыл бұрын

    Thanks for the problem variation with ranges! I probably would have solved that using segment trees. Tackling each bit individually is a good trick; I'd have counted the number of 1s (or 0s) in an interval instead of calculating where the next 0 is though. But that's just personal preference.

  • @nopecharon
    @nopecharon Жыл бұрын

    thanks Errichto. It was beautifully explained.

  • @musicfan1695
    @musicfan16954 жыл бұрын

    your way of bye bye , that's so cool. never change that

  • @jamjam3448
    @jamjam344820 күн бұрын

    thanks bro. It took me some hours to digest your solution after going through it on paper

  • @aisakyunhotahai8130
    @aisakyunhotahai81303 жыл бұрын

    I did this :) { while(left return right; }

  • @genuineprofile6400
    @genuineprofile64004 жыл бұрын

    Learning something in every video.

  • @andreykarayvansky9549
    @andreykarayvansky95494 жыл бұрын

    Thank you for the video again! I was checking if numbers are equal I put 1 to the lower bit and shift both numbers right until one of the numbers is zero. int rangeBitwiseAnd(int m, int n) { int res = 0; for (int bit = 1; n != 0 && m != 0; n >>>= 1, m >>>= 1, bit

  • @amansinhparmar3336
    @amansinhparmar33364 жыл бұрын

    This guy is on another level

  • @dune499
    @dune4994 жыл бұрын

    You deserve all the subs in the world.

  • @yogeshdhiman8786
    @yogeshdhiman87864 жыл бұрын

    Hi errichto, love your video can you start a series of explaining and coding complex algorithms? Like lucas theorem, segment trees... It would be a gem.

  • @mrvargarobert
    @mrvargarobert4 жыл бұрын

    Nice video! You will not get an all zero suffix when going from m to n. For example, from 5 to 6, 101 - 110, with common prefix 1--. Still, the presented approach works because it is enough to have a zero bit in any number, which will happen due to carries.

  • @1732ashish
    @1732ashish4 жыл бұрын

    it is good that you guessed wrong, we learned two techniques

  • @Anshul42
    @Anshul424 жыл бұрын

    Great solution!

  • 4 жыл бұрын

    That's much simpler than my solution. Very clever.

  • @mohammedshoaib1635
    @mohammedshoaib16354 жыл бұрын

    I love your explanations Errichto, thank you for making these videos! Btw, you are on Linux but have a full version of Microsoft One Note? I thought you could only use the web client on Linux.

  • @AnshuBhuwania
    @AnshuBhuwania4 жыл бұрын

    In bitwise and for range of number[l,r] ,by precomputation you mean to store result for every 30bits of prefix AND at each index right??? And then for getting and of (l,r) for every bit = check if ( result[i][r] - result[i][l-1] == r-l+1) ie.. that many number of 1's is that you meant. or something more simpler...please reply??

  • @harsh6390
    @harsh63904 жыл бұрын

    As always so easy solution .By the way T-shirt is really cool ...From were did you get this?

  • @Errichto

    @Errichto

    4 жыл бұрын

    I guess some Codeforces contest :D

  • @0x1h0b
    @0x1h0b4 жыл бұрын

    Bonus....2 question in 1 video🤩🤩

  • @siddharthchoudhary2148
    @siddharthchoudhary21484 жыл бұрын

    Any suggestion to learn and practice bitwise problems. I am struggling to solve bitwise problems

  • @aravintharjunan9604
    @aravintharjunan96042 жыл бұрын

    while left

  • @ziolkovy
    @ziolkovy4 жыл бұрын

    Hi, great episode. I am thinking abut constant complexity when dealing with difference (n-m). I am not quite sure if it is possible. d = n - m; shift = d ^ n ^ (n|d); //(other bitwise stuff) return n & (-1

  • @itech40
    @itech404 жыл бұрын

    Hello if we didn't have time to complete everything before the end of the 30 days can we do it after?

  • @toghrulgasimzade1066
    @toghrulgasimzade10664 жыл бұрын

    which theme do you use in geany ?

  • @ahsanulameensabit
    @ahsanulameensabit4 жыл бұрын

    Your pseudo assumptions 💚 help us understanding new tricks...

  • @ezizhummedov3607
    @ezizhummedov36074 жыл бұрын

    Can you please show your hands when you're typing and how did you practice your fingers to code typing ( not just typing ). I mean normal letters are okay but symbols and characters are very diffucult to not looking keyboard.

  • @shaswatadutta4451
    @shaswatadutta44514 жыл бұрын

    It can be done using prefix sum of number of zeroes till a specific index, I guess..

  • @satyambhardwaj2289
    @satyambhardwaj22894 жыл бұрын

    every odd no, in Binary ends in 1 and likewise even no. ends in 0. careful with examples Errichto..Anyway Thanks man . YOu're a life saver.

  • @Errichto

    @Errichto

    4 жыл бұрын

    I don't get your comment. Did I say something wrong?

  • @satyambhardwaj2289

    @satyambhardwaj2289

    4 жыл бұрын

    @@Errichto Nope....I just meant the random examples you gave in Binary because the corresponding decimal were even you know..

  • @rohit236c
    @rohit236c4 жыл бұрын

    Great Solution!!!

  • @sumitprajapati821
    @sumitprajapati8214 жыл бұрын

    How did you win that t-shirt which contest?

  • @ThePelcher
    @ThePelcher4 жыл бұрын

    Thank you for these videos. I like your explanations! This is what I produced after hearing your thought process public int rangeBitwiseAnd(int m, int n) { int bitCheck = Integer.MIN_VALUE; while (bitCheck != -1) { if ((bitCheck & m) != (bitCheck & n)) { break; } bitCheck >>= 1; } return m & bitCheck; }

  • @ganeshchowdhary3024
    @ganeshchowdhary30244 жыл бұрын

    I wish one day i could earn a tshirt like that. you are always a true inspirer!

  • @harshraj22_
    @harshraj22_4 жыл бұрын

    T-shirt !!! Thats really cool :)

  • @jacobdomagala
    @jacobdomagala4 жыл бұрын

    First thing that came to my mind: int bitWise(int n, int k) { int ret = INT_MAX; for (int i = n; i

  • @ClaudioBrogliato
    @ClaudioBrogliato4 жыл бұрын

    I solved it by right shifting numbers until they were identical or zero. The concept is the same.

  • @akramelomrani8728

    @akramelomrani8728

    3 жыл бұрын

    yeah and it prevents from overflow it is more recommended

  • @cwagnello
    @cwagnello4 жыл бұрын

    I don't follow what's happening in the "else" how does that give you the correct bitwise & of the values in the range?

  • @cwagnello

    @cwagnello

    4 жыл бұрын

    oh, I figured it out. Went through a couple of examples and suddenly everything was crystal clear.

  • @kamujulasrikar5523
    @kamujulasrikar55234 жыл бұрын

    Thanks man

  • @preetamvarun9219
    @preetamvarun92194 жыл бұрын

    Thanks

  • @gasquakestudios
    @gasquakestudios4 жыл бұрын

    What software do you use to make the visualizations?

  • @Errichto

    @Errichto

    4 жыл бұрын

    OneNote

  • @anirudhatalmale5575
    @anirudhatalmale55753 жыл бұрын

    superb

  • @karthiksk7246
    @karthiksk72464 жыл бұрын

    Damn,that was fast!

  • @prateeksinghal630
    @prateeksinghal6304 жыл бұрын

    I think you might have thought that "Since I'm wearing codeforces t-shirt, why not lets take part in today's codeforces contest as well" xD xD

  • @Errichto

    @Errichto

    4 жыл бұрын

    Maybe, maybe.

  • @annsway
    @annsway3 жыл бұрын

    Briliant!

  • @AnirudhAchal
    @AnirudhAchal4 жыл бұрын

    4:37 the range would pass through '10101 10000000000' instead of '10101 00000000000' 'right? Anyways love the channel! keep posting awesome tutorials please!! :D

  • @szarusz

    @szarusz

    4 жыл бұрын

    still works though, because from the start of the range you carry over a 0. But that's lucky. I suppose that if you're as good as this guy, you tend to do things right by hunch, without fully understanding why you're doing things right :)

  • @AnirudhAchal

    @AnirudhAchal

    4 жыл бұрын

    @@szarusz haha true XD

  • @Kekazen

    @Kekazen

    4 жыл бұрын

    he says it right at 7:20 anyway

  • @szarusz

    @szarusz

    4 жыл бұрын

    @@Kekazen not exactly. His example just hides the problem in a different way.

  • @jinxblaze
    @jinxblaze4 жыл бұрын

    I have more commented code in my solution than code xD

  • @Errichto

    @Errichto

    4 жыл бұрын

    Well, this is what happens when you don't get it right in the first try.

  • @CrystalSergeant
    @CrystalSergeant4 жыл бұрын

    nice. 3 problems in one

  • @PrashantNigam
    @PrashantNigam2 жыл бұрын

    Problem begins at 3:00

  • @anilkrajamoni1484
    @anilkrajamoni14844 жыл бұрын

    which application you are using for writing

  • @ChristianESL

    @ChristianESL

    4 жыл бұрын

    I think is msft oneNote for ipad. The ipad version is the only one with dark theme. The desktop or web version are white theme only.

  • @zenboost.8
    @zenboost.8 Жыл бұрын

    How to get the tshirt? I want to buy

  • @c_tanki207
    @c_tanki2074 жыл бұрын

    @Errichto is someone fexing CF t-shirt ?? hmmmmmmmmmm .... I mma jealous XD

  • @shashanktiwari4442
    @shashanktiwari44424 жыл бұрын

    How do someone get codeforces tshirt?

  • @RAJPATEL-nm9nz

    @RAJPATEL-nm9nz

    4 жыл бұрын

    In some contests of cf, T-shirts are given as prizes.

  • @chandrasekharhethahavya
    @chandrasekharhethahavya4 жыл бұрын

    prefix sums can also be used right?

  • @chandrasekharhethahavya

    @chandrasekharhethahavya

    4 жыл бұрын

    for the problem you stated at beginning

  • @Errichto

    @Errichto

    4 жыл бұрын

    Nope, prefixes can't be used for sum or XOR, but not for minimum or bitwise AND. Just think what happens if there is 0 at the beginning.

  • @Akarirpf

    @Akarirpf

    4 жыл бұрын

    @@Errichto What about we store the prefix sum of total number of 1s upto position i. sum[i] = total number of 1s upto i Then compare sum[R] - sum[L-1] == R-L+1

  • @parthsheth5318

    @parthsheth5318

    4 жыл бұрын

    @@Errichto For the bitwise AND queries problem, what if we store an array such that arr[i] stores the XOR of all numbers before i. Then for any query interval L,R we take int a = arr[R] XOR arr[L], and our answer will be arr[L]&(!a). Would this be right?

  • @CarrotCakeMake

    @CarrotCakeMake

    4 жыл бұрын

    You could use a segment tree though. Wouldn't be as fast but it would work.

  • @ZzwhiskeybkszZ
    @ZzwhiskeybkszZ4 жыл бұрын

    What device do you use to draw?

  • @SameerKhan-nd5qb

    @SameerKhan-nd5qb

    4 жыл бұрын

    Ipad ig

  • @ZzwhiskeybkszZ

    @ZzwhiskeybkszZ

    4 жыл бұрын

    @@SameerKhan-nd5qb Thanks bro

  • @imsiddu
    @imsiddu4 жыл бұрын

    i think this might also work ---> while(n>m)n&=n-1; return n;

  • @Omar-Farhat
    @Omar-Farhat2 ай бұрын

    nice

  • @sounakgupta1372
    @sounakgupta13724 жыл бұрын

    When u code, can u zoom the editor a bit for better view, please!

  • @Errichto

    @Errichto

    4 жыл бұрын

    It's already 125%, are you sure more is needed?

  • @sounakgupta1372

    @sounakgupta1372

    4 жыл бұрын

    @@Errichto no actually I commented during the start of the video when it was not zoomed. Later forgot to delete the comment. Its perfect at 125.

  • @PiyushKumar-ey7qw
    @PiyushKumar-ey7qw4 жыл бұрын

    Why did you take 30 instead of 32???

  • @siddharthgarg5803

    @siddharthgarg5803

    4 жыл бұрын

    32 bits are numbered from 0 to 31. he didnt take 31 because its the sign bit. not sure though.

  • @PiyushKumar-ey7qw

    @PiyushKumar-ey7qw

    4 жыл бұрын

    @@siddharthgarg5803 thanks got it.

  • @sirajummunir2001
    @sirajummunir20014 жыл бұрын

    please be my teacher!!

  • @kishorprasad3817
    @kishorprasad38174 жыл бұрын

    Why your videos are posted one day late?

  • @RAJPATEL-nm9nz

    @RAJPATEL-nm9nz

    4 жыл бұрын

    he post sol after time for prbm completed so that if we were not able to solved it we can understand it, even if we have solved we can get extra information. Posting sol during contest is bad thing.

  • @Errichto

    @Errichto

    4 жыл бұрын

    It's forbidden to discuss problems in 24-hour window after it is published. Even if it was allowed, what's the point in a competition if you can rewrite my solution?

  • @RAJPATEL-nm9nz

    @RAJPATEL-nm9nz

    4 жыл бұрын

    @@Errichto exactly.

  • @kishorprasad3817

    @kishorprasad3817

    4 жыл бұрын

    @@Errichto I think it's not a competition , people can anyways see solution in problem section going to discussion of a question, the winners will be random I guess.

  • @Chirrup...
    @Chirrup...4 жыл бұрын

    Is he from Russia?

  • @tusharkuchchal2891
    @tusharkuchchal28914 жыл бұрын

    for(i=0;i

  • @akashkumarbhagat4764
    @akashkumarbhagat47644 жыл бұрын

    Can I get that t shirt 😏🥺🥺

  • @Errichto

    @Errichto

    4 жыл бұрын

    I don't have two :(

  • @akashkumarbhagat4764

    @akashkumarbhagat4764

    4 жыл бұрын

    @@Errichto no worries, if you get an extra T-shirt in future (which you will get for sure) you can send me that ;-)

  • @huh_wtf
    @huh_wtf4 жыл бұрын

    Speak loudly plz