-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHappyNumber.java
More file actions
145 lines (113 loc) · 3.44 KB
/
HappyNumber.java
File metadata and controls
145 lines (113 loc) · 3.44 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package Algorithms.Hashing;
import java.util.HashSet;
import java.util.Set;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 16 Sept 2025
* @link 202. Happy Number <a href="https://leetcode.com/problems/happy-number/">Leetcode link</a>
* @topics Hash Table, Math, Two Pointers
* Number of iterations ≤ number of unique states (≤ 810).
* 2 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 ... this is how the loop continues
*/
public class HappyNumber {
public static void main(String[] args) {
int n = 19;
System.out.println("isHappy using HashSet => " + isHappyUsingHashSet(n));
System.out.println("isHappy using Recurrence Detection => " + isHappyUsingRecurrenceDetection(n));
System.out.println("isHappy using Two Pointers Cycle Detection => " + isHappyUsingTwoPointersCycleDetection(n));
System.out.println("isHappy using Count => " + isHappyUsingCount(n));
}
/**
* @TimeComplexity O(log n)
* @SpaceComplexity O(log n)
*/
public static boolean isHappyUsingHashSet(int n) {
Set<Integer> seen = new HashSet<>();
while (seen.add(n)) {
int sum = 0; // or sum(n);
while (n>0) {
int lastDigit = n%10;
n /= 10;
sum += lastDigit*lastDigit;
/*
// or
int tens = (int) Math.pow(10, (int) Math.log10(n));
int firstDigit = n / tens;
sum += firstDigit*firstDigit;
n = n - firstDigit*tens;
*/
}
if(sum == 1) {
return true;
}
n = sum;
}
return false;
}
/**
* @TimeComplexity O(log n)
* @SpaceComplexity O(1)
*/
public static boolean isHappyUsingRecurrenceDetection(int givenNum) { // by comparing against the original number
int n = givenNum;
if(n==1 || n==7) {
return true;
}
while(n>9) {
int sum = sum(n);
if(sum ==1 || sum==7) {
return true;
} else if(sum==givenNum) {
return false;
}
n=sum;
}
return false;
}
public static int sum(int n) {
int sum=0;
while(n>0) {
int rem=n%10; // remainder or lastDigit
sum +=rem*rem;
n=n/10;
}
return sum;
}
/**
* @TimeComplexity O(log n)
* @SpaceComplexity O(1)
*/
public static boolean isHappyUsingTwoPointersCycleDetection(int n) {
int slow = n;
int fast = sum(n);
while (slow != fast) {
fast = sum(sum(fast));
slow = sum(slow);
}
return fast == 1;
/*
// or
int slow = sum(n);
int fast = sum(sum(n));
while (slow != fast) {
if (fast == 1) return true;
slow = sum(slow);
fast = sum(sum(fast));
}
return slow == 1;
*/
}
/**
* @TimeComplexity O(log n)
* @SpaceComplexity O(1) -- ⚠️ But correctness is not guaranteed — “1000” is just an arbitrary bound.
*/
public static boolean isHappyUsingCount(int n) {
int count = 0;
while (n != 1) {
int sum = sum(n);
if (count++ > 1000) return false;
n = sum;
}
return true;
}
}