|
| 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 | +## Problem Description |
| 18 | + |
| 19 | +Given a number N. Your task is to check whether it is fascinating or not. |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +## Examples |
| 24 | + |
| 25 | +Example 1: |
| 26 | + |
| 27 | +Input: N = 192 |
| 28 | +Output: Fascinating |
| 29 | +Explanation: After multiplication with 2 and 3, and concatenating with original number, number will become 192384576 which contains all digits from 1 to 9. |
| 30 | + |
| 31 | +Example 2: |
| 32 | + |
| 33 | +Input: N = 853 |
| 34 | +Output: Not Fascinating |
| 35 | +Explanation: It's not a fascinating number. |
| 36 | + |
| 37 | +## Your Task |
| 38 | + |
| 39 | +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. |
| 40 | + |
| 41 | +Expected Time Complexity: $O(1)$ |
| 42 | +Expected Auxiliary Space: $O(1)$ |
| 43 | + |
| 44 | +## Constraints |
| 45 | + |
| 46 | +100 <= N <= 2*10^9 |
| 47 | + |
| 48 | +## Problem Explanation |
| 49 | + |
| 50 | +The problem is to determine if a given number N is a fascinating number. A fascinating number is defined as follows: |
| 51 | + |
| 52 | +1. The number must have at least three digits. |
| 53 | +2. Multiply the number by 2 and 3 to get two products. |
| 54 | +3. Concatenate the original number, the product of the number and 2, and the product of the number and 3 into a single string. |
| 55 | +4. The concatenated string should contain all digits from 1 to 9 exactly once, with no other digits present (e.g., no zeros). |
| 56 | + |
| 57 | +## CPP Code |
| 58 | + |
| 59 | +```cpp |
| 60 | +class Solution { |
| 61 | +public: |
| 62 | + bool fascinating(int n) { |
| 63 | + int m2 = n * 2; |
| 64 | + int m3 = n * 3; |
| 65 | + string num = to_string(n) + to_string(m2) + to_string(m3); |
| 66 | + sort(num.begin(), num.end()); |
| 67 | + if (num.find("123456789") == string::npos) |
| 68 | + return false; |
| 69 | + else if (num.length() - num.find("123456789") > 9) |
| 70 | + return false; |
| 71 | + else return true; |
| 72 | + } |
| 73 | +}; |
| 74 | +``` |
| 75 | +
|
| 76 | +## Python Code |
| 77 | +
|
| 78 | +```py |
| 79 | +class Solution: |
| 80 | +
|
| 81 | + def fascinating(self, n): |
| 82 | + m2 = n * 2 |
| 83 | + m3 = n * 3 |
| 84 | + num = str(n) + str(m2) + str(m3) |
| 85 | + num = "".join(sorted(num)) |
| 86 | + zero_count = num.count("0") |
| 87 | + if (num.find("123456789") == -1): |
| 88 | + return False |
| 89 | + elif (len(num) - zero_count > 9): |
| 90 | + return False |
| 91 | + else: |
| 92 | + return True |
| 93 | +``` |
| 94 | + |
| 95 | +## Example Walkthrough |
| 96 | + |
| 97 | +For N = 192: |
| 98 | +1. Original number: 192 |
| 99 | +2. Multiply by 2: 192 × 2 = 384 |
| 100 | +3. Multiply by 3: 192 × 3 = 576 |
| 101 | +4. Concatenate: "192" + "384" + "576" = "192384576" |
| 102 | +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. |
| 103 | + |
| 104 | +Therefore, 192 is a fascinating number. |
| 105 | + |
| 106 | +## Solution Logic: |
| 107 | + |
| 108 | +1. Compute the Products: Multiply the number N by 2 and 3 to get two new numbers. |
| 109 | +2. Concatenate the Results: Convert the original number and the two products to strings and concatenate them. |
| 110 | +3. Sort and Check Digits: Sort the concatenated string and check if it contains the sequence "123456789" exactly once. |
| 111 | +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. |
| 112 | + |
| 113 | +## Time Complexity |
| 114 | + |
| 115 | +The time complexity is $O(1)$ because the operations involve a fixed number of steps regardless of the size of N: |
| 116 | + |
| 117 | +* Multiplication and string concatenation are constant time operations. |
| 118 | +* Sorting a string of fixed length (at most 9 characters) is a constant time operation. |
| 119 | +* Checking for the sequence "123456789" in a fixed-length string is also a constant time operation. |
| 120 | + |
| 121 | +## Space Complexity |
| 122 | + |
| 123 | +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. |
0 commit comments