← All problems
Happy Number (cycle detection)
Determine whether a number is 'happy': repeatedly replace it by the sum of the squares of its digits; if the process reaches 1 it is happy, if it loops forever it is not. Detect the loop efficiently.
Complexity
Time O(log n) per step, Space O(1) Floyd
Common pitfalls
- Infinite loop without cycle detection
- Digit extraction bugs
Your solution
Log in to save your code and track progress.