Fascinating Number

14 May, 2021
Given a number N, the task is to check whether it is fascinating or not. Fascinating Number: When a number( 3 digits or more ) is multiplied by 2 and 3, and when both these products are concatenated with the original number, then it results in all digits from 1 to 9 present exactly once. There could be any number of zeros and are ignored. Examples:  
Input: 192 Output: Yes After multiplication with 2 and 3, and concatenating with original number, resultant number is 192384576 which contains all digits from 1 to 9.Input: 853 Output: No After multiplication with 2 and 3, and concatenating with original number, the resultant number is 85317062559. In this, number 4 is missing and the number 5 has appeared multiple times.  
 
Approach:  
Check if the given number has three digits or more. If not, print No.Else, Multiply the given number with 2 and 3.Concatenate these products with the given number to form a string.Traverse this string, keep the frequency count of the digits.Print No if any digit is missing or has appeared multiple times.Else, print Yes.
Check if the given number has three digits or more. If not, print No.
Else, Multiply the given number with 2 and 3.
Concatenate these products with the given number to form a string.
Traverse this string, keep the frequency count of the digits.
Print No if any digit is missing or has appeared multiple times.
Else, print Yes.
Below is the implementation of above approach:  
C++14
Java
Python 3
C#
PHP
Javascript
// C++ program to implement// fascinating number#include <bits/stdc++.h>using namespace std; // function to check if number// is fascinating or notbool isFascinating(int num){    // frequency count array    // using 1 indexing    int freq[10] = {0};     // obtaining the resultant number    // using string concatenation    string val = "" + to_string(num) +                      to_string(num * 2) +                      to_string(num * 3);     // Traversing the string    // character by character    for (int i = 0; i < val.length(); i++)    {         // gives integer value of        // a character digit        int digit = val[i] - '0';         // To check if any digit has        // appeared multiple times        if (freq[digit] and digit != 0 > 0)            return false;        else            freq[digit]++;    }     // Traversing through freq array to    // check if any digit was missing    for (int i = 1; i < 10; i++)    {        if (freq[i] == 0)            return false;    }    return true;} // Driver codeint main(){    // Input number    int num = 192;     // Not a valid number    if (num < 100)        cout << "No" << endl;     else    {        // Calling the function to        // check if input number        // is fascinating or not        bool ans = isFascinating(num);        if (ans)            cout << "Yes";        else            cout << "No";    }} // This code is contributed// by Subhadeep
// Java program to implement// fascinating numberimport java.io.*;import java.util.*; public class GFG {     // function to check if number    // is fascinating or not    public static boolean isFascinating(                                int num)    {                 // frequency count array        //using 1 indexing        int[] freq = new int[10];         // obtaining the resultant number        // using string concatenation        String val = "" + num + num * 2 +                                num * 3;         // Traversing the string character //by character        for (int i = 0; i < val.length(); i++)        {             // gives integer value of //a character digit            int digit = val.charAt(i) - '0';             // To check if any digit has            // appeared multiple times            if (freq[digit]>0 && digit != 0)                return false;            else                freq[digit]++;        }         // Traversing through freq array to        // check if any digit was missing        for (int i = 1; i < freq.length; i++)        {            if (freq[i] == 0)                return false;        }        return true;    }     // Driver code    public static void main(String args[])    {                 // Input number        int num = 192;         // Not a valid number        if (num < 100)            System.out.println("No");         else        {                         // Calling the function to check            // if input number is fascinating or not            boolean ans = isFascinating(num);            if (ans)                System.out.println("Yes");            else                System.out.println("No");        }    }}
# Python 3 program to implement# fascinating number # function to check if number# is fascinating or notdef isFascinating(num) :     # frequency count array    # using 1 indexing    freq = [0] * 10     # obtaining the resultant number    # using string concatenation    val = (str(num) + str(num * 2) +                      str(num * 3))     # Traversing the string    # character by character    for i in range(len(val)) :         # gives integer value of        # a character digit        digit = int(val[i])         # To check if any digit has        # appeared multiple times        if freq[digit] and digit != 0 > 0 :            return False        else :            freq[digit] += 1     # Traversing through freq array to    # check if any digit was missing    for i in range(1, 10) :         if freq[i] == 0 :            return False     return True # Driver Codeif __name__ == "__main__" :     # Input number    num = 192     # Not a valid number    if num < 100 :        print("No")     else :                 # Calling the function to        # check if input number        # is fascinating or not        ans = isFascinating(num)                 if ans :            print("Yes")        else :            print("No") # This code is contributed by ANKITRAI1
// C# program to implement// fascinating numberusing System; class GFG{ // function to check if number// is fascinating or notpublic static bool isFascinating(int num){    // frequency count array    // using 1 indexing    int[] freq = new int[10];     // obtaining the resultant number    // using string concatenation    String val = "" + num.ToString() +                     (num * 2).ToString() +                     (num * 3).ToString();     // Traversing the string    // character by character    for (int i = 0; i < val.Length; i++)    {         // gives integer value of        // a character digit        int digit = val[i] - '0';         // To check if any digit has        // appeared multiple times        if (freq[digit] && digit != 0 > 0 )            return false;        else            freq[digit]++;    }     // Traversing through freq array to    // check if any digit was missing    for (int i = 1; i < freq.Length; i++)    {        if (freq[i] == 0)            return false;    }    return true;} // Driver codestatic void Main(){    // Input number    int num = 192;     // Not a valid number    if (num < 100)        Console.WriteLine("No");     else    {        // Calling the function to check        // if input number is fascinating or not        bool ans = isFascinating(num);        if (ans)            Console.WriteLine("Yes");        else            Console.WriteLine("No");    }}} // This code is contributed by mits
<?php// PHP program to implement// fascinating number // function to check if number// is fascinating or notfunction isFascinating($num){    // frequency count array    // using 1 indexing    $freq = array_fill(0, 10, NULL);     // obtaining the resultant number    // using string concatenation    $val = "" . $num . ($num * 2).                       ($num * 3);     // Traversing the string    // character by character    for ($i = 0; $i < strlen($val); $i++)    {         // gives integer value of        // a character digit        $digit = $val[$i] - '0';         // To check if any digit has        // appeared multiple times        if ($freq[$digit] > 0 && $digit != 0)            return false;        else            $freq[$digit]++;    }     // Traversing through freq array to    // check if any digit was missing    for ($i = 1; $i < 10; $i++)    {        if ($freq[$i] == 0)            return false;    }    return true;} // Driver code // Input number$num = 192; // Not a valid numberif ($num < 100)    echo "No" ; else{    // Calling the function to    // check if input number    // is fascinating or not    $ans = isFascinating($num);    if ($ans)        echo "Yes";    else        echo "No";} // This code is contributed// by ChitraNayal?>
<script> // Javascript program to implement// fascinating number         // function to check if number    // is fascinating or not    function isFascinating(num)    {        // frequency count array        //using 1 indexing        let freq = new Array(10);        for(let i=0;i<freq.length;i++)        {            freq[i]=0;        }          // obtaining the resultant number        // using string concatenation        let val = "" + num + num * 2 +                                num * 3;          // Traversing the string character //by character        for (let i = 0; i < val.length; i++)        {              // gives integer value of //a character digit            let digit = val[i].charCodeAt(0) -            '0'.charCodeAt(0);              // To check if any digit has            // appeared multiple times            if (freq[digit]>0 && digit != 0)                return false;            else                freq[digit]++;        }          // Traversing through freq array to        // check if any digit was missing        for (let i = 1; i < freq.length; i++)        {            if (freq[i] == 0)                return false;        }        return true;    }         // Driver code              // Input number    let num = 192;    // Not a valid number    if (num < 100)        document.write("No");      else    {                      // Calling the function to check        // if input number is fascinating or not        let ans = isFascinating(num);        if (ans)            document.write("Yes");        else            document.write("No");    } // This code is contributed by rag2127 </script>
Yes
 
rachana soma
Mithun Kumar
tufan_gupta2000
ankthon
ukasp
sourav23
saunaknandi021000
rag2127
C-String-Question
Hash
Numbers
Arrays
Mathematical
Arrays
Hash
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
Chocolate Distribution Problem
Find duplicates in O(n) time and O(1) extra space | Set 1
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Coin Change | DP-7