You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dsa-solutions/gfg-solutions/0002-fascinating-number.md
+53-36Lines changed: 53 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,8 @@ tags:
14
14
description: "This is a solution to the Fascinating Number problem on Geeks for Geeks."
15
15
---
16
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
+
17
19
## Problem Description
18
20
19
21
Given a number N. Your task is to check whether it is fascinating or not.
@@ -22,15 +24,15 @@ Fascinating Number: When a number(should contain 3 digits or more) is multiplied
22
24
23
25
## Examples
24
26
25
-
Example 1:
27
+
**Example 1:**
26
28
27
29
```
28
30
Input: N = 192
29
31
Output: Fascinating
30
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.
31
33
```
32
34
33
-
Example 2:
35
+
**Example 2:**
34
36
35
37
```
36
38
Input: N = 853
@@ -58,43 +60,52 @@ The problem is to determine if a given number N is a fascinating number. A fasci
58
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.
59
61
4. The concatenated string should contain all digits from 1 to 9 exactly once, with no other digits present (e.g., no zeros).
60
62
61
-
## CPP Code
62
-
63
-
```cpp
64
-
classSolution {
65
-
public:
66
-
bool fascinating(int n) {
67
-
int m2 = n * 2;
68
-
int m3 = n * 3;
69
-
string num = to_string(n) + to_string(m2) + to_string(m3);
70
-
sort(num.begin(), num.end());
71
-
if (num.find("123456789") == string::npos)
72
-
return false;
73
-
else if (num.length() - num.find("123456789") > 9)
74
-
return false;
75
-
else return true;
63
+
## Code Implementation
64
+
65
+
<Tabs>
66
+
<TabItemvalue="Python"label="Python"default>
67
+
<SolutionAuthorname="@iamanolive"/>
68
+
```py
69
+
classSolution:
70
+
71
+
deffascinating(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
+
returnFalse
79
+
elif (len(num) - zero_count >9):
80
+
returnFalse
81
+
else:
82
+
returnTrue
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
+
elseif (num.length() - num.find("123456789") >9)
100
+
return false;
101
+
elsereturn true;
76
102
}
77
103
};
78
104
```
79
105
80
-
## Python Code
81
-
82
-
```py
83
-
class Solution:
84
-
85
-
def fascinating(self, n):
86
-
m2 = n * 2
87
-
m3 = n * 3
88
-
num = str(n) + str(m2) + str(m3)
89
-
num = "".join(sorted(num))
90
-
zero_count = num.count("0")
91
-
if (num.find("123456789") == -1):
92
-
return False
93
-
elif (len(num) - zero_count > 9):
94
-
return False
95
-
else:
96
-
return True
97
-
```
106
+
</TabItem>
107
+
</Tabs>
108
+
98
109
99
110
## Example Walkthrough
100
111
@@ -124,4 +135,10 @@ The time complexity is $O(1)$ because the operations involve a fixed number of s
124
135
125
136
## Space Complexity
126
137
127
-
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.
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)
0 commit comments