Given an integer **N**^{ }and two integers **D1** and **D2 **(* < 10*), the task is to find the number of co-prime pairs less than or equal to **N** consisting of only digits** D1** and **D2**.

**Examples:**

Input:N = 30, D1 = 2, D2 = 3Output:5Explanation:

All possible pairs of numbers upto 30 having digits 2 and 3 which are co-prime to each other are (2, 3), (2, 23), (3, 22), (3, 23), (22, 23) .

Input:N = 100, D1 = 5, D2 = 6Output:8Explanation:

All possible pairs of numbers upto 100 having digits 5 and 6 which are co-prime to each other are (5, 6), (5, 56), (5, 66), (6, 55), (6, 65), (55, 56), (56, 65), (65, 66).

**Approach: **The idea to solve this problem is based on the following observations:

Observation:

- Find and append every number consisting
only given two digitswhich are less than or equal toNinto a list.- Now it’s easy to find the number of unordered pairs which are co-primes.
- Note that there can be maximum
1 + 2 + 2numbers in the list i.e. Overall Time Complexity cannot exceed^{2}+ 2^{3}+ 2^{4}+ ………2^{10}= 2047O(2047 * 2047),as the maximum no. of digits possible is 9.

Follow the steps below to solve the problem:

- Initialize an empty list, say
**l**, - Sort the list.
- Initialize two lists, say
**total**and**temp2**for further use. - Iterate until
**l[0] < 10:**- Append the given two digits as a string to all the elements present in the list
**l.** - Keep updating
**l**in the way shown below:- [2, 3] ->
**[‘2’ + ‘2’, ‘2’ + ‘3’, ‘3’ + ‘2’, ‘3’ + ‘3’]** - [22, 23, 32, 33] – >
**[’22’ + ‘2’, ’22’ + ‘3’, ’23’ + ‘2’, ’23’ + ‘3’, ’32’ + ‘2’, ’32’ + ‘3’, ’33’ + ‘2’, ’33’ + ‘3’]**and so on.

- [2, 3] ->

- Append the given two digits as a string to all the elements present in the list
- Define a function
**numOfPairs()**which returns the count of unordered co-prime pairs. - Print the count returned by the above function as the answer.

Below is the implementation of the above approach:

## Python3

`# Python3 program for the above approach ` ` ` `from` `copy ` `import` `deepcopy ` `import` `math ` ` ` `# Function to check whether given ` `# integers are co-prime or not ` `def` `coprime(a, b): ` ` ` `return` `(math.gcd(a, b)) ` `=` `=` `1` ` ` `# Utility function to count ` `# number of co-prime pairs ` `def` `numOfPairs(arr, N): ` ` ` `count ` `=` `0` ` ` ` ` `# Traverse the array ` ` ` `for` `i ` `in` `range` `(` `0` `, N` `-` `1` `): ` ` ` `for` `j ` `in` `range` `(i` `+` `1` `, N): ` ` ` ` ` `# If co-prime ` ` ` `if` `(coprime(` `int` `(arr[i]), ` `int` `(arr[j]))): ` ` ` ` ` `# Increment count ` ` ` `count ` `=` `count ` `+` `1` ` ` ` ` `# Return count ` ` ` `return` `count ` ` ` `# Function to count number ` `# of co-prime pairs ` `def` `noOfCoPrimePairs(N, d1, d2): ` ` ` ` ` `# Stores digits in string form ` ` ` `l ` `=` `[] ` ` ` `l.append(` `str` `(d1)) ` ` ` `l.append(` `str` `(d2)) ` ` ` ` ` `# Sort the list ` ` ` `l.sort() ` ` ` ` ` `if` `int` `(N) < ` `int` `(l[` `1` `]): ` ` ` `return` `0` ` ` ` ` `# Keep two copies of list l ` ` ` `total ` `=` `temp2 ` `=` `deepcopy(l) ` ` ` `flag ` `=` `0` ` ` `temp3 ` `=` `[] ` ` ` ` ` `# Generate 2 digit numbers ` ` ` `# using d1 and d2 ` ` ` `while` `len` `(l[` `0` `]) < ` `10` `: ` ` ` `for` `i ` `in` `range` `(` `len` `(l)): ` ` ` `for` `j ` `in` `range` `(` `2` `): ` ` ` ` ` `# If current number ` ` ` `# does not exceed N ` ` ` `if` `int` `(l[i]` `+` `temp2[j]) > ` `int` `(N): ` ` ` `flag ` `=` `1` ` ` `break` ` ` ` ` `total.append(l[i]` `+` `temp2[j]) ` ` ` `temp3.append(l[i]` `+` `temp2[j]) ` ` ` ` ` `if` `flag ` `=` `=` `1` `: ` ` ` `break` ` ` `if` `flag ` `=` `=` `1` `: ` ` ` `break` ` ` `l ` `=` `deepcopy(temp3) ` ` ` `temp3 ` `=` `[] ` ` ` ` ` `# Stores length of list ` ` ` `lenOfTotal ` `=` `len` `(total) ` ` ` ` ` `# Stores number of co-prime pairs ` ` ` `ans ` `=` `numOfPairs(total, lenOfTotal) ` ` ` ` ` `# Print number of co-prime pairs ` ` ` `print` `(ans) ` ` ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# Given value of N, d1, d2 ` ` ` `N ` `=` `30` ` ` `d1 ` `=` `2` ` ` `d2 ` `=` `3` ` ` ` ` `# Function call to count ` ` ` `# number of co-prime pairs ` ` ` `noOfCoPrimePairs(N, d1, d2) ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity:** O(2^{logN})**Auxiliary Space: **O(2^{logN})

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.