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++
#include<bits/stdc++.h>
using namespace std;
bool checkPandigital(int b, char n[])
{
if (strlen(n) < b)
return false;
bool hash[b];
memset(hash, false, sizeof(hash));
for (int i = 0; i < strlen(n); i++)
{
if (n[i] >= '0' && n[i] <= '9')
hash[n[i] - '0'] = true;
else if (n[i] - 'A' <= b - 11)
hash[n[i] - 'A' + 10] = true;
}
for (int i = 0; i < b; i++)
if (hash[i] == false)
return false;
return true;
}
int main()
{
int b = 13;
char n[] = "1298450376ABC";
(checkPandigital(b, n))? (cout << "Yes" << endl):
(cout << "No" << endl);
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean checkPandigital(int b, String n) {
if (n.length() < b)
return false;
boolean hash[] = new boolean[b];
Arrays.fill(hash, false);
for (int i = 0; i < n.length(); i++) {
if (n.charAt(i) >= '0' && n.charAt(i) <= '9')
hash[n.charAt(i) - '0'] = true;
else if (n.charAt(i) - 'A' <= b - 11)
hash[n.charAt(i) - 'A' + 10] = true;
}
for (int i = 0; i < b; i++)
if (hash[i] == false)
return false;
return true;
}
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");
}
}
|
Python3
def checkPandigital(b, n):
if (len(n) < b):
return 0;
hash = [0] * b;
for i in range(len(n)):
if (n[i] >= '0' and n[i] <= '9'):
hash[ord(n[i]) - ord('0')] = 1;
elif (ord(n[i]) - ord('A') <= b - 11):
hash[ord(n[i]) - ord('A') + 10] = 1;
for i in range(b):
if (hash[i] == 0):
return 0;
return 1;
b = 13;
n = "1298450376ABC";
if(checkPandigital(b, n)):
print("Yes");
else:
print("No");
|
C#
using System;
class GFG {
static bool checkPandigital(int b, string n) {
if (n.Length < b)
return false;
bool []hash = new bool[b];
for(int i = 0; i < b; i++)
hash[i] = false;
for (int i = 0; i < n.Length; i++) {
if (n[i] >= '0' && n[i] <= '9')
hash[n[i] - '0'] = true;
else if (n[i] - 'A' <= b - 11)
hash[n[i] - 'A' + 10] = true;
}
for (int i = 0; i < b; i++)
if (hash[i] == false)
return false;
return true;
}
public static void Main()
{
int b = 13;
String n = "1298450376ABC";
if (checkPandigital(b, n))
Console.Write("Yes");
else
Console.Write("No");
}
}
|
PHP
<?php
function checkPandigital($b, $n)
{
if (strlen($n) < $b)
return 0;
$hash = array();
for($i = 0; $i< $b; $i++)
$hash[$i] = 0;
for ($i = 0; $i < strlen($n); $i++)
{
if ($n[$i] >= '0' && $n[$i] <= '9')
$hash[$n[$i] - '0'] = 1;
else if (ord($n[$i]) - ord('A') <= $b - 11)
$hash[ord($n[$i]) - ord('A') + 10] = 1;
}
for ($i = 0; $i < $b; $i++)
if ($hash[$i] == 0)
return 0;
return 1;
}
$b = 13;
$n = "1298450376ABC";
if(checkPandigital($b, $n))
echo "Yes";
else
echo "No";
?>
|
Javascript
<script>
function checkPandigital(b, n)
{
if (n.length < b)
return 0;
let hash = [];
for(let i = 0; i< b; i++)
hash[i] = 0;
for (let i = 0; i < n.length; i++)
{
if (n[i] >= '0' && n[i] <= '9')
hash[n[i] - '0'] = 1;
else if (n.charCodeAt(i) - 'A'.charCodeAt(0) <= b - 11)
hash[n.charCodeAt(i) - 'A'.charCodeAt(0) + 10] = 1;
}
for (let i = 0; i < b; i++)
if (hash[i] == 0)
return 0;
return 1;
}
let b = 13;
let n = "1298450376ABC";
if(checkPandigital(b, n))
document.write("Yes");
else
document.write("No");
</script>
|
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 write.geeksforgeeks.org or mail your article to review-team@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.