Happy Number

Read the lesson → understand the pattern → then code. Tutor works offline (built-in teacher); optional Cursor/Gemini keys upgrade chat quality.

Topic refresher: Hashing — Pay Space, Buy Time

Replace nested loops with unordered_map / unordered_set lookups.

unordered_map<int,int> mp;
for (int i = 0; i < n; i++) {
    int need = target - nums[i];
    if (mp.count(need)) return {mp[need], i};
    mp[nums[i]] = i;
}
Hashing

Happy Number

Easy
LeetCode
7/8
EasyHashingLeetCode #202

Happy Number

Replace n by sum of squares of digits. Happy if you reach 1. Detect cycles with a set (or Floyd).

Examples

Example 1

Input

19

Output

true
Example 2

Input

2

Output

false

Stuck? Take a nudge

C++17