Skip to content

Instantly share code, notes, and snippets.

@SumanSudhir
Created April 30, 2020 18:28
Show Gist options
  • Save SumanSudhir/92ba0b6cde82d7c69996197b31f58107 to your computer and use it in GitHub Desktop.
Save SumanSudhir/92ba0b6cde82d7c69996197b31f58107 to your computer and use it in GitHub Desktop.
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