Count Pandigital Fractions pairs in given Array

Given an array arr[], the task is to count the pairs in the array such that arr[i]/arr[j] is a Pandigital Fraction.

A fraction N/D is called a Pandigital Fraction if the fraction  \frac{N}{D} contains all the digits from 0 to 9.  
 

Examples:

Input: arr = [ 12345, 67890, 123, 4567890 ] 
Output:
Explanation:
The fractions are 12345/67890, 12345/4567890, and 123/4567890 

Input: arr = [ 12345, 6789 ] 
Output:



 

Approach: The idea is to iterate over every possible pair of the array using two nested loops and for every pair concatenate arr[i] and arr[j] into a single number and check if the concatenation of arr[i] and arr[j] is a Pandigital number in base 10 then increment count.

Below is the implementation of the above approach:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the 
# above approach
  
import math 
  
# Function to concatenate 
# two numbers into one
def numConcat(num1, num2): 
    
     # Find number of digits in num2 
     digits = len(str(num2)) 
    
     # Add zeroes to the end of num1 
     num1 = num1 * (10**digits) 
    
     # Add num2 to num1 
     num1 += num2 
    
     return num1 
       
# Return true if n is pandigit
# else return false.  
def checkPanDigital(n):
    n = str(n)
    b = 10
      
    # 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
  
# Returns true if N is a 
# Pandigital Fraction Number
def isPandigitalFraction(N, D):
    join = numConcat(N, D)
    return checkPanDigital(join)
  
# Returns number pandigital fractions
# in the array
def countPandigitalFraction(v, n) : 
    
    # iterate over all  
    # pair of strings 
    count = 0
    for i in range(0, n) : 
    
        for j in range (i + 1,  
                        n) : 
            
            if (isPandigitalFraction(v[i], 
                             v[j])) : 
                count = count + 1
    return count 
    
  
# Driver Code 
if __name__ == "__main__"
        
    arr = [ 12345, 67890, 123, 4567890
    n = len(arr) 
    
    print(countPandigitalFraction(arr, n))

chevron_right


Output:

3

Time Complexity: O(N2) 
Reference: https://mathworld.wolfram.com/PandigitalFraction.html
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.