Count Set Bits in First N natural numbers | Total Set Bits from 1 to N | Bit Manipulation

  Рет қаралды 69,501

Pepcoding

Pepcoding

Күн бұрын

Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we discuss the problem where we are required to find the set bits in the first N natural numbers using Bit Manipulation. In this problem,
1. You are given a number n.
2. You have to print the count of set bits of first n natural numbers.
To submit this question, click here: www.pepcoding....
For a better experience and more exercises, VISIT:
#bitmanipulation #bitmasking
Have a look at our result:
Follow us on our FB page: / pepcoding
Follow us on Instagram: / pepcoding
Follow us on LinkedIn: / pepcoding-education

Пікірлер: 205
@shivamagrawal2284
@shivamagrawal2284 3 жыл бұрын
Best explanation found after googling for more than 1hrs. Thank you
@Pepcoding
@Pepcoding 3 жыл бұрын
Glad it was helpful and If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
@sumitsharma6738
@sumitsharma6738 2 жыл бұрын
sahi me bro
@vardhanshah8843
@vardhanshah8843 3 жыл бұрын
The approach of subtraction to convert it into recursion was amazing.
@pramodtarpe
@pramodtarpe 3 жыл бұрын
You can use floor(log base 2(n)) to find 2 power x less than or equal to n.
@rishabsharma5307
@rishabsharma5307 3 жыл бұрын
Can you tell me why this is working?
@pramodtarpe
@pramodtarpe 3 жыл бұрын
@@rishabsharma5307 thats the purpose of log
@ayushpatel2171
@ayushpatel2171 3 жыл бұрын
@@rishabsharma5307 In simple words logarithm is "How many of one number do we multiply to get another number?" Eg. log2(8) means how many 2s we need to multiply in order to get 8. The answer is 3, so the (log base 2(8)) = 3. Now if we do log base 2 for a number which is not a power of 2 we will get a decimal value eg. log base 2(10) = 3.322. And if we use floor function with log base 2(10) we will get 3. And 2^3 is highest power of 2 less than 10.
@rishabsharma5307
@rishabsharma5307 3 жыл бұрын
@@ayushpatel2171 thnx
@uttkarshsingh3771
@uttkarshsingh3771 3 жыл бұрын
Finally got some worthy content to look for. Pepcoding is great 🔥🔥🔥
@ManishKumar-zk5ky
@ManishKumar-zk5ky Жыл бұрын
Best explanation on entire youtube sir Thanks a lot sir🤩
@b_1729-j8j
@b_1729-j8j Жыл бұрын
Ur explanation is better than GFG article. Thank you.
@rishijain8472
@rishijain8472 2 жыл бұрын
great explanation, best coding teacher on youtube 2nd is striver
@RajSingh-YrBTechChemicalEnggII
@RajSingh-YrBTechChemicalEnggII Жыл бұрын
best explanation, better than google
@Biradar_Ganesh
@Biradar_Ganesh Ай бұрын
Clean Explanation Sir!! Learned a new technique 🫡
@systemforge
@systemforge 3 жыл бұрын
Even though I don't know hindi.. i was able to understand it.. u r legend sir.. ❤️
@Pepcoding
@Pepcoding 3 жыл бұрын
Great, Keep learning, Keep growing and keep loving Pepcoding!😊
@stith_pragya
@stith_pragya Жыл бұрын
Thank You So Much Sumeet Sir.................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@PulkitMalhotra
@PulkitMalhotra 4 жыл бұрын
Thank you sir 1 number smz aa gya
@Pepcoding
@Pepcoding 4 жыл бұрын
Thankyou beta! Keep learning and keep loving Pepcoding😊 If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )
@arijitchowdhury4189
@arijitchowdhury4189 7 ай бұрын
Mszza ahh gaya sir ... Very good explanation. Thank you 🔥🔥🔥
@sreejitkar8044
@sreejitkar8044 21 күн бұрын
Best explanation sir ❤
@kashbhalla4363
@kashbhalla4363 2 жыл бұрын
about to complete this series you are too good sir.
@lalitkumarmehta1721
@lalitkumarmehta1721 3 жыл бұрын
The way u explain is pretty awesome, Thanks, bro.
@csgirl6895
@csgirl6895 3 жыл бұрын
Thank you so much, Sir. Your way of explaining the solution is awesome. After watching your videos most difficult questions also become easy for me.
@sonianarendramodi2605
@sonianarendramodi2605 3 жыл бұрын
I love you ❤❤❤💖💖💖
@srinivasak4087
@srinivasak4087 2 ай бұрын
very nicely explained Sir, Thanks!
@Devendrakr63
@Devendrakr63 3 жыл бұрын
Best place to learn hard topics in easy way. Great!!!!
@CrackMODS8
@CrackMODS8 Ай бұрын
one of the best soln
@neerajbhatia2008
@neerajbhatia2008 3 жыл бұрын
subscribing ur channel now. seriously man, you are too good. love your style of presentation and explanation, keep up the good work.
@Pepcoding
@Pepcoding 3 жыл бұрын
Thanks a ton!
@Pepcoding
@Pepcoding 3 жыл бұрын
For better experience, do checkout nados.pepcoding.com
@yashgupta9662
@yashgupta9662 2 жыл бұрын
A BIG Thanks for the explanation. 🙌
@Uniyaltech
@Uniyaltech 2 жыл бұрын
great explanation i was struggling from past two days.
@amreshgiri
@amreshgiri 3 жыл бұрын
If implementing in Python, don't use the bit shifting method for calculating power as it will give negative shift value error when the value of x
@kusalsaraf5464
@kusalsaraf5464 2 жыл бұрын
Thanks a lot, but can you tell the reason as it working for even number but not for odds
@bhailog_gaming_2000
@bhailog_gaming_2000 3 жыл бұрын
Thank you sir. This was the best explanation!
@UCVision93
@UCVision93 2 жыл бұрын
Clear cut explaination! Sir ji !
@vishalrajak2417
@vishalrajak2417 4 жыл бұрын
maje aa gaya bero solution dekkh ke
@Pepcoding
@Pepcoding 4 жыл бұрын
Shukriya ji😊 Bs aise he apna pyaar aur sahyog bnaye rkhe hum pr🙏
@prayagpatel9136
@prayagpatel9136 2 жыл бұрын
Great explanation!!!!!
@pradeepmondal4943
@pradeepmondal4943 4 жыл бұрын
Ekdaam faadu explaination ❤️❤️❤️❤️❤️❤️❤️ ..boleto jakhaas
@Pepcoding
@Pepcoding 4 жыл бұрын
If you like my efforts, I request a review g.page/Pepcoding/review?rc You can subscribe to our channel here kzbin.infoabout?view_as=subscriber For clearing your doubts, you can join our community on telegram t.me/pepcoding
@vikas5840
@vikas5840 2 жыл бұрын
thank you sir for such clear explanation
@dhruvpurwar6642
@dhruvpurwar6642 2 жыл бұрын
Seriously tooo good!!!!
@powerofbasics869
@powerofbasics869 4 жыл бұрын
Sir I've jst started the getting started wala portion.... And it is really really good.. Bohat deep explanation Kiya Gaya h .. Thank u so much sir... Hope this channel will become the highest subscribed channel for coding.....:)
@Pepcoding
@Pepcoding 4 жыл бұрын
aapko agar sahi lag rha hai to ek review daal dijie g.page/Pepcoding/review?rc
@057anmolkesarwani4
@057anmolkesarwani4 3 жыл бұрын
Best Soln explanation for this question
@utkrsh06
@utkrsh06 11 ай бұрын
You freaking genius!
@divyansh3266
@divyansh3266 3 жыл бұрын
Best explanation..
@BOSS-s6w8w
@BOSS-s6w8w Жыл бұрын
best explanation sir
@amolshrivastava7786
@amolshrivastava7786 3 жыл бұрын
Thank you sir, sab smjh m aa gaya after this solution ...also m phele pow(2,x) use karta tha but from now on i'll use (1x) to get 1/2^x.
@karunakeshari1058
@karunakeshari1058 3 жыл бұрын
Best explanation💯
@aravkhandelwal7043
@aravkhandelwal7043 3 жыл бұрын
very nice explanation
@Pepcoding
@Pepcoding 3 жыл бұрын
Thanks for liking. keep motivating, keep learning and keep loving Pepcoding😊
@myyoutubeisthis
@myyoutubeisthis 8 ай бұрын
Thank you very much sir
@vamsimadugula8524
@vamsimadugula8524 Жыл бұрын
It can be more easily solved using vector countBits(int n) { vector ans(n+1); for(int i=0;i
@mehirrouth7346
@mehirrouth7346 3 жыл бұрын
Amazing Explaination
@jolly_dollyyy
@jolly_dollyyy 3 жыл бұрын
excellent explanation sir
@harshitbajaj7549
@harshitbajaj7549 3 жыл бұрын
Thank you so much sir, best explanation
@karankesri3648
@karankesri3648 3 жыл бұрын
For finding the largest of power 2 you can use log2(n) which will give the result in O(1)
@harshalgarg1676
@harshalgarg1676 10 ай бұрын
int countSetBits(int n) { if(n==0) return 0; int x = log(n)/log(2); int power = pow(2,x); int ans = (power/2)*x + (n-power+1) + countSetBits(n-power); return ans; }
@kunalkheeva
@kunalkheeva 2 жыл бұрын
Thank you!
@yagizegemen7303
@yagizegemen7303 4 жыл бұрын
Sir the explanation is amazing. Appreciate your effort . Just a suggestion if you can also explain the Time and Space Complexity as well that would be great.
@Pepcoding
@Pepcoding 4 жыл бұрын
Ok beta. Soon
@binitrajshah9354
@binitrajshah9354 3 жыл бұрын
You explain great, kudos to you
@ROHITKADYANJI
@ROHITKADYANJI 3 жыл бұрын
Sumit sir 🤟🤟🤟 Sir explanation is too good 🙌💯
@Pepcoding
@Pepcoding 3 жыл бұрын
keep motivating, keep learning and keep loving Pepcoding😊
@manavpatnaik1948
@manavpatnaik1948 3 жыл бұрын
Very clear explanation. Thanks!
@Pepcoding
@Pepcoding 3 жыл бұрын
Glad it was helpful! Keep learning. And for better experience and well organised content visit nados.pepcoding.com
@raorao7329
@raorao7329 7 ай бұрын
loveed the soln
@rahuldwivedi4758
@rahuldwivedi4758 5 күн бұрын
Nice explanation but it's difficult to remember the formula. We could simply do as following. If the number if even, the result will be f(n/2) and if it's odd it will simply be 1 + f(n/2) take base cases as 0 for n == 0 and 1 for n == 1 const helper = function(n){ if(n === 0) return 0; if(n === 1) return 1; if (n % 2 === 0) return helper(parseInt(n/2)); return 1 + helper(parseInt(n/2)); } var countBits = function(n) { const ans = []; for(let i=0; i
@akhileshsonkar2061
@akhileshsonkar2061 3 жыл бұрын
Nice Explanation
@thejasnaveen4223
@thejasnaveen4223 2 жыл бұрын
Beautiful explanation ❤
@Pepcoding
@Pepcoding 2 жыл бұрын
Glad you liked it, for better experience and well organised content sign up on nados.io and start learning.
@AbhishekSingh-fc2rx
@AbhishekSingh-fc2rx 4 жыл бұрын
next level sir, its is very good explanation
@akshayjadhav4199
@akshayjadhav4199 2 жыл бұрын
super 😍😍😍😍
@manideep7148
@manideep7148 Жыл бұрын
Nice Explanation, It would be great if you had explained in english
@adityamhetras4799
@adityamhetras4799 3 жыл бұрын
Thanks you Sir!
@Pepcoding
@Pepcoding 3 жыл бұрын
Keep learning. And for better experience, visit nados.io, where you will get well curated content and career opportunities.
@mickyman753
@mickyman753 4 жыл бұрын
fabulous explaination , you made it so easy to understand , i saw the of the same problem on gfg and after hitting my head for several minutes and still not getting it ,saw your explanation ,love you sir
@Pepcoding
@Pepcoding 4 жыл бұрын
Thankyou beta! I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem. If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )
@mickyman753
@mickyman753 4 жыл бұрын
@@Pepcoding yes sir i will , and will wait for question solving videos ,they are so good
@surabhichoubey2987
@surabhichoubey2987 2 жыл бұрын
Nice sir
@SaumyaSharma007
@SaumyaSharma007 4 жыл бұрын
thanks a lot, sir...... you are really a great mentor. Your efforts justify Pepcoding's vision. (yesterday I watched your full interview......) huge respect for u SIR...Long live Sumeet SIR. Sir just want to ask u where can I get your full coding questions sheet (level 1,2). plzzzzz reply
@Pepcoding
@Pepcoding 4 жыл бұрын
I am glad. Keep learning. Keep supporting. Your kind words are the kind of motivation that truly help me in making more and more content. Especially, these days, not everybody is generous with motivating anybody either. It means a lot😊 You can find this content on our website under free resources section, as there the content is given in a very structured and sequencial order. and out of which level-1 is almost complete and level-2 is still underprocess.
@SaumyaSharma007
@SaumyaSharma007 4 жыл бұрын
@@Pepcoding Thank you Sir 🌟🙏 for the reply...... I am Big fan of yours 🙏
@sonalisharma9654
@sonalisharma9654 3 жыл бұрын
Very nice explanation sir :)
@ayushmishra9100
@ayushmishra9100 3 жыл бұрын
sir u are incredible!!!!
@vishalm784
@vishalm784 4 жыл бұрын
For getting largest power, we can use Math.floor(log2(num)) where log2 can be computed as Math.log(num)/Math.log(2);
@mickyman753
@mickyman753 4 жыл бұрын
but bitwise operations are faster
@shivammehta9661
@shivammehta9661 3 жыл бұрын
Nice Explanation..Keep making videos
@Pepcoding
@Pepcoding 3 жыл бұрын
Thank you, I will
@kritikakashyap384
@kritikakashyap384 4 жыл бұрын
Nice way of teaching
@Pepcoding
@Pepcoding 4 жыл бұрын
Thankyou beta! I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem. If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )
@samardhiman4365
@samardhiman4365 3 жыл бұрын
great explanation sir..
@dontgiveup5271
@dontgiveup5271 3 жыл бұрын
Great explanation sir !! 👏👏 But I wanna know what are the complexities
@akhilagrawal4559
@akhilagrawal4559 3 жыл бұрын
Really Loved it Sir!!!
@Pepcoding
@Pepcoding 3 жыл бұрын
Thank you for appreciating. The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos. So, keep motivating, keep learning and keep loving Pepcoding😊
@utkarsh_108
@utkarsh_108 3 жыл бұрын
thanks a lot
@labbusharma9865
@labbusharma9865 2 жыл бұрын
sir colour change kr liya kro please.... and best explanation
@samartajshaikh2601
@samartajshaikh2601 3 жыл бұрын
great explanation.
@ruchaharshe6489
@ruchaharshe6489 3 жыл бұрын
nicely explained...thanks!!
@Pepcoding
@Pepcoding 3 жыл бұрын
Glad it was helpful! and If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
@adarshjayadevan4379
@adarshjayadevan4379 4 жыл бұрын
Superb explanation. Thank you.
@Pepcoding
@Pepcoding 4 жыл бұрын
Glad to know that you liked the content and thank you for appreciating. The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos. If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )
@hiteshyadav2298
@hiteshyadav2298 4 жыл бұрын
I love the way you explained everything. Please use variable name in simplest form if possible. so that we can easily track flow of code. pls ignore words like - btill2x, msb2xton,etc
@Pepcoding
@Pepcoding 4 жыл бұрын
Thankyou beta, I am glad you liked it. I also hope that you are watching till end. If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )
@hiteshyadav2298
@hiteshyadav2298 4 жыл бұрын
@@Pepcoding ✔
@taukirlalwala2865
@taukirlalwala2865 3 жыл бұрын
thank u so much
@Pepcoding
@Pepcoding 3 жыл бұрын
Happy to help
@Prashantkumar-pn6qq
@Prashantkumar-pn6qq 4 жыл бұрын
Thanks Sir! Nice work.
@mannumichel1925
@mannumichel1925 4 жыл бұрын
maja aa gya sirji
@Pepcoding
@Pepcoding 4 жыл бұрын
Thankyou beta! I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem. If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )
@prakhyat2001
@prakhyat2001 2 жыл бұрын
thanks a lot :)))
@GoblinMaster30
@GoblinMaster30 3 жыл бұрын
Thanks a lot, your video helped me a lot.. I have optimized your code which runs even faster.. hope it helps others { int sum = 0; int x; while(n>0){ x = floor(log(n)/log(2)); sum += x * (1
@nomaanahmad3357
@nomaanahmad3357 3 жыл бұрын
thanks for the optimization! Appreciate your help
@sagarsattigeri9005
@sagarsattigeri9005 2 жыл бұрын
Will recurssion called only one? for all the cases
@saloniranka5609
@saloniranka5609 3 жыл бұрын
Going forward, can you please add time and space complexity in the end as well, it would be of more help to the existing content
@Devendrakr63
@Devendrakr63 3 жыл бұрын
Time complexity would be O(logn) , since we are dividing the problem into two parts in each call. And Stack Space take by the algo will also be O(logn), since there are log n recursive function calls.
@omprakashbarupal2129
@omprakashbarupal2129 2 жыл бұрын
@@Devendrakr63 ig TC is O(longn)^2, as recursive call has TC logn and in each call we we finding 2's power less than n has also TC as logn.
@omprakashbarupal2129
@omprakashbarupal2129 2 жыл бұрын
TC is O(longn)^2, as recursive call has TC logn and in each call we we finding 2's power less than n has also TC as logn.
@nomaanahmad3357
@nomaanahmad3357 3 жыл бұрын
You could have used floor(log2(n)) to find largest power of 2 less than or equal to n. Though log2() function is not available in java but there are multiple ways to do so.
@abhishekshankar1136
@abhishekshankar1136 3 жыл бұрын
same thing actually 1
@kaushiksarmah999
@kaushiksarmah999 3 жыл бұрын
thanks alot bro
@divyanshuchaudhari5416
@divyanshuchaudhari5416 4 жыл бұрын
if n==1: return 1 This condition should also be included,otherwise it gives negative shift value error for input 1. For this assignment : btill2x = x*(1
@Pepcoding
@Pepcoding 4 жыл бұрын
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
@beinghappy9223
@beinghappy9223 Жыл бұрын
Ig it gives random value and x being 0 , the final result will be 0 and so no effect
@rishabhrawat7943
@rishabhrawat7943 3 жыл бұрын
Sir what is the time complexity of this approach. O(logN) or O(logN*logN)?
@rohitshinde8478
@rohitshinde8478 4 жыл бұрын
thank you for this amazing explaination
@Pepcoding
@Pepcoding 4 жыл бұрын
Thankyou rohit for being aur constant supporter. Keep learning and keep loving Pepcoding😊
@itsyashh1308
@itsyashh1308 4 жыл бұрын
Sir I have a request and feedback, will you please show stats list on website that is really motivation to solve maximum number of questions in a day and questions serial number if possible ???
@Pepcoding
@Pepcoding 4 жыл бұрын
questions are in perfect order on portal. use portal instead of youtube. Yes, stats will be brought back.
@rahulbhatia3075
@rahulbhatia3075 4 жыл бұрын
Amazing explanation 🔥🔥
@Pepcoding
@Pepcoding 4 жыл бұрын
Glad you liked it
@aparajitakumari2936
@aparajitakumari2936 3 жыл бұрын
How is he using bitwise left shift operator to calculate power? Please explain.
@Pepcoding
@Pepcoding 3 жыл бұрын
For clearing all your doubts visit on nados.pepcoding.com Don't forget to follow us on Instagram instagram.com/pepcoding/
@Radaradababurao
@Radaradababurao 4 жыл бұрын
amazing content
@Pepcoding
@Pepcoding 4 жыл бұрын
Thankyou beta! I am glad you liked it. I also hope that you are watching till the end and trying to understand the what, how, and especially why of the problem. If you like our efforts, will you like to review us here - g.page/Pepcoding/review?rc
@payalsagar1808
@payalsagar1808 3 жыл бұрын
Wow!
@Pepcoding
@Pepcoding 3 жыл бұрын
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem. If you like our efforts, we request a review g.page/Pepcoding/review?rc You can subscribe to our channel here kzbin.infoabout?view_as=subscriber
@apoorvmendiratta9002
@apoorvmendiratta9002 2 жыл бұрын
class Solution: def countBits(self, N): x = self.largest(N) ans = 0 one = x*(1
@AmanSharma-vb5jl
@AmanSharma-vb5jl 3 жыл бұрын
What is the time complexity of this solution?
@RoshanSingh-kj4fy
@RoshanSingh-kj4fy 4 ай бұрын
Easy or effective
@AryanKumar-cc7ku
@AryanKumar-cc7ku 3 жыл бұрын
playlist of this video???
@abhimanyu8575
@abhimanyu8575 3 жыл бұрын
does this program uses time complexity of theta logn??
@omkark7118
@omkark7118 Жыл бұрын
Can anyone please help me, how do I use the same logic using BigInteger class? Thank you in advance
@satyakidas9593
@satyakidas9593 3 жыл бұрын
what is the time complexity of this?
It’s all not real
00:15
V.A. show / Магика
Рет қаралды 20 МЛН
Count total set bits | Problem of the Day 23/09/21 | Siddharth Hazra
22:03
GeeksforGeeks Practice
Рет қаралды 20 М.
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 336 М.