Happy Number

Write an algorithm to determine if a number `n` is a happy number. A happy number is defined by the following process: starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (happy) or loops endlessly (not happy). Return `true` if n is happy, otherwise return `false`.

Input Format

n = a positive integer

Output Format

true if n is a happy number, false otherwise

Constraints

Examples

Example 1:

Input:

n = 19

Output:

true

Explanation:

19 -> 1^2+9^2=82 -> ... eventually reaches 1.

Example 2:

Input:

n = 2

Output:

false

Explanation:

2 enters a cycle that does not include 1.

Example 3:

Input:

n = 7

Output:

true

Explanation:

7 -> 49 -> 97 -> ... eventually reaches 1.

Loading...
Happy Number - Two Pointers DSA Problem