Given a number N, the task is to check if the all sub-numbers of this number have distinct digit product.
- An N digit number has N*(N+1)/2 sub-numbers. For example, all possible sub-numbers of 975 are 9, 7, 5, 97, 75, 975.
- Digit product of a number is product of its digits.
Input : N = 324 Output : YES Sub-numbers of 324 are 3, 2, 4, 32, 24 and 324 and digit products are 3, 2, 4, 6, 8 and 24 respectively. All the digit products are different. Input : N = 323 Output : NO Sub-numbers of 323 are 3, 2, 3, 32, 23 and 323 and digit products are 3, 2, 3, 6, 6 and 18 respectively. Digit products 3 and 6 have occurred twice.
- Make a digit array i.e., an array with its elements as digits of given number N.
- Now finding sub-numbers of N is similar to finding all possible subarrays of the digit array.
- Maintain a list of digit products of these subarrays.
- If any digit product has appeared more than once, print NO.
- Else print YES.
Below is the implementation of the above approach :
# Python3 program to check if all
# sub-numbers have distinct Digit product
# Function to calculate product of
# digits between given indexes
def digitProduct(digits, start, end):
pro = 1
for i in range(start, end + 1):
pro *= digits[i]
# Function to check if all sub-numbers
# have distinct Digit product
s = str(N)
# Length of number N
length = len(s)
# Digit array
digits = [None] * length
# set to maintain digit products
products = set()
for i in range(0, length):
digits[i] = int(s[i])
# Finding all possible subarrays
for i in range(0, length):
for j in range(i, length):
val = digitProduct(digits, i, j)
if val in products:
# Driver Code
if __name__ == “__main__”:
N = 324
if isDistinct(N) == True:
# This code is contributed
# by Rituraj Jain
- Check whether a number can be expressed as a product of single digit numbers
- Integers from the range that are composed of a single distinct digit
- Distinct Prime Factors of Array Product
- Product of non-repeating (distinct) elements in an Array
- Find two distinct prime numbers with given product
- Check if frequency of each digit is less than the digit
- Product of N with its largest odd digit
- Digit - Product - Sequence
- First digit in product of an array of numbers
- Largest palindrome which is product of two n-digit numbers
- Maximum number with same digit factorial product
- Maximum of sum and product of digits until number is reduced to a single digit
- Check if all array elements are distinct
- Check whether a number has exactly three distinct factors or not
- Check if the sum of distinct digits of two integers are equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.