Counting Bits | Leetcode

This video explains a very important programming interview problem which is to find the number of set bits for all numbers from 0 to N and push them in an array and return as answer.This problem is frequently repeated in interview.There are many approaches to solving this problem but the simplest is to just iterate over all the numbers and every bit of each number and find the count of set bits.This takes O(NlogN) time.I have shown this approach as method-1 in this video.We can further optimize this solution by using simple observations and use extra space of O(N) to just solve it in O(N) time.I have shown the entire intuition with examples and CODE for this optimized solution as method 2. CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
=================================================================
INSTAGRAM: / surya.pratap.k
LinkedIn: / surya-pratap-kahar-47b...
=================================================================
CODE LINK: gist.github.com/SuryaPratapK/...
SIMILAR PROBLEMS:-
Bitwise AND of numbers range: • Bitwise AND of numbers...
Majority element in an array (BitMasking): • Majority element in an...

Пікірлер: 276

  • @Official-tk3nc
    @Official-tk3nc4 жыл бұрын

    I think this is the most underrated youtube channel for programming believe me your channel will reach 1 Million subscribers soon :)

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks for your motivation

  • @tonyz2203

    @tonyz2203

    2 жыл бұрын

    true

  • @subham-raj
    @subham-raj4 жыл бұрын

    I'm glad that I found this channel :)

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Ahhh.... 😁

  • @snehilgupta8558
    @snehilgupta85584 жыл бұрын

    You explain these problems so easily and in the best way ...... Amazing work sir

  • @ShivamKumar-qv6em
    @ShivamKumar-qv6em2 жыл бұрын

    finest u tube channel for competitive programming .

  • @basedmangoes3379
    @basedmangoes33793 жыл бұрын

    amazing explanation. thank you!!! i was struggling with the intuition, but breaking it down was extremeyl helpful!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Nice :)

  • @AnandKumar-uy7nn
    @AnandKumar-uy7nn2 жыл бұрын

    Here the trick is that number of set bits in even numbers is equal to the number of set bits in n/2 and +1 is the number is odd Bcz doubling the number = shifting the number 1 position towards left which contribute to one 0 at the end so no effect on the number of set bits if the number is odd that means we need first add one zero to the end and then add +1 to it in order to make it odd ex : 6 ==> 110 while making it 12 we can shift by one position left so 12 = 110 1100 no more 1 is added to it while making the 13 we need to add +1 to the 12 which has a zero at its end and hence it directly contributes to one 1 12 + 1 ==> 1100 + 1 ==> 1101 class Solution { public: vector countBits(int n) { vector ans(n+1); ans[0]=0; for(int i=1; i

  • @Mercer80

    @Mercer80

    2 жыл бұрын

    So it's dynamic programming as we are using previous results

  • @AnandKumar-uy7nn

    @AnandKumar-uy7nn

    2 жыл бұрын

    @@Mercer80 yeah !!

  • @pforprogrammer
    @pforprogrammer2 жыл бұрын

    watch multiple videos but didn't even get the gist of this topic, but your way of explaining this problem is osm really got the concept... . Thankyou man

  • @pratham654
    @pratham6542 жыл бұрын

    This is the best explanation. More kudos to you 💯💯

  • @udayptp
    @udayptp10 ай бұрын

    Saw many different ways to solve this problem on you tube and gfg but this was best approach. ❤

  • @siddhartha.saif25
    @siddhartha.saif253 жыл бұрын

    i like the way you develop the solution. take example, then have some observations. great job! thank you!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome

  • @violetamalinkova8840
    @violetamalinkova88403 ай бұрын

    Amazing explanation, thank you so much.

  • @dhawalpatil4038
    @dhawalpatil40383 жыл бұрын

    just loved the explanation!

  • @anssha2643
    @anssha26434 жыл бұрын

    Your videos are all so good. Thanks for the conetent. Simple to understand and easy to implement

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @HenryMufc82
    @HenryMufc8211 ай бұрын

    very clear explanation of the solution and the theory behind it, thanks very much :)

  • @Michandel
    @Michandel10 ай бұрын

    Great explanation!

  • @abhishekrai4325
    @abhishekrai43254 жыл бұрын

    Thank you so much sir for such a brilliant explanation. Love your videos. Thank you for putting so much effort :)

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @dayanandraut5660
    @dayanandraut56603 жыл бұрын

    clear cut explanation. 1000 likes for the idea and another 1000 likes for easy explanation. Keep it up. You just earned a subscriber.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks ❤️

  • @ersinerdem7285
    @ersinerdem72852 жыл бұрын

    Great explanation for the DP solution!

  • @saravanan925
    @saravanan9253 жыл бұрын

    Nowadays, I am searching for one problem in youtube, if tech dose has uploaded that problem then my problem becomes easy

  • @saranshdabas4223
    @saranshdabas42233 жыл бұрын

    Instant sub for this kind of content. Keep it up!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @akshaycharjan6384
    @akshaycharjan638410 ай бұрын

    Best one even after 3 years!

  • @raghuveernaraharisetti
    @raghuveernaraharisetti3 жыл бұрын

    Always the best!

  • @nagendravelpuri444
    @nagendravelpuri4442 жыл бұрын

    This is the best algorithm for solving this problem ,good explanation sir!!

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks ☺️

  • @amiteshanand5157
    @amiteshanand51574 жыл бұрын

    can u explain more optimized solution using just log 32 complexity or log num

  • @ayeshaadhikari6123
    @ayeshaadhikari61232 жыл бұрын

    thank you so much sir!

  • @rogers2934
    @rogers29344 жыл бұрын

    Now this is how you explain!!! Great work!!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @tanujkalra7334
    @tanujkalra73343 жыл бұрын

    This approach was fantastic :) Thanks a ton!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @Oscar-vr2md
    @Oscar-vr2md4 жыл бұрын

    You rock, Duuuude! Learned a lot from your videos!!!!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks man :)

  • @farjanask869
    @farjanask8699 ай бұрын

    Chala help ayindi anna thank you so much Annaya

  • @mosiurrahman4061
    @mosiurrahman40618 ай бұрын

    bow down to this guy.

  • @KushalBhatia
    @KushalBhatia3 жыл бұрын

    Mind blowing power of observation. Thank you Keep up the good work @TECH DOSE

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @eshwarnaidu4747
    @eshwarnaidu47472 жыл бұрын

    clear explanation

  • @karthikkunjithapatham3814
    @karthikkunjithapatham38143 жыл бұрын

    Great solution. Am wondering how can one think and arrive at this kind of an approach. Especially during interviews :) This is where I struggle. Mostly my mind revolves around standard problem solving techniques.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Practice will improve your ability :) so keep practicing.

  • @udaychatterjee4424
    @udaychatterjee44242 жыл бұрын

    It's not easy to come up with this solution but if you do...you are awesome !

  • @devmahfuz
    @devmahfuz2 жыл бұрын

    what is the name of the app used for drawing?

  • @priyeshjammula854
    @priyeshjammula8543 жыл бұрын

    Superb explanation

  • @asthapandey265
    @asthapandey2654 жыл бұрын

    @TECH DOSE solution is great , but if the space complexity is o(n), then would it not fail for nos ~=10^9??

  • @amolmishra8621
    @amolmishra86213 жыл бұрын

    This solution is just wow! Mindblown totally.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    :)

  • @harsh007301
    @harsh0073012 жыл бұрын

    what if we were given any random range instead of 0 to n? is there any O(n) solution?

  • @ShivamKumar-nx6tt
    @ShivamKumar-nx6tt2 жыл бұрын

    Great explanation bro 🔥🔥

  • @SHARMATUSHAR1_
    @SHARMATUSHAR1_9 ай бұрын

    Best explanation. I watched Neetcode's video first but this explanation is a lot better.

  • @techdose4u

    @techdose4u

    9 ай бұрын

    :)

  • @chayandas4942
    @chayandas49423 жыл бұрын

    Bhaiya, you such a great teacher. You make us understand so patiently and easily. Thank you

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @wentingsong9435
    @wentingsong94354 жыл бұрын

    Brilliant, you are a savior!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @siddhartha.saif25
    @siddhartha.saif253 жыл бұрын

    very nice! thank you!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome

  • @mondayemmanuel191
    @mondayemmanuel1913 жыл бұрын

    I totally enjoy your explanations.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @RanjuRao
    @RanjuRao3 жыл бұрын

    Thank you for the solution :)

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome

  • @newbeessfu7820
    @newbeessfu78203 жыл бұрын

    EXCELLENT

  • @JamesHalpert8555
    @JamesHalpert85554 жыл бұрын

    Great explanation as always!!! Very much helpful!! Thank You!! Can you please make a video on time complexity and space complexity...

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Yea sure. I have started the playlist but not finished due to challenges. Once this is over, I will finish it.

  • @anand.prasad502
    @anand.prasad5023 жыл бұрын

    simply awesome! Thank you very much :)

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome

  • @chidinmakalu502
    @chidinmakalu5022 жыл бұрын

    Thank you for your explanation! However, I don't quite understand the time complexity for the first approach. Isn't it supposed to be O(N)? Time complexity using Brian Kernighan's algorithm is O(1) + O(N+ 1) to iterate from range(0, n+1). Please, am I missing something?

  • @ajaywadhwa3398
    @ajaywadhwa33983 жыл бұрын

    Awesome Explanations !! Really helped a lot.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @liftwithamey
    @liftwithamey3 жыл бұрын

    Great Explanation !!!!!!!!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @michaelchan6144
    @michaelchan61443 жыл бұрын

    great vid! subscribed!!!!!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks 😊

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

    vector countBits(int n) { vectorbitcount; for(int i=0;i

  • @krishangopal6795
    @krishangopal67954 жыл бұрын

    Best youtube channel till now.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @krishangopal6795

    @krishangopal6795

    4 жыл бұрын

    TECH DOSE Sir please share your mail or contact number need placement related advice

  • @pennilesscoder3926
    @pennilesscoder39263 жыл бұрын

    The Method is Generally Look Up table Method......For finding set bits at each Number....But We can Find Total Number of Set bits from 1 to N with Time complexity O(logN)....Which is better than O(N)....And the right shift or divide by 2 approcah is well explained and understood

  • @lekanosagie

    @lekanosagie

    3 жыл бұрын

    I think you're misunderstanding. This method finds the number of set bits in O(1) time, not O(logN). Given that there are N elements in the array, the time omplexity is O(1) * N = O(N). Populating the array is O(N) at the least, so if it took LogN to find the number of set bits, then the time complexity for your solution would be O(NLogN) which is slower than TECH DOSE's approach.

  • @PriyanshuSingh-dt3oz
    @PriyanshuSingh-dt3oz2 жыл бұрын

    Crystal Clear Explaination

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks ❤️

  • @sandeepkumarnagamalla1020
    @sandeepkumarnagamalla10204 жыл бұрын

    Excellent explanation !! Very easy to understand sir. thanks a lot for making this video.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @ishwaripednekar5164
    @ishwaripednekar51642 жыл бұрын

    Cool 👍

  • @santanu29
    @santanu292 жыл бұрын

    Thank you for the explanation

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome 😊

  • @sahilbabbar8859
    @sahilbabbar88592 жыл бұрын

    Sir ye wali video bit manipulation Vali playlist me ni hai. Daal do pls

  • @bogasaiteja8968
    @bogasaiteja89683 жыл бұрын

    Thanks man!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome

  • @mahadishakkhor123
    @mahadishakkhor1236 ай бұрын

    i can understand from ur video

  • @navyaagrawal2474
    @navyaagrawal24744 жыл бұрын

    Please continue making videos in the upcoming June challenge too. Your videos are awesome!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @abhisheksunda7925
    @abhisheksunda79253 жыл бұрын

    Great!! How to come up with this kind of solution?

  • @techNtech352
    @techNtech3524 жыл бұрын

    Seriously, this is one of the best Channel for coding problems. Such a nice explanation as always

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks

  • @siddhantsamal8878
    @siddhantsamal88783 жыл бұрын

    sir , you are truly a genius ,always helped a lot..Thankyou

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @orchideirakozesr8842
    @orchideirakozesr88423 жыл бұрын

    Very thankful for the work you’re doing mate !

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @dontknow592
    @dontknow5923 жыл бұрын

    Thank You Sir. Very precisely explained 🤓🙌🏻

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @mrinmoypaul6810
    @mrinmoypaul68103 жыл бұрын

    great explanation and solution

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @srirammuralidaran2121
    @srirammuralidaran21213 жыл бұрын

    Superb explanation and easy to understand the solution. Thanks keep going ...

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @RicheshGuptaRichu
    @RicheshGuptaRichu3 жыл бұрын

    Tagda bhai.. GFG DSA course me bhgaa dia tha iss topic ko

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks bhai 😀

  • @keshavraghav3896
    @keshavraghav38963 жыл бұрын

    ooooooohhhhh... you are really awesome sir...!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @shivam7929
    @shivam79292 жыл бұрын

    this is explanation is lit man , I am totally mesmerized

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks :)

  • @kunalsoni7681
    @kunalsoni76814 жыл бұрын

    Sir this video is such a helpful to me.. to understand bit manipulation 😊☺️.. thank you..

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

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

    Excellent Explanation

  • @techdose4u

    @techdose4u

    Жыл бұрын

    Thanks :)

  • @sabihaakter4580
    @sabihaakter45803 жыл бұрын

    Nice Explanation

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @mahipalsingh-yo4jt
    @mahipalsingh-yo4jt3 жыл бұрын

    Excellent sir !!!!!!!!!!!!!!!!!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @aditikhandelwal135
    @aditikhandelwal1354 жыл бұрын

    Your explanation are just amazing 😍😍

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @rashmimishra5356
    @rashmimishra53562 жыл бұрын

    Awesome explaination

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks 😊

  • @rahulbawa3969
    @rahulbawa39693 жыл бұрын

    Thanks for the awesome explanation. However, I doubt if I could have come up with this observation in an actual interview. I would have just gone with O(32n) solution :(

  • @bree9895

    @bree9895

    Жыл бұрын

    i dont think anyone can come up with this without seeing it first :/

  • @Owais486
    @Owais4865 ай бұрын

    thank u so much sir 😭😭😭

  • @bhopalsingh4102
    @bhopalsingh41024 жыл бұрын

    Clean and crisp explanation...

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks

  • @abhishekshankar1136
    @abhishekshankar11363 жыл бұрын

    sir please explain how to solve it in log(N) time using O(1) space, im unable to understand the solution provided in GFG

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    You can do the logN solution easily after you understand the O(N) solution. Just little tweak is required :)

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

    you rock sir

  • @NoOneIsHereRightNow
    @NoOneIsHereRightNow3 жыл бұрын

    Amazing solution thanks

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @saravana6882
    @saravana68823 жыл бұрын

    fantastic solution

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @anujchauhan4876
    @anujchauhan48762 жыл бұрын

    Thanks bro !

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome 😄

  • @gokulnaathb2627
    @gokulnaathb26272 жыл бұрын

    Very nicely explained, Sir

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks 😊

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

    great explanation

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @abhisekmishra5245
    @abhisekmishra52454 жыл бұрын

    just wow !!! nice explanation sir

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @yashkapoor508
    @yashkapoor5083 жыл бұрын

    The best solution I found so far 😁🥳

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Nice :)

  • @sukanyasen305
    @sukanyasen3053 жыл бұрын

    Beautiful solution

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @manassingh8063
    @manassingh80634 жыл бұрын

    Will improving solution from O(32*N) to O(N) will make any difference because asymptotically both the solution are of same complexity?

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Yea it will. If think carefully then 32N is actually NlogN. Ask yourself if improving time from NlogN to N will make any difference!!

  • @agileprogramming7463
    @agileprogramming74634 жыл бұрын

    Mind == blown away

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    🤣 a coder's way to represent feelings

  • @deveshsharma7487
    @deveshsharma74873 жыл бұрын

    Really a great obervation and the best method to solve this problem I have seen till now. Thanks a lot, I wish your channel becomes more and more famous. Keep up the good work

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks for your wishes. Touchwood 😀

  • @m.s.nabhiram1532
    @m.s.nabhiram15323 жыл бұрын

    very nicely explained

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @pazarcimpazarcim8677
    @pazarcimpazarcim86772 жыл бұрын

    bro, you're amazing

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks :)

  • @dipeshsaili4468
    @dipeshsaili44682 жыл бұрын

    Thankyou so much sir

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome 😊

  • @rishabsinha8749
    @rishabsinha87493 жыл бұрын

    dp[i] = dp[i>>1] + (i&1); can also be done and it is faster than / and %