We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Check if The Number is Fascinating

Number: 2824

Difficulty: Easy

Paid? No

Companies: N/A


Problem Description

Given an integer n with exactly 3 digits, determine if n is fascinating. A number is fascinating if, when you concatenate n, 2n, and 3n into one number, the resulting number contains every digit from 1 to 9 exactly once and does not contain any zeros.


Key Insights

  • n is a 3-digit number, so n, 2n, and 3n together will ideally form a 9-digit number.
  • Using concatenation, the resulting string must be checked for:
    • Having a total length of 9 digits.
    • Containing all digits from 1 to 9 exactly once.
    • Not containing the digit 0.
  • A straightforward approach is to generate the concatenated string and then verify it against "123456789".

Space and Time Complexity

Time Complexity: O(1)
Space Complexity: O(1)


Solution

The solution involves first creating the concatenated string by converting n, 2n, and 3n to strings and joining them. Since the length is fixed (9 characters), we simply need to check if sorting this string results in "123456789". This also ensures that each digit is present exactly once and that there are no zeros.


Code Solutions

# Function to check if a number is fascinating
def is_fascinating(n):
    # Create the concatenated string from n, 2*n, and 3*n
    concatenated_str = str(n) + str(n * 2) + str(n * 3)
    
    # Check if the concatenated string has exactly 9 characters and if sorting results in "123456789"
    return len(concatenated_str) == 9 and ''.join(sorted(concatenated_str)) == "123456789"

# Test cases
print(is_fascinating(192))  # Expected output: True
print(is_fascinating(100))  # Expected output: False
← Back to All Questions