Happy number Problem

Happy number Problem — ExecCode Medium DSA Practice

Solve the Happy number problem on ExecCode. Free online medium DSA practice in Hashing. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Write an algorithm to determine if a number n is happy. Repeatedly replace n with the sum of the squares of its digits. If this process reaches 1, the number is happy; if it loops forever in a cycle that never includes 1, it is not. Return true if n is happy, and false otherwise.

Examples

Input n = 19; Output true. Input n = 2; Output false. Input n = 1; Output true

Constraints

1 ≤ n ≤ 2³¹ - 1

Practice Happy number free on ExecCode. Browse DSA problems, topic map, and placement guides.