Counting Bits | Leetcode

  Рет қаралды 68,590

Techdose

Techdose

4 жыл бұрын

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-tk3nc 4 жыл бұрын
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-raj 4 жыл бұрын
I'm glad that I found this channel :)
@techdose4u
@techdose4u 4 жыл бұрын
Ahhh.... 😁
@snehilgupta8558
@snehilgupta8558 4 жыл бұрын
You explain these problems so easily and in the best way ...... Amazing work sir
@ShivamKumar-qv6em
@ShivamKumar-qv6em 2 жыл бұрын
finest u tube channel for competitive programming .
@basedmangoes3379
@basedmangoes3379 3 жыл бұрын
amazing explanation. thank you!!! i was struggling with the intuition, but breaking it down was extremeyl helpful!
@techdose4u
@techdose4u 3 жыл бұрын
Nice :)
@udayptp
@udayptp 11 ай бұрын
Saw many different ways to solve this problem on you tube and gfg but this was best approach. ❤
@pratham654
@pratham654 2 жыл бұрын
This is the best explanation. More kudos to you 💯💯
@anssha2643
@anssha2643 4 жыл бұрын
Your videos are all so good. Thanks for the conetent. Simple to understand and easy to implement
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@siddhartha.saif25
@siddhartha.saif25 3 жыл бұрын
i like the way you develop the solution. take example, then have some observations. great job! thank you!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@pforprogrammer
@pforprogrammer 2 жыл бұрын
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
@HenryMufc82
@HenryMufc82 11 ай бұрын
very clear explanation of the solution and the theory behind it, thanks very much :)
@abhishekkeshri3974
@abhishekkeshri3974 Жыл бұрын
vector countBits(int n) { vectorbitcount; for(int i=0;i
@saranshdabas4223
@saranshdabas4223 3 жыл бұрын
Instant sub for this kind of content. Keep it up!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@dhawalpatil4038
@dhawalpatil4038 3 жыл бұрын
just loved the explanation!
@violetamalinkova8840
@violetamalinkova8840 3 ай бұрын
Amazing explanation, thank you so much.
@ersinerdem7285
@ersinerdem7285 2 жыл бұрын
Great explanation for the DP solution!
@akshaycharjan6384
@akshaycharjan6384 11 ай бұрын
Best one even after 3 years!
@dayanandraut5660
@dayanandraut5660 3 жыл бұрын
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 ❤️
@abhishekrai4325
@abhishekrai4325 4 жыл бұрын
Thank you so much sir for such a brilliant explanation. Love your videos. Thank you for putting so much effort :)
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@tanujkalra7334
@tanujkalra7334 3 жыл бұрын
This approach was fantastic :) Thanks a ton!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@AnandKumar-uy7nn
@AnandKumar-uy7nn 2 жыл бұрын
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 !!
@Oscar-vr2md
@Oscar-vr2md 4 жыл бұрын
You rock, Duuuude! Learned a lot from your videos!!!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks man :)
@Michandel
@Michandel 11 ай бұрын
Great explanation!
@rogers2934
@rogers2934 4 жыл бұрын
Now this is how you explain!!! Great work!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@nagendravelpuri444
@nagendravelpuri444 2 жыл бұрын
This is the best algorithm for solving this problem ,good explanation sir!!
@techdose4u
@techdose4u 2 жыл бұрын
Thanks ☺️
@KushalBhatia
@KushalBhatia 3 жыл бұрын
Mind blowing power of observation. Thank you Keep up the good work @TECH DOSE
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@amolmishra8621
@amolmishra8621 3 жыл бұрын
This solution is just wow! Mindblown totally.
@techdose4u
@techdose4u 3 жыл бұрын
:)
@raghuveernaraharisetti
@raghuveernaraharisetti 3 жыл бұрын
Always the best!
@SHARMATUSHAR1_
@SHARMATUSHAR1_ 10 ай бұрын
Best explanation. I watched Neetcode's video first but this explanation is a lot better.
@techdose4u
@techdose4u 10 ай бұрын
:)
@karthikkunjithapatham3814
@karthikkunjithapatham3814 3 жыл бұрын
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.
@mondayemmanuel191
@mondayemmanuel191 3 жыл бұрын
I totally enjoy your explanations.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@saravanan925
@saravanan925 3 жыл бұрын
Nowadays, I am searching for one problem in youtube, if tech dose has uploaded that problem then my problem becomes easy
@udaychatterjee4424
@udaychatterjee4424 2 жыл бұрын
It's not easy to come up with this solution but if you do...you are awesome !
@sandeepkumarnagamalla1020
@sandeepkumarnagamalla1020 4 жыл бұрын
Excellent explanation !! Very easy to understand sir. thanks a lot for making this video.
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@RanjuRao
@RanjuRao 3 жыл бұрын
Thank you for the solution :)
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@wentingsong9435
@wentingsong9435 4 жыл бұрын
Brilliant, you are a savior!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@amiteshanand5157
@amiteshanand5157 4 жыл бұрын
can u explain more optimized solution using just log 32 complexity or log num
@farjanask869
@farjanask869 10 ай бұрын
Chala help ayindi anna thank you so much Annaya
@ayeshaadhikari6123
@ayeshaadhikari6123 3 жыл бұрын
thank you so much sir!
@chayandas4942
@chayandas4942 3 жыл бұрын
Bhaiya, you such a great teacher. You make us understand so patiently and easily. Thank you
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@asthapandey265
@asthapandey265 4 жыл бұрын
@TECH DOSE solution is great , but if the space complexity is o(n), then would it not fail for nos ~=10^9??
@ShivamKumar-nx6tt
@ShivamKumar-nx6tt 2 жыл бұрын
Great explanation bro 🔥🔥
@mosiurrahman4061
@mosiurrahman4061 8 ай бұрын
bow down to this guy.
@ajaywadhwa3398
@ajaywadhwa3398 3 жыл бұрын
Awesome Explanations !! Really helped a lot.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@JamesHalpert8555
@JamesHalpert8555 4 жыл бұрын
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.
@liftwithamey
@liftwithamey 3 жыл бұрын
Great Explanation !!!!!!!!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@priyeshjammula854
@priyeshjammula854 3 жыл бұрын
Superb explanation
@PriyanshuSingh-dt3oz
@PriyanshuSingh-dt3oz 2 жыл бұрын
Crystal Clear Explaination
@techdose4u
@techdose4u 2 жыл бұрын
Thanks ❤️
@eshwarnaidu4747
@eshwarnaidu4747 2 жыл бұрын
clear explanation
@siddhartha.saif25
@siddhartha.saif25 3 жыл бұрын
very nice! thank you!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@chidinmakalu502
@chidinmakalu502 2 жыл бұрын
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?
@michaelchan6144
@michaelchan6144 3 жыл бұрын
great vid! subscribed!!!!!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks 😊
@anand.prasad502
@anand.prasad502 4 жыл бұрын
simply awesome! Thank you very much :)
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@krishangopal6795
@krishangopal6795 4 жыл бұрын
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
@srirammuralidaran2121
@srirammuralidaran2121 3 жыл бұрын
Superb explanation and easy to understand the solution. Thanks keep going ...
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@santanu29
@santanu29 2 жыл бұрын
Thank you for the explanation
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😊
@bhopalsingh4102
@bhopalsingh4102 4 жыл бұрын
Clean and crisp explanation...
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@aditikhandelwal135
@aditikhandelwal135 4 жыл бұрын
Your explanation are just amazing 😍😍
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@siddhantsamal8878
@siddhantsamal8878 3 жыл бұрын
sir , you are truly a genius ,always helped a lot..Thankyou
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@shivam7929
@shivam7929 2 жыл бұрын
this is explanation is lit man , I am totally mesmerized
@techdose4u
@techdose4u 2 жыл бұрын
Thanks :)
@bogasaiteja8968
@bogasaiteja8968 3 жыл бұрын
Thanks man!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@kunalsoni7681
@kunalsoni7681 4 жыл бұрын
Sir this video is such a helpful to me.. to understand bit manipulation 😊☺️.. thank you..
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@dontknow592
@dontknow592 3 жыл бұрын
Thank You Sir. Very precisely explained 🤓🙌🏻
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@techNtech352
@techNtech352 4 жыл бұрын
Seriously, this is one of the best Channel for coding problems. Such a nice explanation as always
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@mahipalsingh-yo4jt
@mahipalsingh-yo4jt 3 жыл бұрын
Excellent sir !!!!!!!!!!!!!!!!!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@suryakiran2970
@suryakiran2970 Жыл бұрын
Excellent Explanation
@techdose4u
@techdose4u Жыл бұрын
Thanks :)
@gokulnaathb2627
@gokulnaathb2627 2 жыл бұрын
Very nicely explained, Sir
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊
@mrinmoypaul6810
@mrinmoypaul6810 3 жыл бұрын
great explanation and solution
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@keshavraghav3896
@keshavraghav3896 3 жыл бұрын
ooooooohhhhh... you are really awesome sir...!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@abhisekmishra5245
@abhisekmishra5245 4 жыл бұрын
just wow !!! nice explanation sir
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@newbeessfu7820
@newbeessfu7820 3 жыл бұрын
EXCELLENT
@rashmimishra5356
@rashmimishra5356 2 жыл бұрын
Awesome explaination
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊
@orchideirakozesr8842
@orchideirakozesr8842 3 жыл бұрын
Very thankful for the work you’re doing mate !
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@NirmalSilwal
@NirmalSilwal 3 жыл бұрын
great explanation
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@ishwaripednekar5164
@ishwaripednekar5164 2 жыл бұрын
Cool 👍
@NoOneIsHereRightNow
@NoOneIsHereRightNow 3 жыл бұрын
Amazing solution thanks
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@sabihaakter4580
@sabihaakter4580 3 жыл бұрын
Nice Explanation
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@deveshsharma7487
@deveshsharma7487 3 жыл бұрын
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 😀
@RicheshGuptaRichu
@RicheshGuptaRichu 3 жыл бұрын
Tagda bhai.. GFG DSA course me bhgaa dia tha iss topic ko
@techdose4u
@techdose4u 3 жыл бұрын
Thanks bhai 😀
@m.s.nabhiram1532
@m.s.nabhiram1532 3 жыл бұрын
very nicely explained
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@agileprogramming7463
@agileprogramming7463 4 жыл бұрын
Mind == blown away
@techdose4u
@techdose4u 4 жыл бұрын
🤣 a coder's way to represent feelings
@saravana6882
@saravana6882 3 жыл бұрын
fantastic solution
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@harsh007301
@harsh007301 3 жыл бұрын
what if we were given any random range instead of 0 to n? is there any O(n) solution?
@yashkapoor508
@yashkapoor508 3 жыл бұрын
The best solution I found so far 😁🥳
@techdose4u
@techdose4u 3 жыл бұрын
Nice :)
@sukanyasen305
@sukanyasen305 3 жыл бұрын
Beautiful solution
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@devmahfuz
@devmahfuz 2 жыл бұрын
what is the name of the app used for drawing?
@anujchauhan4876
@anujchauhan4876 2 жыл бұрын
Thanks bro !
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😄
@rahulbawa3969
@rahulbawa3969 3 жыл бұрын
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 :/
@LALITKUMAR-dg4yj
@LALITKUMAR-dg4yj 4 жыл бұрын
it helps..thank you...
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@manassingh8063
@manassingh8063 4 жыл бұрын
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!!
@navyaagrawal2474
@navyaagrawal2474 4 жыл бұрын
Please continue making videos in the upcoming June challenge too. Your videos are awesome!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@mahadishakkhor123
@mahadishakkhor123 6 ай бұрын
i can understand from ur video
@pazarcimpazarcim8677
@pazarcimpazarcim8677 2 жыл бұрын
bro, you're amazing
@techdose4u
@techdose4u 2 жыл бұрын
Thanks :)
@topcoder3812
@topcoder3812 4 жыл бұрын
Simple code sol // Time: O(n) // Space: O(n) class Solution { public: vector countBits(int num) { vector res{0}; for (int i = 1; i > 1]); } return res; } };
@techdose4u
@techdose4u 4 жыл бұрын
👍
@pennilesscoder3926
@pennilesscoder3926 3 жыл бұрын
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.
@singurubhavana8368
@singurubhavana8368 4 жыл бұрын
Simply awesome👌👌
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@dipeshsaili4468
@dipeshsaili4468 2 жыл бұрын
Thankyou so much sir
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😊
@yyyooohhhooo
@yyyooohhhooo 4 жыл бұрын
Thank you sir!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@rishabsinha8749
@rishabsinha8749 3 жыл бұрын
dp[i] = dp[i>>1] + (i&1); can also be done and it is faster than / and %
@Owais486
@Owais486 6 ай бұрын
thank u so much sir 😭😭😭
Counting Bits - Dynamic Programming - Leetcode 338 - Python
13:24
Remove K digits | Build lowest number | Leetcode #402
15:30
Techdose
Рет қаралды 88 М.
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 67 МЛН
No empty
00:35
Mamasoboliha
Рет қаралды 10 МЛН
ЧУТЬ НЕ УТОНУЛ #shorts
00:27
Паша Осадчий
Рет қаралды 10 МЛН
Maximal square | Dynamic programming | Leetcode #221
18:27
Techdose
Рет қаралды 70 М.
Jump game | Leetcode #55 | Valley peak approach
12:28
Techdose
Рет қаралды 185 М.
SHA: Secure Hashing Algorithm - Computerphile
10:21
Computerphile
Рет қаралды 1,2 МЛН
5 Math Skills Every Programmer Needs
9:08
Sahil & Sarra
Рет қаралды 1 МЛН
Trick for spiral matrix traversal
10:12
Techdose
Рет қаралды 199 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 633 М.
Contiguous array | Leetcode #525
13:12
Techdose
Рет қаралды 52 М.
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 67 МЛН