forked from MilindGupta-Creator/Competitive-Coding-Section
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
40 lines (37 loc) · 1.11 KB
/
Solution.java
File metadata and controls
40 lines (37 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// The happy number can be defined as a number which will yield 1 when it is replaced by the sum of the square of its digits repeatedly
//Approach 1:
//Runtime: 2ms
//Memory usage: 33MB
import java.util.*;
class Solution {
//Using HashMaps to store the evaluated values to detect repetitions.
//FUNC TO RETURN WHETHER NUMBER IS HAPPY OR NOT
public static boolean isHappy(int n) {
HashSet<Integer> set = new HashSet();
set.add(n);
int sum = n;
while(sum!=1){
sum = getDigitSqrSum(sum);
if(set.contains(sum)){
return false;
} else {
set.add(sum);
}
}
return true;
}
//FUNCTION TO ADD SQUARE OF EACH DIGIT OF THE NUMBER
private static int getDigitSqrSum(int n){
int sum = 0;
while(n!=0){
int d = n%10;
sum+=(d*d);
n=n/10;
}
return sum;
}
public static void main(String[] arg){
int number = 19;
System.out.println(isHappy(number));
}
}