Fascinating Number

Given a number N with three or more digits, determine if it is a fascinating number. A fascinating number is one that when multiplied by 2 and 3, and the resulting products are concatenated with the original number, contains all digits from 1 to 9 exactly once.


Example 1:

Input: 
N = 192
Output: Fascinating
Explanation: After multiplying the original number by 2 and 3, and concatenating it with the original number, the resulting number is 192384576. This number contains all the digits from 1 to 9 exactly once.


Example 2:

Input: 
N = 853
Output: Not Fascinating
Explanation: It's not a fascinating
number.



Method 1:

1)Compute n*2 and n*3 and convert them to string.
2)Concatenate the two strings.
3)Concatenate the original number n with the two concatenated strings.
4)Create a set and store unique characters.
5)Iterate each character in the total concatenated strings and check if the current character is '0'. If so, it means the concatenated string contains a '0', and according to the fascinating number criteria, it should not have any '0'. In this case, the function returns false.
6)If the current character is not '0', it adds the character to the set, ensuring that only unique characters are stored.
7)After iterating over all the characters in the total concatenated string, it checks if the size of the set is equal to the length of the concatenated string. If they are equal, it means all the digits from 1 to 9 are present exactly once in the concatenated string, satisfying the fascinating number criteria. In this case, the function returns true.
8)If any of the checks fail, the function returns false.



Java implementation:

class codemummy {
    boolean fascinating(long n) {
        String multiply2=String.valueOf(n*2);
        String multiply3=String.valueOf(n*3);
        String multiply=multiply2+multiply3;
        String multi=String.valueOf(n)+multiply;
        HashSet<Character> hs=new HashSet<>();
        for(int i=0;i<multi.length();i++){
            if(multi.charAt(i)=='0'){
               return false;
            }
            hs.add(multi.charAt(i));
        }
        return (hs.size()==multi.length());
    }
}

 Time Complexity: O(log N)
 Auxiliary Space: O(1).




C++ implementation:

#include <bits/stdc++.h> using namespace std; bool fascinating(long long n) { string multiply2 = to_string(n * 2); string multiply3 = to_string(n * 3); string multiply = multiply2 + multiply3; string multi = to_string(n) + multiply; unordered_set < char > hs; for (int i = 0; i < multi.length(); i++) { if (multi[i] == '0') { return false; } hs.insert(multi[i]); } return (hs.size() == multi.length()); }

 Time Complexity: O(log N)
 Auxiliary Space: O(1).




Python implementation:

def fascinating(n):
    multiply2 = str(n * 2)
    multiply3 = str(n * 3)
    multiply = multiply2 + multiply3
    multi = str(n) + multiply
    hs = set()
    for digit in multi:
        if digit == '0':
            return False
        hs.add(digit)
    return len(hs) == len(multi)

 Time Complexity: O(log N)
 Auxiliary Space: O(1).







No comments

darkmode