Created
April 30, 2020 18:28
-
-
Save SumanSudhir/92ba0b6cde82d7c69996197b31f58107 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public:; | |
int sumSquare(int n) { | |
int sum = 0; | |
while(n !=0){ | |
sum += (n%10) * (n%10); | |
n = n/10; | |
} | |
return sum; | |
} | |
bool isHappy(int n) { | |
map<int, int> track; | |
track[n]++; | |
int sum = sumSquare(n); | |
while(true){ | |
track[sum]++; | |
if(sum == 1) return true; | |
if(track[sum] > 1) return false; | |
sum = sumSquare(sum); | |
} | |
return false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment