304 North Cardinal St.
Dorchester Center, MA 02124

# Happy Number LeetCode Solution

## Problem – Happy Number

Write an algorithm to determine if a number `n` is happy.

happy number is a number defined by the following process:

• Starting with any positive integer, replace the number by the sum of the squares of its digits.
• Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
• Those numbers for which this process ends in 1 are happy.

Return `true` if `n` is a happy number, and `false` if not.

Example 1:

``````Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1``````

Example 2:

``````Input: n = 2
Output: false``````

Constraints:

• `1 <= n <= 231 - 1`

### Happy Number LeetCode Solution in Java

``````public boolean isHappy(int n) {
Set<Integer> inLoop = new HashSet<Integer>();
int squareSum,remain;
squareSum = 0;
while (n > 0) {
remain = n%10;
squareSum += remain*remain;
n /= 10;
}
if (squareSum == 1)
return true;
else
n = squareSum;

}
return false;

}
``````

### Happy Number LeetCode Solution in Python

``````def isHappy(self, n):
mem = set()
while n != 1:
n = sum([int(i) ** 2 for i in str(n)])
if n in mem:
return False
else:
else:
return True``````

### Happy Number LeetCode Solution in C++

``````class Solution {
public:
bool isHappy(int n) {
unordered_set<int> tmp;

while(n != 1)
{
if(tmp.find(n) == tmp.end())
tmp.insert(n);
else
return false;

int sum = 0;
while(n != 0)
{
sum += pow(n % 10,2);
n = n / 10;
}

n = sum;
}

return true;
}
};
``````
##### Happy Number LeetCode Solution Review:

In our experience, we suggest you solve this Happy Number LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Happy Number LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Happy Number LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions
{"ft_lang_label":"__label__en","ft_lang_prob":0.69227344,"math_prob":0.9814616,"size":1766,"snap":"2022-40-2023-06","text_gpt3_token_len":491,"char_repetition_ratio":0.10045403,"word_repetition_ratio":0.006153846,"special_character_ratio":0.31540203,"punctuation_ratio":0.1570248,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9915573,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-01-31T14:11:16Z\",\"WARC-Record-ID\":\"<urn:uuid:6806d038-6730-4001-a269-575af86ccfbc>\",\"Content-Length\":\"98614\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:02159b8b-8fec-439e-8ff2-f1d0b051f998>\",\"WARC-Concurrent-To\":\"<urn:uuid:7b4a76ad-8006-4aed-9108-765ddf334941>\",\"WARC-IP-Address\":\"104.21.78.155\",\"WARC-Target-URI\":\"https://queslers.com/happy-number-leetcode-solution/\",\"WARC-Payload-Digest\":\"sha1:N3HYRZL3FWISQM2QVBUVPHB4ZTVLYO2U\",\"WARC-Block-Digest\":\"sha1:JKEB73ZT27QHPPVLTDJ7OYEA7HBNXD2A\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499871.68_warc_CC-MAIN-20230131122916-20230131152916-00245.warc.gz\"}"}