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:
// 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 ;
} // Driver Program int main()
{ int b = 13;
char n[] = "1298450376ABC" ;
(checkPandigital(b, n)) ? (cout << "Yes" << endl)
: (cout << "No" << endl);
return 0;
} |
// 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 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
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 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# 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. |
<script> // Javascript 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 (n.length < b)
return 0;
let hash = [];
for (let i = 0; i< b; i++)
hash[i] = 0;
// Traversing each digit of the number.
for (let i = 0; i < n.length; i++)
{
// If digit is integer
if (n[i] >= '0' && n[i] <= '9' )
hash[n[i] - '0' ] = 1;
// If digit is alphabet
else if (n.charCodeAt(i) - 'A' .charCodeAt(0) <= b - 11)
hash[n.charCodeAt(i) - 'A' .charCodeAt(0) + 10] = 1;
}
// Checking hash array, if any index is
// unmarked.
for (let i = 0; i < b; i++)
if (hash[i] == 0)
return 0;
return 1;
} // Driver Program let b = 13; let n = "1298450376ABC" ;
if (checkPandigital(b, n))
document.write( "Yes" );
else document.write( "No" );
// This code is contributed by _saurabh_jaiswal. </script> |
<?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;
} // Driver Program $b = 13;
$n = "1298450376ABC" ;
if (checkPandigital( $b , $n ))
echo "Yes" ;
else echo "No" ;
// This code is contributed by Sam007. ?> |
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.
Using set :
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.
#include <iostream> #include <string> #include <set> using namespace std;
// Function to check if a number is pandigital for a given base bool isPandigitalSet( const string& n, int b) {
// Create a set containing digits from 0 to (b-1)
set< char > digits;
for ( char i = '0' ; i < '0' + b; i++) {
digits.insert(i);
}
// Create a set of digits from the given number
set< char > numDigits(n.begin(), n.end());
// Check if the two sets are equal
return digits == numDigits;
} int main() {
// Test case 1
string n1 = "9651723480" ;
int b1 = 10;
cout << "Input: n = " << n1 << ", b = " << b1 << endl;
if (isPandigitalSet(n1, b1)) {
cout << "Output: Yes\nGiven number n has all digits from 0 to 9" << endl;
} else {
cout << "Output: No" << endl;
}
// Test case 2
string n2 = "23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
int b2 = 36;
cout << "\nInput: n = " << n2 << ", b = " << b2 << endl;
if (isPandigitalSet(n2, b2)) {
cout << "Output: Yes\nGiven number n has all digits from 0 to 9" << endl;
} else {
cout << "Output: No" << endl;
}
return 0;
} |
import java.util.HashSet;
import java.util.Set;
public class Main {
// Function to check if a number is pandigital for a given base
static boolean isPandigitalSet(String n, int b) {
// Create a set containing digits from '0' to (char)('0' + b - 1)
Set<Character> digits = new HashSet<>();
for ( char i = '0' ; i < ( char )( '0' + b); i++) {
digits.add(i);
}
// Create a set of digits from the given number
Set<Character> numDigits = new HashSet<>();
for ( char c : n.toCharArray()) {
numDigits.add(c);
}
// Check if the two sets are equal
return digits.equals(numDigits);
}
public static void main(String[] args) {
// Test case 1
String n1 = "9651723480" ;
int b1 = 10 ;
System.out.println( "Input: n = " + n1 + ", b = " + b1);
if (isPandigitalSet(n1, b1)) {
System.out.println( "Output: Yes\nGiven number n has all digits from 0 to 9" );
} else {
System.out.println( "Output: No" );
}
// Test case 2
String n2 = "23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
int b2 = 36 ;
System.out.println( "\nInput: n = " + n2 + ", b = " + b2);
if (isPandigitalSet(n2, b2)) {
System.out.println( "Output: Yes\nGiven number n has all digits from 0 to 9" );
} else {
System.out.println( "Output: No" );
}
}
} |
def is_pandigital_set(n, b):
digits = set ( str (i) for i in range (b))
num_digits = set ( str (n))
return digits = = num_digits
# test case 1 n1 = "9651723480"
b1 = 10
print ( "Input:" , "n =" , n1, ", b =" , b1)
if is_pandigital_set(n1, b1):
print ( "Output: Yes\nGiven number n has all digits from 0 to 9" )
else :
print ( "Output: No" )
# test case 2 n2 = "23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
b2 = 36
print ( "\nInput:" , "n =" , n2, ", b =" , b2)
if is_pandigital_set(n2, b2):
print ( "Output: Yes\nGiven number n has all digits from 0 to 9" )
else :
print ( "Output: No" )
|
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{ // Function to check if a number is pandigital for a given base
static bool IsPandigitalSet( string n, int b)
{
// Create a set containing characters representing digits from 0 to (b-1)
HashSet< char > digits = new HashSet< char >();
for ( char i = '0' ; i < '0' + b; i++)
{
digits.Add(i);
}
// Create a set of digits from the given number
HashSet< char > numDigits = new HashSet< char >(n);
// Check if the two sets are equal
return digits.SetEquals(numDigits);
}
static void Main( string [] args)
{
// Test case 1
string n1 = "9651723480" ;
int b1 = 10;
Console.WriteLine($ "Input: n = {n1}, b = {b1}" );
if (IsPandigitalSet(n1, b1))
{
Console.WriteLine( "Output: Yes\nGiven number n has all digits from 0 to 9" );
}
else
{
Console.WriteLine( "Output: No" );
}
// Test case 2
string n2 = "23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
int b2 = 36;
Console.WriteLine($ "\nInput: n = {n2}, b = {b2}" );
if (IsPandigitalSet(n2, b2))
{
Console.WriteLine( "Output: Yes\nGiven number n has all digits from 0 to 9" );
}
else
{
Console.WriteLine( "Output: No" );
}
}
} |
// Function to check if a number is pandigital for a given base function isPandigitalSet(n, b) {
// Create a set containing characters representing digits from 0 to (b-1)
const digits = new Set();
for (let i = '0' ; i < String.fromCharCode( '0' .charCodeAt(0) + b); i++) {
digits.add(i);
}
// Create a set of digits from the given number
const numDigits = new Set(n);
// Check if the two sets are equal
return [...digits].every(digit => numDigits.has(digit));
} // Test case 1 const n1 = "9651723480" ;
const b1 = 10; console.log(`Input: n = ${n1}, b = ${b1}`); if (isPandigitalSet(n1, b1)) {
console.log( "Output: Yes\nGiven number n has all digits from 0 to 9" );
} else {
console.log( "Output: No" );
} // Test case 2 const n2 = "23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
const b2 = 36; console.log(`\nInput: n = ${n2}, b = ${b2}`); if (isPandigitalSet(n2, b2)) {
console.log( "Output: Yes\nGiven number n has all digits from 0 to 9" );
} else {
console.log( "Output: No" );
} |
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)