Check if The Number is Fascinating - Problem

You are given an integer n that consists of exactly 3 digits.

We call the number n fascinating if, after the following modification, the resulting number contains all the digits from 1 to 9 exactly once and does not contain any 0's:

Concatenate n with the numbers 2 * n and 3 * n.

Return true if n is fascinating, or false otherwise.

Note: Concatenating two numbers means joining them together. For example, the concatenation of 121 and 371 is 121371.

Input & Output

Example 1 — Fascinating Number
$ Input: n = 192
Output: true
💡 Note: Concatenating 192 with 2×192=384 and 3×192=576 gives us 192384576. This contains all digits 1-9 exactly once with no zeros, making it fascinating.
Example 2 — Contains Zero
$ Input: n = 100
Output: false
💡 Note: Concatenating 100 with 2×100=200 and 3×100=300 gives us 100200300. This contains zeros, which violates the fascinating number rule.
Example 3 — Duplicate Digits
$ Input: n = 111
Output: false
💡 Note: Concatenating 111 with 2×111=222 and 3×111=333 gives us 111222333. This has duplicate digits (1 appears 3 times, 2 appears 3 times, 3 appears 3 times) instead of each digit 1-9 appearing exactly once.

Constraints

  • 100 ≤ n ≤ 999
  • n consists of exactly 3 digits

Visualization

Tap to expand
Check if The Number is Fascinating INPUT n = 192 (3-digit number) Concatenation Process: 192 n 384 2*n 576 3*n 192384576 9 digits to check 1 9 2 3 8 4 5 7 6 ALGORITHM STEPS 1 Extract Digits Process n, 2*n, 3*n using modulo (% 10) 2 Track with Bitmask Use bits 1-9 to mark seen digits Bit positions: 9 8 7 6 5 4 3 2 1 1 1 1 1 1 1 1 1 1 = 1022 3 Check No Zeros If digit == 0, return false immediately 4 Verify Bitmask Check if mask == 1022 (all 1-9 present once) mask |= (1 << digit) Set bit for each digit FINAL RESULT Digit Verification: 1: OK 2: OK 3: OK 4: OK 5: OK 6: OK 7: OK 8: OK 9: OK No zeros found - OK Final bitmask value: 1022 == 1022 Output: true 192 is fascinating! Key Insight: Using a bitmask (integer with bits 1-9) allows O(1) tracking of which digits have been seen. The target value 1022 (binary: 1111111110) represents all digits 1-9 present exactly once. Direct digit processing avoids string conversion overhead. Time: O(1), Space: O(1). TutorialsPoint - Check if The Number is Fascinating | Optimized - Direct Digit Processing
Asked in
Google 8 Amazon 5 Microsoft 3
12.5K Views
Medium Frequency
~15 min Avg. Time
247 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen