← Discussions on Happy Number
S saiaga1 OFFICIAL
28 May 2026, 11:24

Clean C++ solution, beats most submissions.

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.

1 reply

arjundas 4 months, 2 weeks ago

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

Sign in to reply.