Skip to content

Commit 28654c2

Browse files
committed
updates
1 parent 394c707 commit 28654c2

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

dsa-solutions/gfg-solutions/0002-fascinating-number.md

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ tags:
1414
description: "This is a solution to the Fascinating Number problem on Geeks for Geeks."
1515
---
1616

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+
1719
## Problem Description
1820

1921
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
2224

2325
## Examples
2426

25-
Example 1:
27+
**Example 1:**
2628

2729
```
2830
Input: N = 192
2931
Output: Fascinating
3032
Explanation: After multiplication with 2 and 3, and concatenating with original number, number will become 192384576 which contains all digits from 1 to 9.
3133
```
3234

33-
Example 2:
35+
**Example 2:**
3436

3537
```
3638
Input: N = 853
@@ -58,43 +60,52 @@ The problem is to determine if a given number N is a fascinating number. A fasci
5860
3. Concatenate the original number, the product of the number and 2, and the product of the number and 3 into a single string.
5961
4. The concatenated string should contain all digits from 1 to 9 exactly once, with no other digits present (e.g., no zeros).
6062

61-
## CPP Code
62-
63-
```cpp
64-
class Solution {
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+
<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;
76102
}
77103
};
78104
```
79105

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+
98109

99110
## Example Walkthrough
100111

@@ -124,4 +135,10 @@ The time complexity is $O(1)$ because the operations involve a fixed number of s
124135

125136
## Space Complexity
126137

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)
144+
- **Authors LeetCode Profile:** [Anoushka](https://www.geeksforgeeks.org/user/iamanolive/)

0 commit comments

Comments
 (0)