|
| 1 | +--- |
| 2 | +id: fascinating-number |
| 3 | +title: Fascinating Number Problem (Geeks for Geeks) |
| 4 | +sidebar_label: 0002 - Fascinating Number |
| 5 | +tags: |
| 6 | + - Beginner |
| 7 | + - String |
| 8 | + - Find |
| 9 | + - Multiplication |
| 10 | + - Geeks for Geeks |
| 11 | + - CPP |
| 12 | + - Python |
| 13 | + - DSA |
| 14 | +description: "This is a solution to the Fascinating Number problem on Geeks for Geeks." |
| 15 | +--- |
| 16 | + |
| 17 | +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++. |
| 18 | + |
| 19 | +## Problem Description |
| 20 | + |
| 21 | +Given a number N. Your task is to check whether it is fascinating or not. |
| 22 | + |
| 23 | +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. |
| 24 | + |
| 25 | +## Examples |
| 26 | + |
| 27 | +**Example 1:** |
| 28 | + |
| 29 | +``` |
| 30 | +Input: N = 192 |
| 31 | +Output: Fascinating |
| 32 | +Explanation: After multiplication with 2 and 3, and concatenating with original number, number will become 192384576 which contains all digits from 1 to 9. |
| 33 | +``` |
| 34 | + |
| 35 | +**Example 2:** |
| 36 | + |
| 37 | +``` |
| 38 | +Input: N = 853 |
| 39 | +Output: Not Fascinating |
| 40 | +Explanation: It's not a fascinating number. |
| 41 | +``` |
| 42 | + |
| 43 | +## Your Task |
| 44 | + |
| 45 | +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. |
| 46 | + |
| 47 | +Expected Time Complexity: $O(1)$ |
| 48 | +Expected Auxiliary Space: $O(1)$ |
| 49 | + |
| 50 | +## Constraints |
| 51 | + |
| 52 | +`100 <= N <= 2*10^9` |
| 53 | + |
| 54 | +## Problem Explanation |
| 55 | + |
| 56 | +The problem is to determine if a given number N is a fascinating number. A fascinating number is defined as follows: |
| 57 | + |
| 58 | +1. The number must have at least three digits. |
| 59 | +2. Multiply the number by 2 and 3 to get two products. |
| 60 | +3. Concatenate the original number, the product of the number and 2, and the product of the number and 3 into a single string. |
| 61 | +4. The concatenated string should contain all digits from 1 to 9 exactly once, with no other digits present (e.g., no zeros). |
| 62 | + |
| 63 | +## Code Implementation |
| 64 | + |
| 65 | +<Tabs> |
| 66 | + <TabItem value="Python" label="Python" default> |
| 67 | + <SolutionAuthor name="@iamanolive"/> |
| 68 | + ```py |
| 69 | + class Solution: |
| 70 | + |
| 71 | + def fascinating(self, n): |
| 72 | + m2 = n * 2 |
| 73 | + m3 = n * 3 |
| 74 | + num = str(n) + str(m2) + str(m3) |
| 75 | + num = "".join(sorted(num)) |
| 76 | + zero_count = num.count("0") |
| 77 | + if (num.find("123456789") == -1): |
| 78 | + return False |
| 79 | + elif (len(num) - zero_count > 9): |
| 80 | + return False |
| 81 | + else: |
| 82 | + return True |
| 83 | + ``` |
| 84 | + |
| 85 | + </TabItem> |
| 86 | + <TabItem value="C++" label="C++"> |
| 87 | + <SolutionAuthor name="@iamanolive"/> |
| 88 | + |
| 89 | + ```cpp |
| 90 | + class Solution { |
| 91 | + public: |
| 92 | + bool fascinating(int n) { |
| 93 | + int m2 = n * 2; |
| 94 | + int m3 = n * 3; |
| 95 | + string num = to_string(n) + to_string(m2) + to_string(m3); |
| 96 | + sort(num.begin(), num.end()); |
| 97 | + if (num.find("123456789") == string::npos) |
| 98 | + return false; |
| 99 | + else if (num.length() - num.find("123456789") > 9) |
| 100 | + return false; |
| 101 | + else return true; |
| 102 | + } |
| 103 | +}; |
| 104 | +``` |
| 105 | + |
| 106 | + </TabItem> |
| 107 | +</Tabs> |
| 108 | + |
| 109 | + |
| 110 | +## Example Walkthrough |
| 111 | + |
| 112 | +For N = 192: |
| 113 | +1. Original number: 192 |
| 114 | +2. Multiply by 2: 192 × 2 = 384 |
| 115 | +3. Multiply by 3: 192 × 3 = 576 |
| 116 | +4. Concatenate: "192" + "384" + "576" = "192384576" |
| 117 | +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. |
| 118 | + |
| 119 | +Therefore, 192 is a fascinating number. |
| 120 | + |
| 121 | +## Solution Logic: |
| 122 | + |
| 123 | +1. Compute the Products: Multiply the number N by 2 and 3 to get two new numbers. |
| 124 | +2. Concatenate the Results: Convert the original number and the two products to strings and concatenate them. |
| 125 | +3. Sort and Check Digits: Sort the concatenated string and check if it contains the sequence "123456789" exactly once. |
| 126 | +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. |
| 127 | + |
| 128 | +## Time Complexity |
| 129 | + |
| 130 | +The time complexity is $O(1)$ because the operations involve a fixed number of steps regardless of the size of N: |
| 131 | + |
| 132 | +* Multiplication and string concatenation are constant time operations. |
| 133 | +* Sorting a string of fixed length (at most 9 characters) is a constant time operation. |
| 134 | +* Checking for the sequence "123456789" in a fixed-length string is also a constant time operation. |
| 135 | + |
| 136 | +## Space Complexity |
| 137 | + |
| 138 | +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. |
| 139 | + |
| 140 | +## References |
| 141 | + |
| 142 | +- **LeetCode Problem:** [Geeks for Geeks Problem](https://www.geeksforgeeks.org/problems/fascinating-number3751/1?page=1&difficulty=School&sortBy=difficulty) |
| 143 | +- **Solution Link:** [Fascinating Number on Geeks for Geeks](https://www.geeksforgeeks.org/problems/fascinating-number3751/1?page=1&difficulty=School&sortBy=difficulty) |
| 144 | +- **Authors LeetCode Profile:** [Anoushka](https://www.geeksforgeeks.org/user/iamanolive/) |
0 commit comments