|
| 1 | +--- |
| 2 | +id: next-happy-number |
| 3 | +title: Next Happy Number (Geeks for Geeks) |
| 4 | +sidebar_label: 0004 - Next Happy Number |
| 5 | +tags: |
| 6 | + - intermediate |
| 7 | + - Fibonacci |
| 8 | + - Dynamic Programming |
| 9 | + - Mathematics |
| 10 | + - Algorithms |
| 11 | +--- |
| 12 | + |
| 13 | +This tutorial contains a complete walk-through of the Next Happy Number problem from the Geeks for Geeks website. It features the implementation of the solution code in three programming languages: Python, C++, and Java. |
| 14 | + |
| 15 | +## Problem Statement |
| 16 | + |
| 17 | +For a given non-negative integer N, find the next smallest Happy Number. A number is called Happy if it leads to 1 after a sequence of steps, where at each step the number is replaced by the sum of squares of its digits. If we start with a Happy Number and keep replacing it with the sum of squares of its digits, we eventually reach 1. |
| 18 | + |
| 19 | +## Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | +``` |
| 24 | +input: |
| 25 | +N = 8 |
| 26 | +
|
| 27 | +Output: |
| 28 | +10 |
| 29 | +``` |
| 30 | + |
| 31 | +Explanation: |
| 32 | +Next happy number after 8 is 10 since |
| 33 | +$[ 1 * 1 + 0 * 0 = 1]$ |
| 34 | + |
| 35 | +**Example 2** |
| 36 | + |
| 37 | +``` |
| 38 | +Input: |
| 39 | +N = 10 |
| 40 | +
|
| 41 | +Output: |
| 42 | +13 |
| 43 | +``` |
| 44 | + |
| 45 | +Explanation: |
| 46 | + |
| 47 | +After 10, 13 is the smallest happy number because |
| 48 | +$[1 * 1 + 3 * 3 = 10]$, so we replace 13 by 10 and $[1 * 1 + 0 * 0 = 1]$. |
| 49 | + |
| 50 | +## Task |
| 51 | + |
| 52 | +You don't need to read input or print anything. Your task is to complete the function `nextHappy()` which takes an integer N as input parameters and returns an integer, the next Happy number after N. |
| 53 | + |
| 54 | +## Constraints |
| 55 | + |
| 56 | +- $(1 \leq N \leq 10^5)$ |
| 57 | + |
| 58 | + |
| 59 | +## Solution Approach |
| 60 | + |
| 61 | +### Intuition |
| 62 | + |
| 63 | +To solve the problem, we need to: |
| 64 | + |
| 65 | +1. Identify the next number greater than N. |
| 66 | +2. Check if it is a Happy Number. |
| 67 | +3. Repeat the process until we find the next Happy Number. |
| 68 | + |
| 69 | +A number is identified as Happy if, by repeatedly replacing it with the sum of squares of its digits, we eventually reach 1. |
| 70 | + |
| 71 | +### Steps |
| 72 | + |
| 73 | +1. Implement a helper function to determine if a number is Happy. |
| 74 | +2. Start checking numbers greater than N, using the helper function to identify the next Happy Number. |
| 75 | + |
| 76 | +### Detailed Explanation |
| 77 | + |
| 78 | +The numbers that, when you repeatedly sum the squares of their digits, eventually result in 1 are known as "happy numbers." |
| 79 | + |
| 80 | +Here are examples of how to determine if numbers less than 10 are happy numbers: |
| 81 | + |
| 82 | +- **Number 1:** |
| 83 | + |
| 84 | + $[1 ^ 2 = 1]$ |
| 85 | + Since we have already reached 1, the process stops here. 1 is a happy number. |
| 86 | + |
| 87 | +- **Number 2:** |
| 88 | + |
| 89 | + $[2^2 = 4]$ |
| 90 | + $[4^2 = 16]$ |
| 91 | + $[1^2 + 6^2 = 37]$ |
| 92 | + $[3^2 + 7^2 = 58]$ |
| 93 | + $[5^2 + 8^2 = 89]$ |
| 94 | + $[8^2 + 9^2 = 145]$ |
| 95 | + $[1^2 + 4^2 + 5^2 = 42]$ |
| 96 | + $[4^2 + 2^2 = 20]$ |
| 97 | + $[2^2 + 0^2 = 4]$ |
| 98 | + |
| 99 | + Since we have reached 4 again, the process will continue in an infinite loop. 2 is not a happy number. |
| 100 | + |
| 101 | +- **Number 3:** |
| 102 | + |
| 103 | + Similar to the above steps, 3 will also enter a loop and is not a happy number. |
| 104 | + |
| 105 | +- **Number 4:** |
| 106 | + |
| 107 | + Similar to the above steps, 4 will also enter a loop and is not a happy number. |
| 108 | + |
| 109 | +- **Number 5:** |
| 110 | + |
| 111 | + Similar to the above steps, 5 will also enter a loop and is not a happy number. |
| 112 | + |
| 113 | +- **Number 6:** |
| 114 | + |
| 115 | + Similar to the above steps, 6 will also enter a loop and is not a happy number. |
| 116 | + |
| 117 | +- **Number 7:** |
| 118 | + |
| 119 | + $[7^2 = 49]$ |
| 120 | + $[4^2 + 9^2 = 97]$ |
| 121 | + $[9^2 + 7^2 = 130]$ |
| 122 | + $[1^2 + 3^2 + 0^2 = 10]$ |
| 123 | + $[1^2 + 0^2 = 1]$ |
| 124 | + |
| 125 | + Since we have reached 1, the process stops here. 7 is a happy number. |
| 126 | + |
| 127 | +- **Number 8:** |
| 128 | + |
| 129 | + Similar to the above steps, 8 will also enter a loop and is not a happy number. |
| 130 | + |
| 131 | +- **Number 9:** |
| 132 | + |
| 133 | + Similar to the above steps, 9 will also enter a loop and is not a happy number. |
| 134 | + |
| 135 | +Based on this analysis, the numbers less than 10 that result in 1 when you repeatedly sum the squares of their digits are: 1 and 7. |
| 136 | + |
| 137 | +### Implementation |
| 138 | + |
| 139 | +#### Code (C++): |
| 140 | + |
| 141 | +```cpp |
| 142 | +#include <iostream> |
| 143 | + |
| 144 | +class Solution { |
| 145 | +public: |
| 146 | + bool solve(int n) { |
| 147 | + if (n == 1 || n == 7) return true; |
| 148 | + if (n == 2 || n == 4 || n == 8 || n == 3 || n == 9 || n == 5 || n == 6) return false; |
| 149 | + int sq_sum = 0; |
| 150 | + while (n) { |
| 151 | + int x = n % 10; |
| 152 | + sq_sum += (x * x); |
| 153 | + n /= 10; |
| 154 | + } |
| 155 | + return solve(sq_sum); |
| 156 | + } |
| 157 | + |
| 158 | + int nextHappy(int n) { |
| 159 | + while (true) { |
| 160 | + n++; |
| 161 | + if (solve(n)) return n; |
| 162 | + } |
| 163 | + } |
| 164 | +}; |
| 165 | + |
| 166 | +int main() { |
| 167 | + Solution sol; |
| 168 | + int N = 8; |
| 169 | + std::cout << "Next happy number after " << N << " is: " << sol.nextHappy(N) << std::endl; |
| 170 | + return 0; |
| 171 | +} |
| 172 | + |
| 173 | +``` |
| 174 | + |
| 175 | +#### Code(Python) |
| 176 | + |
| 177 | +```python |
| 178 | +class Solution: |
| 179 | + def solve(self, n: int) -> bool: |
| 180 | + if n == 1 or n == 7: |
| 181 | + return True |
| 182 | + if n in {2, 4, 8, 3, 9, 5, 6}: |
| 183 | + return False |
| 184 | + sq_sum = 0 |
| 185 | + while n > 0: |
| 186 | + x = n % 10 |
| 187 | + sq_sum += (x * x) |
| 188 | + n //= 10 |
| 189 | + return self.solve(sq_sum) |
| 190 | + |
| 191 | + def nextHappy(self, n: int) -> int: |
| 192 | + while True: |
| 193 | + n += 1 |
| 194 | + if self.solve(n): |
| 195 | + return n |
| 196 | + |
| 197 | +# Example usage |
| 198 | +sol = Solution() |
| 199 | +N = 8 |
| 200 | +print(f"Next happy number after {N} is: {sol.nextHappy(N)}") |
| 201 | + |
| 202 | +``` |
| 203 | + |
| 204 | +#### Code (Java) |
| 205 | + |
| 206 | +```java |
| 207 | +public class Solution { |
| 208 | + public boolean solve(int n) { |
| 209 | + if (n == 1 || n == 7) return true; |
| 210 | + if (n == 2 || n == 4 || n == 8 || n == 3 || n == 9 || n == 5 || n == 6) return false; |
| 211 | + int sq_sum = 0; |
| 212 | + while (n > 0) { |
| 213 | + int x = n % 10; |
| 214 | + sq_sum += (x * x); |
| 215 | + n /= 10; |
| 216 | + } |
| 217 | + return solve(sq_sum); |
| 218 | + } |
| 219 | + |
| 220 | + public int nextHappy(int n) { |
| 221 | + while (true) { |
| 222 | + n++; |
| 223 | + if (solve(n)) return n; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + public static void main(String[] args) { |
| 228 | + Solution sol = new Solution(); |
| 229 | + int N = 8; |
| 230 | + System.out.println("Next happy number after " + N + " is: " + sol.nextHappy(N)); |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +``` |
| 235 | + |
| 236 | +### Complexity |
| 237 | + |
| 238 | +- **Time Complexity:** $O(klog_{10}N)$ due to the operations on digits of the numbers. |
| 239 | + |
| 240 | +#### Explanation : |
| 241 | + |
| 242 | +- We will be able to determine whether a number is happy or not after recursively calling the solve function and adding the square of its digits for a maximum of 15-20 times (you can check for any random value less than 10^5). Let's denote this value as x. |
| 243 | +- The time taken to add the square of the digits is log10(n). Furthermore, as we are checking until we find the happy number, let's denote the number of iterations as c = (happy number>n) -n |
| 244 | +- Let's denote x\*c=k; |
| 245 | + Therefore, TC: (klog10(n)) |
| 246 | +- The value of k won't even reach 10^4 or 10^5. You can try this approach with any random value. |
| 247 | + |
| 248 | +- **Space Complexity:** $O(1)$ since we only use a fixed amount of extra space for the set to store seen numbers. |
| 249 | + |
| 250 | +## Conclusion |
| 251 | + |
| 252 | +To find the next Happy Number after a given integer N, we can implement a solution that iteratively checks each number greater than N until a Happy Number is found. This solution efficiently identifies Happy Numbers using a helper function to compute the sum of squares of digits and a set to track previously seen numbers to avoid infinite loops. |
| 253 | + |
| 254 | +## References |
| 255 | + |
| 256 | +- **GeeksforGeeks Problem:** [Geeks for Geeks Problem](https://www.geeksforgeeks.org/problems/fibonacci-sum/0) |
| 257 | +- **Solution Link:** [Fibonacci Sum on Geeks for Geeks](https://www.geeksforgeeks.org/problems/fibonacci-sum/0) |
| 258 | +- **Authors GeeksforGeeks Profile:** [Vipul](https://www.geeksforgeeks.org/user/lakumvipwjge/) |
| 259 | + |
0 commit comments