![]() |
VOOZH | about |
Given an integer n and its base b. The task is to check if given number is Pandigital Number in the given base or not. A Pandigital number is an integer that has each digit of its base at least once.
It may be assumed that base is smaller than or equal to 36. In base 36, digits are [0, 1, ...9. A, B, ...Z]
Examples :
Input : n = "9651723480", b = 10
Output : Yes
Given number n has all digits from 0 to 9Input : n = "23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
b = 36
Output : No
Given number n doesn't have all digits in base 36. For example 1 is missing.
Make a boolean hash array of size equal to base of the number and initialize it with false. Now, iterate each digit of the number mark its corresponding index value as true in the hash array. In the end, check whether all the value in hash array are marked or not, if marked print "Yes" i.e Pandigital number else print "No".
Below is the implementation of this approach:
Yes
Time Complexity: O(b + strlen(n))
Auxiliary Space: O(b)
Reference:
https://en.wikipedia.org/wiki/Pandigital_number
This article is contributed by Anuj Chauhan.
Approach:
We can use set to check if a given number in a given base is pandigital or not. We create a set of all possible digits in the given base and check if the set of digits in the given number is equal to the set of all possible digits.
Create a set containing all the digits in the base.
Convert the given number to a string.
Convert the string to a set of characters.
Check if the two sets are equal.
Input: n = 9651723480 , b = 10 Output: Yes Given number n has all digits from 0 to 9 Input: n = 23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ , b = 36 Output: No
Time complexity: O(n)
Space complexity: O(b)