← All problems

Happy Number (cycle detection)

Easy mathtwo-pointershash maps LeetCode ↗
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.

Hints (0/3 revealed)

Log in to reveal hints progressively.

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.