Skip to content

Commit c6f130e

Browse files
lxmn22novakscoder082003
andcommittedOct 16, 2024·
Added HashTable Problem-Solution
Co-authored-by: Ashish Kumar Singh <ashishk.singh1008@gmail.com> Co-authored-by: Laxman Singh Koranga <laxmankoranga03@gmail.com>
1 parent 164a647 commit c6f130e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
 

‎Java/HashTable/HappyNumber.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* PROBLEM: 202 : Happy Number
3+
*/
4+
class HappyNumber {
5+
public boolean isHappy(int n) {
6+
int slow = n;
7+
int fast = getSquareDigitSum(n);
8+
while (fast != 1 && slow != fast) {
9+
slow = getSquareDigitSum(slow);
10+
fast = getSquareDigitSum(getSquareDigitSum(fast));
11+
}
12+
return fast == 1;
13+
}
14+
15+
private int getSquareDigitSum(int n) {
16+
int squareDigitSum = 0;
17+
while (n > 0) {
18+
int digit = n % 10;
19+
squareDigitSum += digit * digit;
20+
n /= 10;
21+
}
22+
return squareDigitSum;
23+
}
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.