Skip to content

Commit 5e7f485

Browse files
Create 0202-Happy-Numbers.md
Add solution to the Leetcode problem 0202 Happy Numbers
1 parent 35910a1 commit 5e7f485

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
---
2+
id: Happy-Number
3+
title: Happy Number
4+
sidebar_label: Happy Number
5+
tags:
6+
- Math
7+
- Hash Table
8+
- Two Pointers
9+
---
10+
11+
## Problem Description
12+
13+
| Problem Statement | Solution Link | LeetCode Profile |
14+
| :------------------------------------------------------ | :------------------------------------------------------------------------- | :------------------------------------------------------ |
15+
| [Happy-Number](https://leetcode.com/problems/Happy-Number/description/) | [Happy-Number Solution on LeetCode](https://leetcode.com/problems/Happy-Number/solutions/) | [Nikita Saini](https://leetcode.com/u/Saini_Nikita/) |
16+
17+
## Problem Description
18+
19+
A happy number is a number defined by the following process:
20+
Starting with any positive integer, replace the number by the sum of the squares of its digits.
21+
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
22+
Those numbers for which this process ends in 1 are happy.
23+
24+
Given a positive integer `n`, determine if it is a happy number.
25+
26+
### Example 1:
27+
28+
Input: `n = 19`
29+
Output: `true`
30+
Explanation:
31+
1^2 + 9^2 = 82
32+
8^2 + 2^2 = 68
33+
6^2 + 8^2 = 100
34+
1^2 + 0^2 + 0^2 = 1
35+
36+
### Example 2:
37+
38+
Input: `n = 2`
39+
Output: `false`
40+
Explanation:
41+
2^2 = 4
42+
4^2 = 16
43+
1^2 + 6^2 = 37
44+
3^2 + 7^2 = 58
45+
5^2 + 8^2 = 89
46+
8^2 + 9^2 = 145
47+
1^2 + 4^2 + 5^2 = 42
48+
4^2 + 2^2 = 20
49+
2^2 + 0^2 = 4 (cycle repeats endlessly)
50+
51+
## Constraints
52+
53+
- `1 <= n <= 2^31 - 1`
54+
55+
## Approach
56+
57+
To determine if a number `n` is a happy number:
58+
1. Use a set to keep track of numbers seen during the process to detect cycles.
59+
2. Repeat the process of replacing `n` by the sum of the squares of its digits until `n` becomes 1 or a cycle is detected (a number repeats).
60+
3. If `n` becomes 1, return true; otherwise, return false.
61+
62+
## Solution in Python
63+
64+
```python
65+
def isHappy(n: int) -> bool:
66+
seen = set()
67+
while n != 1 and n not in seen:
68+
seen.add(n)
69+
n = sum(int(digit)**2 for digit in str(n))
70+
return n == 1
71+
```
72+
73+
## Solution in Java
74+
```java
75+
import java.util.HashSet;
76+
import java.util.Set;
77+
78+
class Solution {
79+
public boolean isHappy(int n) {
80+
Set<Integer> seen = new HashSet<>();
81+
while (n != 1 && !seen.contains(n)) {
82+
seen.add(n);
83+
int sum = 0;
84+
while (n > 0) {
85+
int digit = n % 10;
86+
sum += digit * digit;
87+
n /= 10;
88+
}
89+
n = sum;
90+
}
91+
return n == 1;
92+
}
93+
}
94+
```
95+
96+
## Solution in C++
97+
```cpp
98+
class Solution {
99+
public:
100+
bool isHappy(int n) {
101+
unordered_set<int> seen;
102+
while (n != 1 && seen.find(n) == seen.end()) {
103+
seen.insert(n);
104+
int sum = 0;
105+
while (n > 0) {
106+
int digit = n % 10;
107+
sum += digit * digit;
108+
n /= 10;
109+
}
110+
n = sum;
111+
}
112+
return n == 1;
113+
}
114+
};
115+
```
116+
117+
## Solution in C
118+
```c
119+
#include <stdbool.h>
120+
#include <stdio.h>
121+
#include <stdlib.h>
122+
123+
bool isHappy(int n) {
124+
int seen[1000] = {0};
125+
while (n != 1 && !seen[n]) {
126+
seen[n] = 1;
127+
int sum = 0;
128+
while (n > 0) {
129+
int digit = n % 10;
130+
sum += digit * digit;
131+
n /= 10;
132+
}
133+
n = sum;
134+
}
135+
return n == 1;
136+
}
137+
```
138+
139+
## Solution in JavaScript
140+
```js
141+
var isHappy = function(n) {
142+
let seen = new Set();
143+
while (n !== 1 && !seen.has(n)) {
144+
seen.add(n);
145+
n = String(n).split('').reduce((sum, digit) => sum + digit * digit, 0);
146+
}
147+
return n === 1;
148+
};
149+
```
150+
151+
## Step-By_Step Algorithm
152+
1. Initialize an empty set `seen` to keep track of numbers encountered.
153+
2. While `n` is not 1 and `n` is not in `seen`:
154+
- Add `n` to `seen`.
155+
- Compute the sum of the squares of its digits for `n`.
156+
- Update `n` to this computed sum.
157+
3. Check if `n` equals 1. If yes, return true (it is a happy number). If no, return false (it is not a happy number).
158+
159+
## Conclusion
160+
The provided solutions use a similar approach to solve the happy number problem across different programming languages. They efficiently detect cycles and determine if a number eventually leads to 1 or loops endlessly. The use of a set ensures that the algorithm runs in linear time relative to the number of digits in `n`, making it suitable for the given constraints.
161+

0 commit comments

Comments
 (0)