Happy Number

Easy

Topics
MathHash TableTwo Pointers

A number is happy if repeatedly replacing it by the sum of the squares of its digits eventually reaches 1. Numbers that never reach 1 loop forever. Return true if n is happy.

Example 1

1982681001null
Each node is the sum of the squares of the previous node’s digits. Here it reaches 1, so true.
Chain of digit-square sums starting from n.
Input:  n = 19
Output: true
Explanation: 1+81=82, 64+4=68, 36+64=100, 1 -> happy.

Example 2

24163758891454220cycle
Here it cycles, so false.
Input:  n = 2
Output: false

Constraints

  • 1 <= n <= 2^31 - 1
Editorial — intuition, approach, complexity and pitfalls

Intuition

The digit-square process is a sequence that either hits 1 or enters a cycle. Detecting "did we loop?" is exactly cycle detection — use a seen-set, or Floyd’s fast/slow pointers.

Approach

  1. Repeatedly compute the sum of squared digits.
  2. Keep a set of seen values; if you revisit one you are in a loop -> not happy.
  3. If you reach 1, it is happy.

Dry Run

n = 19
  19    1^2 + 9^2         = 82
  82    8^2 + 2^2         = 68
  68    6^2 + 8^2         = 100
  100   1^2 + 0^2 + 0^2   = 1
reached 1, so 19 is happy.  seen = {19, 68, 82, 100}
n = 2
  2     2^2               = 4
  4     4^2               = 16
  16    1^2 + 6^2         = 37
  37    3^2 + 7^2         = 58
  58    5^2 + 8^2         = 89
  89    8^2 + 9^2         = 145
  145   1^2 + 4^2 + 5^2   = 42
  42    4^2 + 2^2         = 20
  20    2^2 + 0^2         = 4
4 is already in seen, so the walk closed a loop -- not happy.
The second walk is the whole reason the set exists. That tail,
4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4, is an 8-value cycle, and every
unhappy number below 20000 ends up in exactly it -- there is no second trap to
worry about. Without seen the condition is just n != 1, which never fires, so the
failure mode is a hang rather than a wrong answer.
Note the sequence does not shrink monotonically: 68 -> 100 and 89 -> 145 both go
up. What keeps it bounded is that the largest 4-digit result is 9999 -> 324, so
nothing at or above 1000 can map to itself or higher. The walk is confined to a
small range and therefore must repeat.

Complexity

  • Time — O(log n) per step: digit counts shrink quickly; the number of distinct values before a cycle is bounded.
  • Space — O(log n): the set of seen values (or O(1) with Floyd’s).

Common Pitfalls

  • Something has to break the cycle. An unhappy number loops forever through the same digit-square sums, so dropping the seen set does not return a wrong answer -- it does not return at all, which a judge reports as a timeout rather than a failure.
  • Floyd's tortoise-and-hare works here too and costs O(1) memory instead of O(log n), which is worth saying out loud even if the set is what you write.

Full Solution — all languages

class Solution:
    def isHappy(self, n):
        seen=set()
        while n!=1 and n not in seen:
            seen.add(n)
            n=sum(int(d)**2 for d in str(n))
        return n==1
SensAI
Beta
Tap the mic to stop voice mode