You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🔥 Happy Number 🔥 || 5 Solutions || Simple Fast and Easy || with Explanation
Solution - 1
Complexity Analysis
Time Complexity: O(n*log(n)).
Auxiliary Space: O(1).
classSolution {
// Runtime: 559 ms, faster than 36.59% of Dart online submissions for Happy Number.// Memory Usage: 140.6 MB, less than 95.12% of Dart online submissions for Happy Number.boolisHappy(int n) {
int slow, fast;
// initialize slow and fast by n
slow = fast = n;
do {
// move slow number// by one iteration
slow =numSquareSum(slow);
// move fast number// by two iteration
fast =numSquareSum(numSquareSum(fast));
} while (slow != fast);
return (slow ==1);
}
// Utility method to return sum of square of// digit of nintnumSquareSum(int n) {
int sumSquare =0;
while (n !=0) {
sumSquare += (n %10) * (n %10);
n ~/=10;
}
return sumSquare;
}
Solution - 2
Complexity-Analysis
Time Complexity: O(n*log(n)).
Auxiliary Space: O(n) since using extra set for storage
classSolution {
// Runtime: 536 ms, faster than 53.66% of Dart online submissions for Happy Number.// Memory Usage: 143.7 MB, less than 29.27% of Dart online submissions for Happy Number.boolisHappy(int n) {
HashSet hs =HashSet();
while (true) {
n =numSquareSum(n);
if (n ==1) returntrue;
if (hs.contains(n)) returnfalse;
hs.add(n);
}
}
intnumSquareSum(int n) {
int sumSquare =0;
while (n !=0) {
sumSquare += (n %10) * (n %10);
n ~/=10;
}
return sumSquare;
}
}
Solution - 3
classSolution {
// Runtime: 484 ms, faster than 78.05% of Dart online submissions for Happy Number.// Memory Usage: 140.6 MB, less than 95.12% of Dart online submissions for Happy Number.boolisHappy(int n) {
if (n ==1|| n ==7) returntrue;
int sum = n, x = n;
// this loop executes till the sum of square of// digits obtained is not a single digit numberwhile (sum >9) {
sum =0;
// this loop finds the sum of square of digitswhile (x >0) {
int d = x %10;
sum += d * d;
x ~/=10;
}
if (sum ==1) returntrue;
x = sum;
}
if (sum ==7) returntrue;
returnfalse;
}
}
Solution - 4
classSolution {
// Runtime: 261 ms, faster than 100.00% of Dart online submissions for Happy Number.// Memory Usage: 139.3 MB, less than 100.00% of Dart online submissions for Happy Number./// > It takes a number, adds the squares of its digits, and returns the result /// /// Args: /// n (int): The number to be checked. /// /// Returns: /// The sum of the squares of the digits of the number.intsolve(int n) {
int sum =0;
for (int i =0; n !=0; i++) {
int r = n %10;
sum = sum + r * r;
n = n ~/10;
}
return sum;
}
boolisHappy(int n) {
int i =10;
/// A loop that checks if the number is happy.while (i >0) {
int happy =solve(n);
if (happy ==1) {
returntrue;
}
n = happy;
i--;
}
returnfalse;
}
}
SOlution 5
classSolution {
// Runtime: 309 ms, faster than 100.00% of Dart online submissions for Happy Number.// Memory Usage: 158 MB, less than 7.32% of Dart online submissions for Happy Number.intsums(int n) {
int ans =0;
while (n !=0) {
//ans += pow(n % 10, 2);
ans +=pow(n %10, 2).floor();
n = n ~/10;
}
return ans;
}
boolisHappy(int n) {
int s =sums(n);
int f =sums(sums(n));
if (f ==1) returntrue; //for case n==1while (s != f) {
if (f ==1) returntrue;
s =sums(s);
f =sums(sums(f));
}
returnfalse;
}
}