Saturday, January 11, 2025
Google search engine
HomeData Modelling & AIPython Program to print all distinct uncommon digits present in two given...

Python Program to print all distinct uncommon digits present in two given numbers

Given two positive integers A and B, the task is to print the distinct digits in descending order, which are not common in the two numbers.

Examples:

Input: A = 378212, B = 78124590
Output: 9 5 4 3 0
Explanation: All distinct digits present in the two numbers are {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. The digits {1, 2, 6, 7} are common in both numbers.

Input: A = 100, B = 273
Output: 7 3 2 1 0
Explanation: All distinct digits present in the two numbers are {0, 1, 2, 3, 7}. The digits {0, 1, 2, 3, 7} are common in both numbers.

Approach: The problem can be solved using sets, and lists in python. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

Python3




# Python implementation
# of the above approach
 
# Function to print uncommon digits
# of two numbers in descending order
def disticntUncommonDigits(A, B):
 
    # Stores digits of A as string
    A = str(A)
 
    # Stores digits of B as string
    B = str(B)
 
    # Stores the characters
    # of A in a integer list
    lis1 = list(map(int, A))
     
    # Stores the characters
    # of B in a integer list
    lis2 = list(map(int, B))
 
    # Converts lis1 to set
    lis1 = set(lis1)
 
    # Converts lis2 to set
    lis2 = set(lis2)
 
    # Stores the uncommon digits present
    # in the sets lis1 and lis2
    lis = lis1.symmetric_difference(lis2)
 
    # Converts lis to list
    lis = list(lis)
 
    # Sort the list in descending order
    lis.sort(reverse = True)
 
    # Print the list lis
    for i in lis:
        print(i, end =" ")
 
# Driver Code
 
 
# Input
A = 378212
B = 78124590
 
disticntUncommonDigits(A, B)


Output:

9 5 4 3 0

Time Complexity: O(log10(A) + log10(B))
Auxiliary Space: O(log10(A) + log10(B))

Method #2:Using Operator.countOf() method

Python3




# Python implementation
# of the above approach
import operator as op
# Function to print uncommon digits
# of two numbers in descending order
 
 
def disticntUncommonDigits(A, B):
 
    # Stores digits of A as string
    A = str(A)
 
    # Stores digits of B as string
    B = str(B)
 
    # Stores the characters
    # of A in a integer list
    lis1 = list(map(int, A))
 
    # Stores the characters
    # of B in a integer list
    lis2 = list(map(int, B))
    res = []
    for i in lis1:
        if op.countOf(lis2, i) == 0:
            res.append(i)
    for i in lis2:
        if op.countOf(lis1, i) == 0:
            res.append(i)
    # Sort the list in descending order
    res.sort(reverse=True)
 
    # Print the list lis
    for i in res:
        print(i, end=" ")
 
# Driver Code
 
 
# Input
A = 378212
B = 78124590
 
disticntUncommonDigits(A, B)


Output

9 5 4 3 0 

Time Complexity: O(log10(A) + log10(B))
Auxiliary Space: O(log10(A) + log10(B))

Approach#3: Using set() and difference() and sorted and union

Convert both numbers A and B to sets of digits. Find uncommon digits in both sets by taking the set difference and union of the sets. Convert the resulting set of uncommon digits to a list and sort it in descending order. Print the list of uncommon digits as a string.

Algorithm

1. Initialize A and B with given values.
2. Convert A and B to sets of digits.
3. Find uncommon digits using set difference and union.
4. Convert resulting set to a list and sort it in descending order.
5. Print the list of uncommon digits as a string.

Python3




A = 378212
B = 78124590
 
# find unique digits in each number
digits_A = set(str(A))
digits_B = set(str(B))
 
# find uncommon digits using set difference
uncommon_digits = digits_A.difference(digits_B).union(digits_B.difference(digits_A))
 
# convert set to list and sort in descending order
uncommon_digits = sorted(list(uncommon_digits), reverse=True)
 
# print the list of uncommon digits
print(' '.join(uncommon_digits))


Output

9 5 4 3 0

Time Complexity: O(m+n), where m and n are the number of digits in A and B, respectively. This is because the set() function and set difference and union operations take O(m) and O(n) time, respectively.

Space Complexity: O(m+n), where m and n are the number of digits in A and B, respectively. This is because we are storing the digits of A and B in sets, which take up O(m) and O(n) space, respectively. The resulting set of uncommon digits and the list of uncommon digits also take up O(m+n) space.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments