Introduction
A Happy Number is a number that eventually reaches 1 when replaced repeatedly by the sum of the squares of its digits.
If the process enters a cycle that does not include 1, the number is not a happy number.
This problem helps in understanding:
- Digit extraction
- Hashing
- Cycle detection
- Set usage
- Number manipulation
Problem Statement
Given an integer n, determine whether it is a happy number.
A number is happy if repeatedly replacing it with the sum of the squares of its digits eventually results in 1.
Return true if the number is happy; otherwise, return false.
Example
Input:
n = 19
Output:
true
Explanation:
19 → 1² + 9²
→ 1 + 81
→ 82
→ 8² + 2²
→ 64 + 4
→ 68
→ 6² + 8²
→ 36 + 64
→ 100
→ 1² + 0² + 0²
→ 1Therefore, 19 is a happy number.
Input:n = 2
Output:
false
Explanation:
2 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4
The sequence enters a cycle and never reaches 1.
Therefore, 2 is not a happy number.
Constraints
- 1 ≤ n ≤ 2³¹ - 1
Approach 1: Brute Force
Explanation
The brute force approach repeatedly calculates the sum of the squares of the digits.
To detect whether the process is stuck in a cycle, we can continue generating values until either:
- The value becomes 1, or
- A known repeating cycle is reached.
For this problem, the cycle can be detected by checking whether the sequence reaches 4.
Steps
- Calculate the sum of the squares of the digits.
- Replace the number with this sum.
- Repeat the process.
- If the number becomes 1, return true.
- If the number becomes 4, return false.
Dry Run
Input:n = 19
19 → 82
82 → 68
68 → 100
100 → 1
The number reaches 1.
Final Result:
true
Brute Force Code
Complexity Analysis
Time Complexity: O(log n) per digit-sum calculation, with a bounded number of iterations after the value becomes small.
Space Complexity: O(1)
Only a constant amount of extra space is used.
Approach 2: Optimized Solution
Explanation
The optimized approach uses a Set to detect repeated values.
Instead of relying on the known cycle value 4, we store every number produced during the process.
If a number appears again, a cycle has been detected.
Steps
- Create an empty Set.
- While n is not 1:
- If n already exists in the Set, a cycle is detected.
- Add n to the Set.
- Replace n with the sum of the squares of its digits.
- Return true if n becomes 1.
Dry Run
Input:n = 2
Start:
Set = {}
2 → add to Set
Set = {2}
4 → add to Set
Set = {2, 4}
16 → add to Set
Set = {2, 4, 16}
37 → add to Set
Set = {2, 4, 16, 37} ...
20 → add to Set
4 → already exists in Set
Cycle detected.
Final Result:
false
Optimized Code
Complexity Analysis
Time Complexity: O(log n) per digit-sum calculation, with a bounded number of generated states.
Space Complexity: O(log n)
The Set stores previously encountered values.
Edge Cases
- n = 1: It is already a happy number.
- n = 19: Eventually reaches 1.
- n = 2: Enters a cycle and is not happy.
- Single-digit number: Check whether repeated digit-square calculation reaches 1.
- Repeated value: Indicates that a cycle has been detected.
Why This Problem is Important
This problem helps in understanding:
- Hash Sets
- Cycle detection
- Digit manipulation
- Modulo and division
- Repeated-state problems
Real-World Applications
Cycle detection concepts are useful in:
- State-based algorithms
- Graph and linked-list problems
- Repeated process detection
- Caching previously visited states
- Algorithm optimization
Common Mistakes
- Forgetting to detect cycles.
- Calculating the digit square incorrectly.
- Forgetting to update the number after each iteration.
- Confusing the sum of digits with the sum of squared digits.
- Using an infinite loop without a cycle-detection condition.
Interview Tips
The key observation is that the sequence can repeat.
A common solution is to use a Set to remember previously seen values.
If the same value appears again, the process is stuck in a cycle and the number is not happy.
Related Questions
- Introduction to Set
- Introduction to Map
- Count Digits
- Reverse Number
- Palindrome Number
Final Takeaway
The Happy Number problem combines digit manipulation with cycle detection.
The brute force approach can use the known cycle condition, while the optimized hashing approach uses a Set to detect repeated states. This makes the problem a useful introduction to applying hashing techniques to number-based problems.