LeetCode Grinding
Algorithms2 min read

Leetcode - Happy Number Solution

Use cycle detection to check whether repeated sums of squared digits reach 1.

The Happy Number problem asks whether repeatedly squaring and summing a number's digits eventually reaches 1. If the sequence repeats a value without reaching 1, the number is not happy.

A Set records which numbers have appeared. Fast and slow pointers detect the same repetition with O(1) extra space.

The sequence

A number is happy if I can repeatedly:

  1. Take its digits
  2. Square each digit
  3. Sum the squares
  4. Repeat until I get 1

If I never get 1 and instead enter a cycle, it is not happy.

This version stores each result in a Set and stops at 1 or a repeated value:

javascript
function isHappyNaive(n) {
  let seen = new Set();

  while (n !== 1 && !seen.has(n)) {
    seen.add(n);
    n = sumOfSquares(n);
  }

  return n === 1;
}

function sumOfSquares(n) {
  let sum = 0;
  while (n > 0) {
    let digit = n % 10;
    sum += digit * digit;
    n = Math.floor(n / 10);
  }
  return sum;
}

This detects both outcomes, but the set grows with the sequence.

Detecting a cycle

The sequence has the same shape as a linked list: each number points to the next result of sumOfSquares. A happy sequence reaches 1. Any other sequence eventually enters a cycle, so fast and slow pointers can distinguish the two cases without storing every value.

The implementation starts fast one step ahead:

javascript
function isHappy(n) {
  let slow = n;
  let fast = sumOfSquares(n); // Fast starts one step ahead

  // Keep going until fast reaches 1 or slow catches up to fast
  while (fast !== 1 && slow !== fast) {
    slow = sumOfSquares(slow); // Move slow one step
    fast = sumOfSquares(sumOfSquares(fast)); // Move fast two steps
  }

  return fast === 1; // If fast reached 1, it's happy!
}

function sumOfSquares(n) {
  let sum = 0;
  while (n > 0) {
    let digit = n % 10;
    sum += digit * digit;
    n = Math.floor(n / 10);
  }
  return sum;
}

For n = 19, the pointers move as follows:

  • slow = 19, fast = sumOfSquares(19) = 82
  • slow = 82, fast = sumOfSquares(sumOfSquares(82)) = sumOfSquares(68) = 100
  • slow = 68, fast = sumOfSquares(sumOfSquares(100)) = sumOfSquares(1) = 1
  • fast === 1, so the function returns true.

If the sequence enters a cycle without 1, slow and fast eventually meet inside it. If it reaches 1, the loop stops with fast === 1.

Each call to sumOfSquares processes the digits of its input. The pointer version keeps a fixed number of variables; the Set version stores every distinct result it visits.

Computing each next value with sumOfSquares lets the same cycle detection work without building a linked list.

Filed under

Thanks for reading

What do you think?

Leave a reaction or a comment below.

Loading reactions…

Share this post

Loading comments...

Related posts

All writing