Write an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Return true if n is a happy number, and false if not.
Example 1:
Input: n = 19 Output: true Explanation: 12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 + 02 = 1
Example 2:
Input: n = 2 Output: false
Constraints:
1 <= n <= 231 - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you start with a number and repeatedly replace it with the sum of the squares of its digits, hoping to eventually reach the number one. The brute force approach simply follows this exact process step by step, hoping it stops at one or keeps running forever.
Here's how the algorithm would work step-by-step:
def is_happy(starting_number):
seen_numbers = set()
current_number = starting_number
while current_number != 1:
# We maintain a history to detect infinite loops since unhappy numbers always cycle
if current_number in seen_numbers:
return False
seen_numbers.add(current_number)
next_sum_of_squares = 0
number_to_process = current_number
while number_to_process > 0:
digit = number_to_process % 10
next_sum_of_squares += digit * digit
number_to_process //= 10
current_number = next_sum_of_squares
# Reaching one means the transformation process successfully completed its mission
return TrueTo determine if a number is happy, we repeatedly replace it with the sum of the squares of its digits until we either reach the goal of one or get stuck in a repeating loop. The optimal strategy relies on a classic chase game between two moving paces to detect if we are trapped in a circle without needing to remember every number we have ever visited.
Here's how the algorithm would work step-by-step:
class Solution:
def get_next_number(self, current_number):
total_sum_squares = 0
while current_number > 0:
current_digit = current_number % 10
total_sum_squares += current_digit ** 2
current_number //= 10
return total_sum_squares
def isHappy(self, initial_number: int) -> bool:
# Tortoise and hare pointers allow cycle detection without extra memory.
slow_runner = initial_number
fast_runner = self.get_next_number(initial_number)
# Moving pointers at different speeds guarantees they meet if a cycle exists.
while fast_runner != 1 and slow_runner != fast_runner:
slow_runner = self.get_next_number(slow_runner)
fast_runner = self.get_next_number(self.get_next_number(fast_runner))
# Reaching one means all digits squared eventually reduced to the success target.
return fast_runner == 1| Case | How to Handle |
|---|---|
| Input is 1 | The algorithm should immediately return true since 1 is defined as a happy number. |
| Input enters a non-happy cycle (such as starting with 4) | A cycle detection mechanism like a HashSet or Fast and Slow pointers prevents infinite loops by catching previously seen sums. |
| Numbers with trailing zeros like 10 or 100 | Digit extraction using modulo and division correctly isolates non-zero digits and includes zero squares in the sum. |
| Maximum 32-bit signed integer inputs | Since the sum of squares of digits for any large integer quickly reduces to a much smaller number below 810, integer overflow is not an issue. |
| Inputs consisting of all 9s such as 99 | The algorithm correctly computes the sum of squares for each digit and transitions to the next step in the sequence. |