1/20/2024 0 Comments Happy number leetcode javascript![]() ![]() We keep repeating this process until we get to 58 then the digits are 5 and 8 and the sum of the squares is 89, can you guess the next number? The next number will be 145 and then so on until we come back to 58 again.ĭo you identify a pattern? If during the repetitions we come across a number that has already occurred once, we can conclude that the number we have received is not a happy number. For 89, the digits are 8 and 9, and the sum of the squares is 145.For 85, the digits are 8 and 5, and the sum of the squares is 89.For 29, the digits are 2 and 9, and the sum of the squares is 85.For 25, the digits are 2 and 5, and the sum of the squares is 29.For 50, the digits are 5 and 0, and the sum of the squares is 25.For 17, the digits are 1 and 7, and the sum of the squares is 50.How do we know when to stop the recalculation and conclude that the number given to us is not a happy number? Let us try to understand the intuition behind a happy number with the help of another example.Ĭonsider the number 17, and let us perform the calculations on it. But logically speaking, how do we identify if a number is a happy number or not? The most obvious method would be to keep replacing the number with the square of its digits.īut this raises a dilemma for us. Intuition behind a Happy NumberĪs we saw in the previous examples, a happy number is any number that can become one if we keep replacing it with the sum of the square of its digits. You can be happy too, you understand what happy numbers are now. Adding all the squares we get the number 1.ĭid you observe how we repeatedly replace the number with the sum of the square of its digit and after a few repetitions we end up with 1? Therefore 19 is a happy number. Now square the digits in 68, that is 6 and 8, and we get 36 and 64. Now the digits in 82 are 8 and 2, so we square the digits again, and we get 64 and 4. Now square the digits, and we get 1 and 81, add them, what do we get? We get 82. The digits in 19 are? 1 and 9, obviously. Too much technical jargon? Let us simplify this. What is a Happy Number?Ī happy number is defined as a number that can be replaced by the sum of the squares of its digit repeatedly and after some repetitions, it will yield the number 1. One of these special numbers is the Happy number. There are various types of special numbers that exist in today's world. If it is, then return false.Number theory is a very popular study of numbers. Check if the current sum of the square of digits of a number is in the HashSet. Calculate the current sum of squares based on the previous sum of squares. Iii) Run a while loop until the sum is not equal to 1. Ii) Calculate the sum of the square of digits of a number. ![]() Here are the following steps to solve this problem – In HashSet, we store the computed sum and at each iteration, we check whether a computed sum is present in HashSet or not. Why we are using HashSet? Because the lookup time in HashSet is O(1) as compared to other data structures. To solve this problem, I am using an additional data structure HashSet. We have to make sure that the process of doing the square of digits of a number and repeating the same process does not loop endlessly (In case the number is not a happy number). In this problem, the tricky part is to detect a loop. Sliding window Happy Number LeetCode Solution – Java Code Here is the list of programming questions on – How do we check whether a number n is happy or not? We have discussed the problem statement with the help of multiple examples. Step 4: 1^2 + 0^2 + 0^2 = 1 (The number ends at 1, So it’s a happy number). Those numbers for which this process ends in 1 are happy numbers. ![]() If it loops endlessly in a cycle which does not include 1 then it’s not a happy number. If it’s equal to 1 then it’s a happy number. Take any positive integer, and replace the number with the sum of the squares of its digits. Repeat the process until the number equals 1 (where it will stay). Given a number n, we have to write a code to check whether a number is happy or not. ![]() In this tutorial, I am going to discuss a very interesting problem to determine if a number is happy or not. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |