← Discussions on Happy Number
T tkumar
11 Sep 2026, 15:41

Simple approach explained for beginners.

Use a hash map from value to index (or count). For each element check whether its complement is already in the map, then insert it. That makes it O(n) instead of the O(n²) brute force.

3 replies

vamsi.t 2 months, 3 weeks ago

You can avoid the extra array and do it in O(1) space.

bhavana_34 2 months, 2 weeks ago

For the TLE: you are rebuilding the list inside the loop, move it outside.

jahnavisub6 2 months, 1 week ago

Same idea, but I used a deque. Works fine.

Sign in to reply.