Fascinating Number

Last Updated : 18 Jul, 2026

Given a number n, check whether it is fascinating or not.  A number with 3 or more digits is considered fascinating if, when it is multiplied by 2 and 3, and the resulting products are concatenated with the original number, the final sequence contains all the digits from 1 to 9 exactly once.

Examples: 

Input: n = 192
Output: true
Explanation: After multiplication with 2 and 3, and concatenating with original number, number will become 192384576 which contains all digits from 1 to 9.

Input: n = 853
Output: false
Explanation: It is not a fascinating number.

Try It Yourself
redirect icon

Using Concatenated String and Check Frequencies - O(D) Time and O(1) Space

The idea is to concatenate the original number with its multiples by 2 and 3 into a string. Then count the frequency of every digit. If the digit 0 appears or any digit from 1 to 9 does not appear exactly once, the number is not fascinating. Otherwise, it is fascinating.

Working of Approach:

  • Form a string by concatenating n, 2 × n, and 3 × n.
  • If the concatenated string does not contain exactly 9 digits, return false.
  • Count the frequency of every digit using a frequency array of size 10.
  • If digit 0 is present or any digit from 1 to 9 does not appear exactly once, return false.
  • Otherwise, all digits 1 to 9 are present exactly once, so return true.
C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;

bool fascinating(int n)
{

    // Form the concatenated string.
    string s = to_string(n) + to_string(2 * n) + to_string(3 * n);

    // Fascinating number must have exactly 9 digits.
    if (s.size() != 9)
        return false;

    // Store frequency of each digit.
    vector<int> freq(10, 0);

    for (char ch : s)
        freq[ch - '0']++;

    // Digit 0 should not be present.
    if (freq[0] > 0)
        return false;

    // Digits 1 to 9 should appear exactly once.
    for (int i = 1; i <= 9; i++)
    {
        if (freq[i] != 1)
            return false;
    }

    return true;
}

int main()
{
    int n = 192;
    cout << (fascinating(n) ? "true" : "false") << endl;
    return 0;
}
Java
import java.util.*;

class GFG {

    static boolean fascinating(long n)
    {

        // Form the concatenated string.
        String s = Long.toString(n) + Long.toString(2 * n)
                   + Long.toString(3 * n);

        // Fascinating number must have exactly 9 digits.
        if (s.length() != 9)
            return false;

        // Store frequency of each digit.
        int[] freq = new int[10];

        for (char ch : s.toCharArray())
            freq[ch - '0']++;

        // Digit 0 should not be present.
        if (freq[0] > 0)
            return false;

        // Digits 1 to 9 should appear exactly once.
        for (int i = 1; i <= 9; i++) {
            if (freq[i] != 1)
                return false;
        }

        return true;
    }

    public static void main(String[] args)
    {
        long n = 192;
        System.out.println(fascinating(n) ? "true"
                                          : "false");
    }
}
Python
def fascinating(n):

    # Form the concatenated string.
    s = str(n) + str(2 * n) + str(3 * n)

    # Fascinating number must have exactly 9 digits.
    if len(s) != 9:
        return False

    # Store frequency of each digit.
    freq = [0] * 10

    for ch in s:
        freq[int(ch)] += 1

    # Digit 0 should not be present.
    if freq[0] > 0:
        return False

    # Digits 1 to 9 should appear exactly once.
    for i in range(1, 10):
        if freq[i] != 1:
            return False

    return True


if __name__ == '__main__':
    n = 192
    print('true' if fascinating(n) else 'false')
C#
using System;

class GFG {

    static bool fascinating(int n)
    {

        // Form the concatenated string.
        string s = n.ToString() + (2 * n).ToString()
                   + (3 * n).ToString();

        // Fascinating number must have exactly 9 digits.
        if (s.Length != 9)
            return false;

        // Store frequency of each digit.
        int[] freq = new int[10];

        foreach(char ch in s) freq[ch - '0']++;

        // Digit 0 should not be present.
        if (freq[0] > 0)
            return false;

        // Digits 1 to 9 should appear exactly once.
        for (int i = 1; i <= 9; i++) {
            if (freq[i] != 1)
                return false;
        }

        return true;
    }

    static void Main()
    {
        int n = 192;
        Console.WriteLine(fascinating(n) ? "true"
                                         : "false");
    }
}
JavaScript
function fascinating(n)
{

    // Form the concatenated string.
    let s = n.toString() + (2 * n).toString()
            + (3 * n).toString();

    // Fascinating number must have exactly 9 digits.
    if (s.length !== 9)
        return false;

    // Store frequency of each digit.
    let freq = new Array(10).fill(0);

    for (let ch of s)
        freq[parseInt(ch)]++;

    // Digit 0 should not be present.
    if (freq[0] > 0)
        return false;

    // Digits 1 to 9 should appear exactly once.
    for (let i = 1; i <= 9; i++) {
        if (freq[i] !== 1)
            return false;
    }

    return true;
}

// Driver Code
let n = 192;
console.log(fascinating(n) ? "true" : "false");

Output
true

Using Bit Mask - O(D) Time and O(1) Space

The idea is to process the digits of n, 2*n, and 3*n one by one without forming a string. A bit mask keeps track of the digits encountered. If a digit is 0 or appears more than once, return false. At the end, verify that all digits from 1 to 9 have been seen exactly once.

Working of Approach:

  • Process the digits of n, 2 × n, and 3 × n one by one without forming a string.
  • Use a bit mask to keep track of the digits that have already appeared.
  • If a digit is 0 or its corresponding bit is already set, return false since the number cannot be fascinating.
  • Set the bit corresponding to every valid digit encountered while extracting digits.
  • After processing all three numbers, check if the bit mask has all bits from 1 to 9 set. If yes, return true; otherwise, return false.
C++
#include <iostream>
using namespace std;

bool checkDigits(int x, int &mask)
{

    while (x > 0)
    {
        int d = x % 10;

        // Digit 0 is not allowed.
        if (d == 0)
            return false;

        // Digit already present.
        if (mask & (1 << d))
            return false;

        mask |= (1 << d);
        x /= 10;
    }

    return true;
}

bool fascinating(int n)
{

    int mask = 0;

    if (!checkDigits(n, mask))
        return false;

    if (!checkDigits(2 * n, mask))
        return false;

    if (!checkDigits(3 * n, mask))
        return false;

    // Bits 1 to 9 should be set.
    return mask == ((1 << 10) - 2);
}

int main()
{
    int n = 192;

    cout << (fascinating(n) ? "true" : "false") << endl;

    return 0;
}
Java
import java.util.*;

class GFG {

    static boolean checkDigits(long x, int[] mask) {

        while (x > 0) {
            int d = (int)(x % 10);

            // Digit 0 is not allowed.
            if (d == 0)
                return false;

            // Digit already present.
            if ((mask[0] & (1 << d)) != 0)
                return false;

            mask[0] |= (1 << d);
            x /= 10;
        }

        return true;
    }

    static boolean fascinating(long n) {

        int[] mask = new int[1];

        if (!checkDigits(n, mask))
            return false;

        if (!checkDigits(2 * n, mask))
            return false;

        if (!checkDigits(3 * n, mask))
            return false;

        // Bits 1 to 9 should be set.
        return mask[0] == ((1 << 10) - 2);
    }

    public static void main(String[] args) {

        long n = 192;

        System.out.println(fascinating(n) ? "true" : "false");
    }
}
Python
def checkDigits(x, mask):

    while x > 0:
        d = x % 10

        # Digit 0 is not allowed.
        if d == 0:
            return False

        # Digit already present.
        if mask[0] & (1 << d):
            return False

        mask[0] |= (1 << d)
        x //= 10

    return True


def fascinating(n):

    mask = [0]

    if not checkDigits(n, mask):
        return False

    if not checkDigits(2 * n, mask):
        return False

    if not checkDigits(3 * n, mask):
        return False

    # Bits 1 to 9 should be set.
    return mask[0] == ((1 << 10) - 2)


if __name__ == "__main__":
    n = 192

    print("true" if fascinating(n) else "false")
C#
using System;

public class GFG {
    public static bool CheckDigits(int x, ref int mask)
    {
        while (x > 0) {
            int d = x % 10;

            // Digit 0 is not allowed.
            if (d == 0)
                return false;

            // Digit already present.
            if ((mask & (1 << d)) != 0)
                return false;

            mask |= (1 << d);
            x /= 10;
        }

        return true;
    }

    public static bool fascinating(int n)
    {
        int mask = 0;

        if (!CheckDigits(n, ref mask))
            return false;

        if (!CheckDigits(2 * n, ref mask))
            return false;

        if (!CheckDigits(3 * n, ref mask))
            return false;

        // Bits 1 to 9 should be set.
        return mask == ((1 << 10) - 2);
    }

    public static void Main()
    {
        int n = 192;

        Console.WriteLine(fascinating(n) ? "true"
                                         : "false");
    }
}
JavaScript
function checkDigits(x, mask)
{

    while (x > 0) {
        let d = x % 10;

        // Digit 0 is not allowed.
        if (d === 0)
            return false;

        // Digit already present.
        if (mask.value & (1 << d))
            return false;

        mask.value |= (1 << d);
        x = Math.floor(x / 10);
    }

    return true;
}

function fascinating(n)
{

    let mask = {value : 0};

    if (!checkDigits(n, mask))
        return false;

    if (!checkDigits(2 * n, mask))
        return false;

    if (!checkDigits(3 * n, mask))
        return false;

    // Bits 1 to 9 should be set.
    return mask.value === ((1 << 10) - 2);
}

// Driver code
let n = 192;
console.log(fascinating(n) ? "true" : "false");

Output
true
Comment