Introduction

A Happy Number is a number that eventually becomes 1 after repeatedly replacing the number with the sum of squares of its digits.

The task is to:

  • repeatedly calculate square digit sum
  • detect cycle
  • determine whether number becomes 1

Example:

Input:19

Output:
True

Explanation:

1² + 9² = 82
8² + 2² = 68
6² + 8² = 100
1² + 0² + 0² = 1
Number becomes 1. So it is happy.

This problem is one of the most important applications of:

Fast and Slow Pointer Technique 

Constraints

1 <= n <= 2^31 - 1

Approach 1 : Brute Force (Using HashSet)

Explanations:

Explanation:

The idea is:

  • repeatedly calculate digit square sum
  • store visited numbers
  • if number repeats:
    cycle exists

Steps:

  1. Calculate digit square sum.
  2. Store current number.
  3. If number repeats:
    return false.
  4. If number becomes 1:
    return true.

This approach works but:

  • uses extra memory

So Fast & Slow Pointer solution is preferred.

Dry Run

Input:19
19 -> 82
82 -> 68
68 -> 100
100 -> 1

Happy Number

Practice :

Complexity Analysis :

Time Complexity:- O(log n)Explanation :
Digit processing is performed repeatedly.
Space Complexity:- O(log n)
Explanation :

HashSet stores visited numbers.

Approach 2 : Optimal Solution(Using Fast & Slow Pointer)

Explanations:

Explanation:

This is the most optimized and interview-preferred solution.

The idea is:

  • use slow and fast pointers
  • both represent numbers
  • slow moves one step
  • fast moves two steps

If both meet:

  • cycle exists

If number becomes 1:

  • happy number exists

Dry Run

Input:19

Slow:
19 -> 82 -> 68
Fast:
19 -> 68 -> 1
Fast becomes 1 Happy Number

Practice :

Complexity Analysis :

Time Complexity:- O(log n)Explanation :
Digits are processed repeatedly.
Space Complexity:- O(1)
Explanation :

No extra space is used.

Why This Problem is Important

This problem builds the foundation for:

  • Fast & Slow Pointer
  • Cycle detection
  • Number manipulation
  • Floyd’s Algorithm
  • Mathematical traversal

Real-World Applications

Cycle detection concepts are used in:

  • Cryptography
  • Number theory
  • Hashing systems
  • Data validation
  • Mathematical simulations

Common Beginner Mistakes

  • Incorrect digit extraction
  • Forgetting cycle detection
  • Infinite loops
  • Wrong square calculations
  • Incorrect fast pointer logic

Interview Tip

Interviewers often expect:

  • Fast & Slow Pointer optimization
  • Floyd Cycle Detection explanation
  • proper digit manipulation
  • clean mathematical traversal

Always explain:

  • why repeating numbers form a cycle
  • how fast and slow pointers detect cycles efficiently

Related Questions

  • Linked List Cycle
  • Detect Cycle II
  • Middle of Linked List
  • Floyd Cycle Detection
  • Number Transformation Problems

Final Takeaway

The Happy Number problem is one of the most important Fast & Slow Pointer problems.

It teaches:

  • cycle detection
  • mathematical traversal
  • Floyd’s Algorithm
  • digit processing

Understanding this problem builds a strong foundation for:

  •  advanced pointer techniques
  • mathematical algorithms
  • interview-level cycle detection problems.