You need to determine if a number is “fascinating.” A number is considered fascinating if, when concatenated with its double and triple, the result contains all digits from 1 to 9 exactly once and does not contain the digit 0.
For simplification, let’s assume the input number is a positive integer and we need to check it directly as described.
n
, 2 * n
, and 3 * n
.function isFascinating(n):
concatenated = str(n) + str(2*n) + str(3*n)
if length of concatenated is not 9:
return False
required_digits = set('123456789')
return set(concatenated) == required_digits
Now, let’s write the Python code for this.
def is_fascinating(n: int) -> bool:
concatenated = str(n) + str(2 * n) + str(3 * n)
if len(concatenated) != 9:
return False
required_digits = set('123456789')
return set(concatenated) == required_digits
# Example Usage
print(is_fascinating(192)) # True
print(is_fascinating(100)) # False
The time complexity of this approach is O(1) because:
This ensures that our solution is efficient and operates in constant time relative to the input size.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?