Pandigital number in a given base
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 9
Input : 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:
C++
// C++ program to check if a number is pandigital // in given base. #include<bits/stdc++.h> using namespace std; // Return true if n is pandigit else return false. bool checkPandigital(int b, char n[]) { // Checking length is less than base if (strlen(n) < b) return false; bool hash[b]; memset(hash, false, sizeof(hash)); // Traversing each digit of the number. for (int i = 0; i < strlen(n); i++) { // If digit is integer if (n[i] >= '0' && n[i] <= '9') hash[n[i] - '0'] = true; // If digit is alphabet else if (n[i] - 'A' <= b - 11) hash[n[i] - 'A' + 10] = true; } // Checking hash array, if any index is // unmarked. for (int i = 0; i < b; i++) if (hash[i] == false) return false; return true; } // Driven Program int main() { int b = 13; char n[] = "1298450376ABC"; (checkPandigital(b, n))? (cout << "Yes" << endl): (cout << "No" << endl); return 0; } |
Java
// Java program to check if a number // is pandigital in given base. import java.util.*; class GFG { // Return true if n is pandigit // else return false. static boolean checkPandigital(int b, String n) { // Checking length is less than base if (n.length() < b) return false; boolean hash[] = new boolean[b]; Arrays.fill(hash, false); // Traversing each digit of the number. for (int i = 0; i < n.length(); i++) { // If digit is integer if (n.charAt(i) >= '0' && n.charAt(i) <= '9') hash[n.charAt(i) - '0'] = true; // If digit is alphabet else if (n.charAt(i) - 'A' <= b - 11) hash[n.charAt(i) - 'A' + 10] = true; } // Checking hash array, if any // index is unmarked. for (int i = 0; i < b; i++) if (hash[i] == false) return false; return true; } // Driver code public static void main(String[] args) { int b = 13; String n = "1298450376ABC"; if (checkPandigital(b, n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 program to check if a number is # pandigital in given base. # Return true if n is pandigit else return false. def checkPandigital(b, n): # Checking length is less than base if (len(n) < b): return 0; hash = [0] * b; # Traversing each digit of the number. for i in range(len(n)): # If digit is integer if (n[i] >= '0' and n[i] <= '9'): hash[ord(n[i]) - ord('0')] = 1; # If digit is alphabet elif (ord(n[i]) - ord('A') <= b - 11): hash[ord(n[i]) - ord('A') + 10] = 1; # Checking hash array, if any index is # unmarked. for i in range(b): if (hash[i] == 0): return 0; return 1; # Driver Code b = 13; n = "1298450376ABC"; if(checkPandigital(b, n)): print("Yes"); else: print("No"); # This code is contributed by mits |
C#
// C# program to check if a number // is pandigital in given base. using System; class GFG { // Return true if n is pandigit // else return false. static bool checkPandigital(int b, string n) { // Checking length is less than base if (n.Length < b) return false; bool []hash = new bool[b]; for(int i = 0; i < b; i++) hash[i] = false; // Traversing each digit of the number. for (int i = 0; i < n.Length; i++) { // If digit is integer if (n[i] >= '0' && n[i] <= '9') hash[n[i] - '0'] = true; // If digit is alphabet else if (n[i] - 'A' <= b - 11) hash[n[i] - 'A' + 10] = true; } // Checking hash array, if any // index is unmarked. for (int i = 0; i < b; i++) if (hash[i] == false) return false; return true; } // Driver code public static void Main() { int b = 13; String n = "1298450376ABC"; if (checkPandigital(b, n)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by nitin mittal. |
PHP
<?php // php program to check if a number is pandigital // in given base. // Return true if n is pandigit else return false. function checkPandigital($b, $n) { // Checking length is less than base if (strlen($n) < $b) return 0; $hash = array(); for($i = 0; $i< $b; $i++) $hash[$i] = 0; // Traversing each digit of the number. for ($i = 0; $i < strlen($n); $i++) { // If digit is integer if ($n[$i] >= '0' && $n[$i] <= '9') $hash[$n[$i] - '0'] = 1; // If digit is alphabet else if (ord($n[$i]) - ord('A') <= $b - 11) $hash[ord($n[$i]) - ord('A') + 10] = 1; } // Checking hash array, if any index is // unmarked. for ($i = 0; $i < $b; $i++) if ($hash[$i] == 0) return 0; return 1; } // Driven Program $b = 13; $n = "1298450376ABC"; if(checkPandigital($b, $n)) echo "Yes"; else echo "No"; // This code is contributed by Sam007. ?> |
Output:
Yes
Reference:
https://en.wikipedia.org/wiki/Pandigital_number
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Given a number N in decimal base, find number of its digits in any base (base b)
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Complement of a number with any base b
- Check whether a number has consecutive 0's in the given base or not
- Check if a number N starts with 1 in b-base
- Pandigital Product
- Check if the number is even or odd whose digits and base (radix) is given
- Number of trailing zeroes in base B representation of N!
- Check if a given number can be represented in given a no. of digits in any base
- Number System and base conversions | Digital Electronics
- Check if a number is power of k using base changing method
- Double Base Palindrome
- Write a program to add two numbers in base 14
- Convert from any base to decimal and vice versa
- All possible numbers of N digits and base B without leading zeros



