2Brute2Best
🔥0

Happy Number

Easy·LeetCode #202 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

Start with a positive integer n. Replace it with the sum of the squares of its digits, and keep doing that.

If this process ever reaches 1, the number is happy. If it never reaches 1 (it keeps going round the same numbers forever), it is not happy.

Return true if n is happy, otherwise false.

Input

A positive integer n.

Output

true if repeating "sum of squared digits" reaches 1, otherwise false.

Example 1
Input: n = 19
Output: true

1² + 9² = 82 → 8² + 2² = 68 → 6² + 8² = 100 → 1² + 0² + 0² = 1. We reached 1.

Example 2
Input: n = 2
Output: false

2 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 → … The numbers repeat from 4 and never reach 1.

Constraints
  • • 1 ≤ n ≤ 2^31 − 1
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“repeat the process”
→ Each value produces exactly one next value → a hidden linked list.
“loops endlessly in a cycle”
→ The statement itself says the walk can cycle → cycle detection.
“reaches 1”
→ 1 maps to itself, so "reaches 1" is just "the cycle it ends in is {1}".
03

Approach Complexity Ladder

Brute Force — set of seen numbersO(log N)O(log N)

Store every number produced; a repeat before 1 means not happy

Optimal — Floyd's cycle detection on numbersO(log N)O(1)

slow = next(slow), fast = next(next(fast)); stop at 1 or when they meet

Brute Force Intuition

Keep producing the next number. Put each one into a hash set. If we reach 1, return true. If we produce a number that is already in the set, the sequence will repeat the same numbers forever without reaching 1, so return false.

Recognition rule for this sub-pattern:

Fast moves 2, slow moves 1. If there is a loop, fast catches slow. Reset one to head to find the loop start.
Remember this · Linked List — Fast & Slow
Trigger
Cycle detection, middle of list, "n-th from end", two lists meeting, repeating sequences.
Think
If one pointer moves faster or starts earlier, where will they meet?
Key question
What speed or gap makes the meeting point tell me the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Linked List — Fast & Slow translate into production architectures and low-level memory mechanics.