From 0c31997135156cf669caa6cfd64e1866c2960977 Mon Sep 17 00:00:00 2001 From: anoushka Date: Sat, 1 Jun 2024 23:49:45 +0300 Subject: [PATCH 1/3] fascinating numbers --- .../gfg-solutions/0002-fascinating-number.md | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 dsa-solutions/gfg-solutions/0002-fascinating-number.md diff --git a/dsa-solutions/gfg-solutions/0002-fascinating-number.md b/dsa-solutions/gfg-solutions/0002-fascinating-number.md new file mode 100644 index 000000000..f3d3f5296 --- /dev/null +++ b/dsa-solutions/gfg-solutions/0002-fascinating-number.md @@ -0,0 +1,123 @@ +--- +id: fascinating-number +title: Fascinating Number Problem (Geeks for Geeks) +sidebar_label: 0002 - Fascinating Number +tags: + - Beginner + - String + - Find + - Multiplication + - Geeks for Geeks + - CPP + - Python + - DSA +description: "This is a solution to the Fascinating Number problem on Geeks for Geeks." +--- + +## Problem Description + +Given a number N. Your task is to check whether it is fascinating or not. + +Fascinating Number: When a number(should contain 3 digits or more) is multiplied by 2 and 3, and when both these products are concatenated with the original number, then it results in all digits from 1 to 9 present exactly once. + +## Examples + +Example 1: + +Input: N = 192 +Output: Fascinating +Explanation: After multiplication with 2 and 3, and concatenating with original number, number will become 192384576 which contains all digits from 1 to 9. + +Example 2: + +Input: N = 853 +Output: Not Fascinating +Explanation: It's not a fascinating number. + +## Your Task + +You don't need to read input or print anything. Your task is to complete the function `fascinating()` which takes the integer n parameters and returns boolean (True or False) denoting the answer. + +Expected Time Complexity: $O(1)$ +Expected Auxiliary Space: $O(1)$ + +## Constraints + +100 <= N <= 2*10^9 + +## Problem Explanation + +The problem is to determine if a given number N is a fascinating number. A fascinating number is defined as follows: + +1. The number must have at least three digits. +2. Multiply the number by 2 and 3 to get two products. +3. Concatenate the original number, the product of the number and 2, and the product of the number and 3 into a single string. +4. The concatenated string should contain all digits from 1 to 9 exactly once, with no other digits present (e.g., no zeros). + +## CPP Code + +```cpp +class Solution { +public: + bool fascinating(int n) { + int m2 = n * 2; + int m3 = n * 3; + string num = to_string(n) + to_string(m2) + to_string(m3); + sort(num.begin(), num.end()); + if (num.find("123456789") == string::npos) + return false; + else if (num.length() - num.find("123456789") > 9) + return false; + else return true; + } +}; +``` + +## Python Code + +```py +class Solution: + + def fascinating(self, n): + m2 = n * 2 + m3 = n * 3 + num = str(n) + str(m2) + str(m3) + num = "".join(sorted(num)) + zero_count = num.count("0") + if (num.find("123456789") == -1): + return False + elif (len(num) - zero_count > 9): + return False + else: + return True +``` + +## Example Walkthrough + +For N = 192: +1. Original number: 192 +2. Multiply by 2: 192 × 2 = 384 +3. Multiply by 3: 192 × 3 = 576 +4. Concatenate: "192" + "384" + "576" = "192384576" +5. Check if the concatenated string contains all digits from 1 to 9 exactly once: "192384576" contains each digit from 1 to 9 exactly once. + +Therefore, 192 is a fascinating number. + +## Solution Logic: + +1. Compute the Products: Multiply the number N by 2 and 3 to get two new numbers. +2. Concatenate the Results: Convert the original number and the two products to strings and concatenate them. +3. Sort and Check Digits: Sort the concatenated string and check if it contains the sequence "123456789" exactly once. +4. Verify Length: Ensure there are no extra digits (like zero or repetitions). The total length of digits should be exactly 9, excluding any zeros. + +## Time Complexity + +The time complexity is $O(1)$ because the operations involve a fixed number of steps regardless of the size of N: + +* Multiplication and string concatenation are constant time operations. +* Sorting a string of fixed length (at most 9 characters) is a constant time operation. +* Checking for the sequence "123456789" in a fixed-length string is also a constant time operation. + +## Space Complexity + +The space complexity is $O(1)$ as well since the operations use a constant amount of extra space for storing the products and concatenated strings. \ No newline at end of file From 394c707e728b83477a57bb201f897f50d628c016 Mon Sep 17 00:00:00 2001 From: anoushka Date: Sun, 2 Jun 2024 04:11:11 +0300 Subject: [PATCH 2/3] revisions --- .../gfg-solutions/0001-value-equal-to-index-value.md | 4 ++-- dsa-solutions/gfg-solutions/0002-fascinating-number.md | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/dsa-solutions/gfg-solutions/0001-value-equal-to-index-value.md b/dsa-solutions/gfg-solutions/0001-value-equal-to-index-value.md index 8aeffe0c8..3d35d9d97 100644 --- a/dsa-solutions/gfg-solutions/0001-value-equal-to-index-value.md +++ b/dsa-solutions/gfg-solutions/0001-value-equal-to-index-value.md @@ -47,8 +47,8 @@ You don't need to read input or print anything. Your task is to complete the fun ## Constraints -* 1 ≤ N ≤ 105 -* 1 ≤ `Arr[i]` ≤ 106 +* `1 ≤ N ≤ 105` +* `1 ≤ Arr[i] ≤ 106` ## Problem Explanation diff --git a/dsa-solutions/gfg-solutions/0002-fascinating-number.md b/dsa-solutions/gfg-solutions/0002-fascinating-number.md index f3d3f5296..c1598b47f 100644 --- a/dsa-solutions/gfg-solutions/0002-fascinating-number.md +++ b/dsa-solutions/gfg-solutions/0002-fascinating-number.md @@ -24,15 +24,19 @@ Fascinating Number: When a number(should contain 3 digits or more) is multiplied Example 1: +``` Input: N = 192 Output: Fascinating Explanation: After multiplication with 2 and 3, and concatenating with original number, number will become 192384576 which contains all digits from 1 to 9. +``` Example 2: +``` Input: N = 853 Output: Not Fascinating Explanation: It's not a fascinating number. +``` ## Your Task @@ -43,7 +47,7 @@ Expected Auxiliary Space: $O(1)$ ## Constraints -100 <= N <= 2*10^9 +`100 <= N <= 2*10^9` ## Problem Explanation From 28654c2263b016f67623299801fbfc86a4c05a1f Mon Sep 17 00:00:00 2001 From: anoushka Date: Sun, 2 Jun 2024 06:23:39 +0300 Subject: [PATCH 3/3] updates --- .../gfg-solutions/0002-fascinating-number.md | 89 +++++++++++-------- 1 file changed, 53 insertions(+), 36 deletions(-) diff --git a/dsa-solutions/gfg-solutions/0002-fascinating-number.md b/dsa-solutions/gfg-solutions/0002-fascinating-number.md index c1598b47f..27d22ebbe 100644 --- a/dsa-solutions/gfg-solutions/0002-fascinating-number.md +++ b/dsa-solutions/gfg-solutions/0002-fascinating-number.md @@ -14,6 +14,8 @@ tags: description: "This is a solution to the Fascinating Number problem on Geeks for Geeks." --- +This tutorial contains a complete walk-through of the Fascinating Number problem from the Geeks for Geeks website. It features the implementation of the solution code in two programming languages: Python and C++. + ## Problem Description Given a number N. Your task is to check whether it is fascinating or not. @@ -22,7 +24,7 @@ Fascinating Number: When a number(should contain 3 digits or more) is multiplied ## Examples -Example 1: +**Example 1:** ``` Input: N = 192 @@ -30,7 +32,7 @@ Output: Fascinating Explanation: After multiplication with 2 and 3, and concatenating with original number, number will become 192384576 which contains all digits from 1 to 9. ``` -Example 2: +**Example 2:** ``` Input: N = 853 @@ -58,43 +60,52 @@ The problem is to determine if a given number N is a fascinating number. A fasci 3. Concatenate the original number, the product of the number and 2, and the product of the number and 3 into a single string. 4. The concatenated string should contain all digits from 1 to 9 exactly once, with no other digits present (e.g., no zeros). -## CPP Code - -```cpp -class Solution { -public: - bool fascinating(int n) { - int m2 = n * 2; - int m3 = n * 3; - string num = to_string(n) + to_string(m2) + to_string(m3); - sort(num.begin(), num.end()); - if (num.find("123456789") == string::npos) - return false; - else if (num.length() - num.find("123456789") > 9) - return false; - else return true; +## Code Implementation + + + + + ```py + class Solution: + + def fascinating(self, n): + m2 = n * 2 + m3 = n * 3 + num = str(n) + str(m2) + str(m3) + num = "".join(sorted(num)) + zero_count = num.count("0") + if (num.find("123456789") == -1): + return False + elif (len(num) - zero_count > 9): + return False + else: + return True + ``` + + + + + + ```cpp + class Solution { + public: + bool fascinating(int n) { + int m2 = n * 2; + int m3 = n * 3; + string num = to_string(n) + to_string(m2) + to_string(m3); + sort(num.begin(), num.end()); + if (num.find("123456789") == string::npos) + return false; + else if (num.length() - num.find("123456789") > 9) + return false; + else return true; } }; ``` -## Python Code - -```py -class Solution: - - def fascinating(self, n): - m2 = n * 2 - m3 = n * 3 - num = str(n) + str(m2) + str(m3) - num = "".join(sorted(num)) - zero_count = num.count("0") - if (num.find("123456789") == -1): - return False - elif (len(num) - zero_count > 9): - return False - else: - return True -``` + + + ## Example Walkthrough @@ -124,4 +135,10 @@ The time complexity is $O(1)$ because the operations involve a fixed number of s ## Space Complexity -The space complexity is $O(1)$ as well since the operations use a constant amount of extra space for storing the products and concatenated strings. \ No newline at end of file +The space complexity is $O(1)$ as well since the operations use a constant amount of extra space for storing the products and concatenated strings. + +## References + +- **LeetCode Problem:** [Geeks for Geeks Problem](https://www.geeksforgeeks.org/problems/fascinating-number3751/1?page=1&difficulty=School&sortBy=difficulty) +- **Solution Link:** [Fascinating Number on Geeks for Geeks](https://www.geeksforgeeks.org/problems/fascinating-number3751/1?page=1&difficulty=School&sortBy=difficulty) +- **Authors LeetCode Profile:** [Anoushka](https://www.geeksforgeeks.org/user/iamanolive/) \ No newline at end of file