← Hash Map

Pattern Recognition Drill

#120 — Happy Number

Easy Math & Geometry

The Problem

Determine if a number is 'happy': repeatedly sum the squares of its digits; if it reaches 1 it's happy.

What approach would you use?

Think about it before scrolling down.

Key Signals

Hash Map

Track seen numbers. If you see a repeat, it's a cycle → not happy. O(log n) per step.

Alt: Two Pointers

Floyd's slow/fast on the digit-sum function. Detects cycle without extra space.

Common Trap

Don't assume it always converges quickly. The cycle detection is essential to avoid infinite loops.

← #119