Happy Number (LeetCode 202) | Full solution with with easy explanation | Study Algorithms

  Рет қаралды 28,290

Nikhil Lohia

2 жыл бұрын

Logically and mathematically, there is no definition of a happy number. According to the problem statement, if you keep on adding the square of all digits of a number to reach “1”, the original number will be a happy number. Watch the video to understand how you can get stuck in a loop and how can you break out of it efficiently. All along with visuals, examples and a dry-run of code in JAVA.
Actual problem on LeetCode: leetcode.com/problems/happy-number/
Chapters:
00:00 - Intro
00:38 - Problem statement and description
03:33 - How to approach the problem?
05:03 - Solving for efficiency
07:30 - Dry-run of Code
10:31 - Final Thoughts
📚 Links to topics I talk about in the video:
Brute Force Method: kzbin.info/www/bejne/oZW3oYigmZxkfZo
Recursion Algorithmic Paradigm: kzbin.info/www/bejne/fIW3eZ6jo9utoq8
Playlist on Algorithmic Paradigms: kzbin.info/aero/PLFdAYMIVJQHOvoD4gQz7CwEhK3pAXWLdX
📘 A text based explanation is available at: studyalgorithms.com
Code on Github: github.com/nikoo28/java-solutions/blob/master/src/main/java/leetcode/easy/HappyNumber.java
Test-cases on Github: github.com/nikoo28/java-solutions/blob/master/src/test/java/leetcode/easy/HappyNumberTest.java
📖 Reference Books:
Starting Learn to Code: amzn.to/36pU0JO
Favorite book to understand algorithms: amzn.to/39w3YLS
Favorite book for data structures: amzn.to/3oAVBTk
Get started for interview preparation: amzn.to/39ysbkJ
🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalgorithms
🎥 My Recording Gear:
Recording Light: amzn.to/3pAqh8O
Microphone: amzn.to/2MCX7qU
Recording Camera: amzn.to/3alg9Ky
Tablet to sketch and draw: amzn.to/3pM6Bi4
Surface Pen: amzn.to/3pv6tTs
Laptop to edit videos: amzn.to/2LYpMqn
💻 Get Social 💻
Follow on Facebook at: studyalgos
Follow on Twitter at: studyalgorithms
Follow on Tumblr at: studyalgos.tumblr.com/
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview

Пікірлер: 52
@RohitPal-lz1wf
@RohitPal-lz1wf Ай бұрын
Bro you are really underrated I saw a lot of people having more subscribers but they simply waste time. You are spot on
@cse9003
@cse9003 2 ай бұрын
Man, i really found the alogrithm treasure and its YOU, thank you bro
@helsinkired8523
@helsinkired8523 7 ай бұрын
Hey, great video. So the basic intuition is that the pattern repeats (i did not do enough iterations to understand that the pattern is repeating) and therefore we can use the same algorithm which we use to detect if a loop exists in a linked list. Rabbit-hare or floyd marshal algorithm. This way we need not use set to store the already encountered numbers.
@nikoo28
@nikoo28 7 ай бұрын
Yes, exactly
@abandubey
@abandubey 6 ай бұрын
How is the space complexity O(1) if we have used an extra space by creating a HashSet? Can you explain?
@nikoo28
@nikoo28 5 ай бұрын
You will never have the sum of digits that is more than 1000. That means no matter the input size, the hashset has a maximum size. Hence constant space
@RishabhSharma-p9o
@RishabhSharma-p9o 3 ай бұрын
sir you are really amazing , your teaching style is perfect
@rohitkatiyar2340
@rohitkatiyar2340 2 жыл бұрын
Your explanations are very good. Keep up the good work! Can u please make a video on Leetcode problem 29( Divide Two Integers)
@nikoo28
@nikoo28 2 жыл бұрын
You can try to solve that problem, but as per my experience it will not be helpful as that problem does not enhance or even improve your problem solving skills. You can skip it if you like. Look at the number of dislikes the problem has. As a rule of thumb, first try to cover the most liked questions. I can soon make a video on how to use LeetCode correctly.
@JohnWickea
@JohnWickea Жыл бұрын
@@nikoo28 did you made that video ?? if yes please share the link
@nikoo2805
@nikoo2805 Жыл бұрын
@@JohnWickea How to use LeetCode and pick problems to solve effectively in #2023 kzbin.info/www/bejne/hpTSna2rd5KZiMU
@sonalagrawal-up3bq
@sonalagrawal-up3bq Ай бұрын
I did not understand, why is it guaranteed to have a cycle if not summing up to 1?
@chiraggoel5977
@chiraggoel5977 Жыл бұрын
but GFG is showing that it's time complexity is n*log(n). Because finding time complexity of finding the digit in any number is log(n). Can you please help in finding the time complexity .
@wish7479
@wish7479 2 жыл бұрын
Sir, Can you make more videos on bit manipulations ? Like xor,And ,Or... How to use this efficiently and when to use?
@nikoo28
@nikoo28 2 жыл бұрын
I will soon start a segment on bitwise operations.
@RishiJain-yk9xr
@RishiJain-yk9xr Ай бұрын
Thanks bhai
@iiju8212
@iiju8212 Жыл бұрын
can anyone explain why is it guaranteed to be cycle? why cant it not continue till infinity ?
@nikoo28
@nikoo28 Жыл бұрын
because the sum's you can get are finite...no matter what you add up, you will get a sum between 2-99. If any of the number is present in the hashset, that means it will become a cycle. Try to think of a case when it happens infinite times?? can you come up with one?
@BCAdudes
@BCAdudes 7 ай бұрын
Actually I wanted to ask we don't have to use any header file in leetcode. I am trying to run the program but I am getting error everytime in leetcode but the same code is working in TURBO C.
@nikoo28
@nikoo28 7 ай бұрын
leetcode already imports some libraries for you
@AadeshKulkarni
@AadeshKulkarni 8 ай бұрын
Nicely explained, baba!
@NiteshMaurya1111
@NiteshMaurya1111 2 ай бұрын
thank you sir i understood clearly
@hawadrammeh8870
@hawadrammeh8870 Жыл бұрын
hi! I believe the Time complexity for the solution is O (log n ) not O(1), could you double check that?
@nikoo28
@nikoo28 Жыл бұрын
why do you think the time complexity will be O(log n)? we do not do any binary operations. O(1) actually means constant time. So we know that no matter what is the test case, it will always finish in a certain amount of time.
@abhishek6575
@abhishek6575 8 ай бұрын
Sir what i i do same problem using vector and using "find "prebuilt function in vector to solve this .what is best to do using hashset or vector
@nikoo28
@nikoo28 8 ай бұрын
both are perfect to use
@AtharvaKulkarni-lw4sv
@AtharvaKulkarni-lw4sv Жыл бұрын
Can you please explain while(true) statement?
@nikoo28
@nikoo28 Жыл бұрын
that just means an infinite loop.
@bendivanitha7211
@bendivanitha7211 Жыл бұрын
but here we are using inner while loop calculate sum of digits which takesO(K) then plz explain how timecomplexity will be O(1) sir.
@nikoo28
@nikoo28 Жыл бұрын
When you say O(K), K is the length of the input integer. Now think about it, the maximum number of digits are 9, as defined by the problem statement. So you know that it will not be greater than O(9), and that means a constant time. Hence O(1)
@bendivanitha7211
@bendivanitha7211 Жыл бұрын
@@nikoo28 yeah thanks you so much sir
@wish7479
@wish7479 2 жыл бұрын
Can you make a video on leetcode 120- triangle. ?
@bhumikabansal6022
@bhumikabansal6022 Жыл бұрын
we are using hashset here . doesn't it will be taking extra space???????
@nikoo28
@nikoo28 Жыл бұрын
it will be constant space. watch at 10:15
@bhumikabansal6022
@bhumikabansal6022 Жыл бұрын
@@nikoo28 thanks for answering
@kunalkheeva
@kunalkheeva Жыл бұрын
why is while(n!=0) instead of while(n!=1)?
@Nishi-Tiwari
@Nishi-Tiwari Жыл бұрын
Do you understood now? If yes, please tell me. Thanks.
@kunalkheeva
@kunalkheeva Жыл бұрын
@@Nishi-Tiwari updated one class Solution { public boolean isHappy(int n) { //set to track the continuous condition HashSet set = new HashSet(); //infinite loop until the conditions match while(true){ int sum =0; while(n != 0){ sum += Math.pow(n%10,2); n= n/10; } // given condition if(sum ==1){ return true; } //so new n is the sum we have gotten after the squares n=sum; // else if there is a repetition then return false, which means it will run infinitely, otherwise add it to the set. if(set.contains(n)){ return false; } else{ set.add(n); } } } }
@mohammedilyas8824
@mohammedilyas8824 2 жыл бұрын
Sir pls make interview based problems
@nikoo28
@nikoo28 2 жыл бұрын
I try to cover interview based problems and most liked problems on LeetCode. If you want any specific questions, let me know :)
@mohammedilyas8824
@mohammedilyas8824 2 жыл бұрын
@@nikoo28 sir pls cover linked list,stack,que,recursion quesns
@Ozoocats
@Ozoocats Жыл бұрын
keep going but please the voice is not clear enough, try another microphone
@nikoo28
@nikoo28 Жыл бұрын
It is fixed in the newer videos. :)
@Ozoocats
@Ozoocats Жыл бұрын
@@nikoo28 ok thankful I'll watch them
@amit9790singh
@amit9790singh 11 ай бұрын
Thanku sir
@subee128
@subee128 9 ай бұрын
Thanks
@kunalkheeva
@kunalkheeva Жыл бұрын
Great!
@mdshayanurrahman78
@mdshayanurrahman78 18 күн бұрын
Sir please make video in hindi as well
@Priya-le1li
@Priya-le1li 2 жыл бұрын
Nice👍🏼
@gowthamselvaraj7793
@gowthamselvaraj7793 9 ай бұрын
class Solution { public boolean isHappy(int n) { boolean ans; while(true){ int temp=n; int sum=0; while(temp>0){ int r=temp%10; sum+=Math.pow(r,2); temp/=10; } n=sum; if(sum
@sarthakjoshi21
@sarthakjoshi21 Жыл бұрын
op explanation
@shivashukla7894
@shivashukla7894 Ай бұрын
please use good mike