From 022df9b8d4418bb030d224ba51d4b5c9aa1b8cce Mon Sep 17 00:00:00 2001 From: Vipul_Lakum Date: Mon, 3 Jun 2024 17:22:06 +0530 Subject: [PATCH 1/6] Next Happy Number solution added --- .../gfg-solutions/0005-next-happy-number.md | 255 ++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 dsa-solutions/gfg-solutions/0005-next-happy-number.md diff --git a/dsa-solutions/gfg-solutions/0005-next-happy-number.md b/dsa-solutions/gfg-solutions/0005-next-happy-number.md new file mode 100644 index 000000000..b3a9ce3b6 --- /dev/null +++ b/dsa-solutions/gfg-solutions/0005-next-happy-number.md @@ -0,0 +1,255 @@ +--- +id: next-happy-number +title: Next Happy Number (Geeks for Geeks) +sidebar_label: 0004 - Next Happy Number +tags: + - intermediate + - Fibonacci + - Dynamic Programming + - Mathematics + - Algorithms +--- + +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. + +## Problem Statement + +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. + +## Examples + +**Example 1:** + +``` +input: + +N = 8 + +Output: + +10 + +Explanation: +Next happy number after 8 is 10 since +\[1*1 + 0*0 = 1\] +``` + +**Example 2** + +``` +Input: + +N = 10 + +Output: + +13 + +Explanation: + +After 10, 13 is the smallest happy number because +\[1*1 + 3*3 = 10\], so we replace 13 by 10 and \[1*1 + 0*0 = 1\]. + +``` + +## Task + +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. + +## Constraints + +1. $(1 \leq N \leq 10^5)$ + +## Solution Approach + +### Intuition + +To solve the problem, we need to: + +1. Identify the next number greater than N. +2. Check if it is a Happy Number. +3. Repeat the process until we find the next Happy Number. + +A number is identified as Happy if, by repeatedly replacing it with the sum of squares of its digits, we eventually reach 1. + +### Steps + +1. Implement a helper function to determine if a number is Happy. +2. Start checking numbers greater than N, using the helper function to identify the next Happy Number. + +### Detailed Explanation + +The numbers that, when you repeatedly sum the squares of their digits, eventually result in 1 are known as "happy numbers." + +Here are examples of how to determine if numbers less than 10 are happy numbers: + +- **Number 1:** + \[1^2 = 1\] + Since we have already reached 1, the process stops here. 1 is a happy number. + +- **Number 2:** + \[2^2 = 4\] + \[4^2 = 16\] + \[1^2 + 6^2 = 37\] + \[3^2 + 7^2 = 58\] + \[5^2 + 8^2 = 89\] + \[8^2 + 9^2 = 145\] + \[1^2 + 4^2 + 5^2 = 42\] + \[4^2 + 2^2 = 20\] + \[2^2 + 0^2 = 4\] + Since we have reached 4 again, the process will continue in an infinite loop. 2 is not a happy number. + +- **Number 3:** + Similar to the above steps, 3 will also enter a loop and is not a happy number. + +- **Number 4:** + Similar to the above steps, 4 will also enter a loop and is not a happy number. + +- **Number 5:** + Similar to the above steps, 5 will also enter a loop and is not a happy number. + +- **Number 6:** + Similar to the above steps, 6 will also enter a loop and is not a happy number. + +- **Number 7:** + \[7^2 = 49\] + \[4^2 + 9^2 = 97\] + \[9^2 + 7^2 = 130\] + \[1^2 + 3^2 + 0^2 = 10\] + \[1^2 + 0^2 = 1\] + Since we have reached 1, the process stops here. 7 is a happy number. + +- **Number 8:** + Similar to the above steps, 8 will also enter a loop and is not a happy number. + +- **Number 9:** + Similar to the above steps, 9 will also enter a loop and is not a happy number. + +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. + +### Implementation + +#### Code (C++): + +```cpp +#include + +class Solution { +public: + bool solve(int n) { + if (n == 1 || n == 7) return true; + if (n == 2 || n == 4 || n == 8 || n == 3 || n == 9 || n == 5 || n == 6) return false; + int sq_sum = 0; + while (n) { + int x = n % 10; + sq_sum += (x * x); + n /= 10; + } + return solve(sq_sum); + } + + int nextHappy(int n) { + while (true) { + n++; + if (solve(n)) return n; + } + } +}; + +int main() { + Solution sol; + int N = 8; + std::cout << "Next happy number after " << N << " is: " << sol.nextHappy(N) << std::endl; + return 0; +} + +``` + +#### Code(Python) + +```python +class Solution: + def solve(self, n: int) -> bool: + if n == 1 or n == 7: + return True + if n in {2, 4, 8, 3, 9, 5, 6}: + return False + sq_sum = 0 + while n > 0: + x = n % 10 + sq_sum += (x * x) + n //= 10 + return self.solve(sq_sum) + + def nextHappy(self, n: int) -> int: + while True: + n += 1 + if self.solve(n): + return n + +# Example usage +sol = Solution() +N = 8 +print(f"Next happy number after {N} is: {sol.nextHappy(N)}") + +``` + +#### Code (Java) + +```java +public class Solution { + public boolean solve(int n) { + if (n == 1 || n == 7) return true; + if (n == 2 || n == 4 || n == 8 || n == 3 || n == 9 || n == 5 || n == 6) return false; + int sq_sum = 0; + while (n > 0) { + int x = n % 10; + sq_sum += (x * x); + n /= 10; + } + return solve(sq_sum); + } + + public int nextHappy(int n) { + while (true) { + n++; + if (solve(n)) return n; + } + } + + public static void main(String[] args) { + Solution sol = new Solution(); + int N = 8; + System.out.println("Next happy number after " + N + " is: " + sol.nextHappy(N)); + } +} + +``` + +### Complexity + +- **Time Complexity:** $O(klog_{10}N)$ due to the operations on digits of the numbers. + +#### Explanation : + +- 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. +- 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 +- Let's denote x\*c=k; + Therefore, TC: (klog10(n)) +- The value of k won't even reach 10^4 or 10^5. You can try this approach with any random value. + +- **Space Complexity:** $O(1)$ since we only use a fixed amount of extra space for the set to store seen numbers. + +## Conclusion + +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. + +## References + +- **GeeksforGeeks Problem:** [Geeks for Geeks Problem](https://www.geeksforgeeks.org/problems/fibonacci-sum/0) +- **Solution Link:** [Fibonacci Sum on Geeks for Geeks](https://www.geeksforgeeks.org/problems/fibonacci-sum/0) +- **Authors GeeksforGeeks Profile:** [Vipul](https://www.geeksforgeeks.org/user/lakumvipwjge/) + +``` +This structured tutorial provides a comprehensive solution to the Fibonacci Sum problem, making it easy for others to understand and implement in various programming languages. +``` \ No newline at end of file From 922f5231cd799b28e045a6d4ea68803ebb58a8fe Mon Sep 17 00:00:00 2001 From: Vipul_Lakum Date: Mon, 3 Jun 2024 17:32:36 +0530 Subject: [PATCH 2/6] Next Happy Number solution added --- dsa-solutions/gfg-solutions/0004-fibbonacci-sum.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/dsa-solutions/gfg-solutions/0004-fibbonacci-sum.md b/dsa-solutions/gfg-solutions/0004-fibbonacci-sum.md index da9bfe81e..7f43ea152 100644 --- a/dsa-solutions/gfg-solutions/0004-fibbonacci-sum.md +++ b/dsa-solutions/gfg-solutions/0004-fibbonacci-sum.md @@ -267,7 +267,3 @@ By leveraging the properties of Fibonacci numbers and matrix multiplication, we - **GeeksforGeeks Problem:** [Geeks for Geeks Problem](https://www.geeksforgeeks.org/problems/fibonacci-sum/0) - **Solution Link:** [Fibonacci Sum on Geeks for Geeks](https://www.geeksforgeeks.org/problems/fibonacci-sum/0) - **Authors GeeksforGeeks Profile:** [Vipul](https://www.geeksforgeeks.org/user/lakumvipwjge/) - -``` -This structured tutorial provides a comprehensive solution to the Fibonacci Sum problem, making it easy for others to understand and implement in various programming languages. -``` From e5e7df23ce60058b50a375c17bebd8b9184e53bb Mon Sep 17 00:00:00 2001 From: Vipul_Lakum Date: Mon, 3 Jun 2024 17:37:38 +0530 Subject: [PATCH 3/6] Next Happy Number solution added --- .../gfg-solutions/0005-next-happy-number.md | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/dsa-solutions/gfg-solutions/0005-next-happy-number.md b/dsa-solutions/gfg-solutions/0005-next-happy-number.md index b3a9ce3b6..dece79837 100644 --- a/dsa-solutions/gfg-solutions/0005-next-happy-number.md +++ b/dsa-solutions/gfg-solutions/0005-next-happy-number.md @@ -29,10 +29,11 @@ Output: 10 +``` + Explanation: Next happy number after 8 is 10 since -\[1*1 + 0*0 = 1\] -``` +$[ 1 * 1 + 0 * 0 = 1]$ **Example 2** @@ -45,12 +46,12 @@ Output: 13 +``` + Explanation: After 10, 13 is the smallest happy number because -\[1*1 + 3*3 = 10\], so we replace 13 by 10 and \[1*1 + 0*0 = 1\]. - -``` +$[1 * 1 + 3 * 3 = 10]$, so we replace 13 by 10 and $[1 * 1 + 0 * 0 = 1]$. ## Task @@ -84,20 +85,24 @@ The numbers that, when you repeatedly sum the squares of their digits, eventuall Here are examples of how to determine if numbers less than 10 are happy numbers: - **Number 1:** - \[1^2 = 1\] + $[1 ^ 2 = 1]$ Since we have already reached 1, the process stops here. 1 is a happy number. - **Number 2:** - \[2^2 = 4\] - \[4^2 = 16\] - \[1^2 + 6^2 = 37\] - \[3^2 + 7^2 = 58\] - \[5^2 + 8^2 = 89\] - \[8^2 + 9^2 = 145\] - \[1^2 + 4^2 + 5^2 = 42\] - \[4^2 + 2^2 = 20\] - \[2^2 + 0^2 = 4\] + +``` + $[2^2 = 4]$ + $[4^2 = 16]$ + $[1^2 + 6^2 = 37]$ + $[3^2 + 7^2 = 58]$ + $[5^2 + 8^2 = 89]$ + $[8^2 + 9^2 = 145]$ + $[1^2 + 4^2 + 5^2 = 42]$ + $[4^2 + 2^2 = 20]$ + $[2^2 + 0^2 = 4]$ + Since we have reached 4 again, the process will continue in an infinite loop. 2 is not a happy number. +``` - **Number 3:** Similar to the above steps, 3 will also enter a loop and is not a happy number. @@ -112,12 +117,15 @@ Here are examples of how to determine if numbers less than 10 are happy numbers: Similar to the above steps, 6 will also enter a loop and is not a happy number. - **Number 7:** - \[7^2 = 49\] - \[4^2 + 9^2 = 97\] - \[9^2 + 7^2 = 130\] - \[1^2 + 3^2 + 0^2 = 10\] - \[1^2 + 0^2 = 1\] + +``` + $[7^2 = 49]$ + $[4^2 + 9^2 = 97]$ + $[9^2 + 7^2 = 130]$ + $[1^2 + 3^2 + 0^2 = 10]$ + $[1^2 + 0^2 = 1]$ Since we have reached 1, the process stops here. 7 is a happy number. +``` - **Number 8:** Similar to the above steps, 8 will also enter a loop and is not a happy number. @@ -250,6 +258,3 @@ To find the next Happy Number after a given integer N, we can implement a soluti - **Solution Link:** [Fibonacci Sum on Geeks for Geeks](https://www.geeksforgeeks.org/problems/fibonacci-sum/0) - **Authors GeeksforGeeks Profile:** [Vipul](https://www.geeksforgeeks.org/user/lakumvipwjge/) -``` -This structured tutorial provides a comprehensive solution to the Fibonacci Sum problem, making it easy for others to understand and implement in various programming languages. -``` \ No newline at end of file From 4dfdb7bb47ccaa60f014de701a270c07d3042bda Mon Sep 17 00:00:00 2001 From: Vipul_Lakum Date: Mon, 3 Jun 2024 17:38:32 +0530 Subject: [PATCH 4/6] Next Happy Number solution added --- dsa-solutions/gfg-solutions/0005-next-happy-number.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/dsa-solutions/gfg-solutions/0005-next-happy-number.md b/dsa-solutions/gfg-solutions/0005-next-happy-number.md index dece79837..1d673f754 100644 --- a/dsa-solutions/gfg-solutions/0005-next-happy-number.md +++ b/dsa-solutions/gfg-solutions/0005-next-happy-number.md @@ -22,13 +22,10 @@ For a given non-negative integer N, find the next smallest Happy Number. A numbe ``` input: - N = 8 Output: - 10 - ``` Explanation: @@ -39,13 +36,10 @@ $[ 1 * 1 + 0 * 0 = 1]$ ``` Input: - N = 10 Output: - 13 - ``` Explanation: @@ -124,6 +118,7 @@ Here are examples of how to determine if numbers less than 10 are happy numbers: $[9^2 + 7^2 = 130]$ $[1^2 + 3^2 + 0^2 = 10]$ $[1^2 + 0^2 = 1]$ + Since we have reached 1, the process stops here. 7 is a happy number. ``` From 1e82488addac2191d9f6b0d6ed9648a32a14ef8c Mon Sep 17 00:00:00 2001 From: Vipul_Lakum Date: Mon, 3 Jun 2024 21:26:53 +0530 Subject: [PATCH 5/6] updated as per your sugession --- dsa-solutions/gfg-solutions/0005-next-happy-number.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dsa-solutions/gfg-solutions/0005-next-happy-number.md b/dsa-solutions/gfg-solutions/0005-next-happy-number.md index f6b486b94..afd83100d 100644 --- a/dsa-solutions/gfg-solutions/0005-next-happy-number.md +++ b/dsa-solutions/gfg-solutions/0005-next-happy-number.md @@ -53,7 +53,8 @@ You don't need to read input or print anything. Your task is to complete the fun ## Constraints -1. $(1 \leq N \leq 10^5)$ +- $(1 \leq N \leq 10^5)$ + ## Solution Approach From 7dbfc7919d208d24503870f60662ec05ca35e6d9 Mon Sep 17 00:00:00 2001 From: Vipul_Lakum Date: Mon, 3 Jun 2024 21:31:20 +0530 Subject: [PATCH 6/6] updated as per your sugession --- dsa-solutions/gfg-solutions/0004-fibbonacci-sum.md | 2 +- .../{0005-next-happy-number.md => 0006-next-happy-number.md} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename dsa-solutions/gfg-solutions/{0005-next-happy-number.md => 0006-next-happy-number.md} (100%) diff --git a/dsa-solutions/gfg-solutions/0004-fibbonacci-sum.md b/dsa-solutions/gfg-solutions/0004-fibbonacci-sum.md index ede738d60..6fc2b34d3 100644 --- a/dsa-solutions/gfg-solutions/0004-fibbonacci-sum.md +++ b/dsa-solutions/gfg-solutions/0004-fibbonacci-sum.md @@ -49,7 +49,7 @@ Expected Auxiliary Space: $O(1)$ ## Constraints -1. $1 \leq N \leq 100000$ +- $(1 \leq N \leq 100000)$ ## Solution Approach diff --git a/dsa-solutions/gfg-solutions/0005-next-happy-number.md b/dsa-solutions/gfg-solutions/0006-next-happy-number.md similarity index 100% rename from dsa-solutions/gfg-solutions/0005-next-happy-number.md rename to dsa-solutions/gfg-solutions/0006-next-happy-number.md