Check if all sub-numbers have distinct Digit product in Python

When working with numbers, we sometimes need to check if all sub-numbers have unique digit products. A sub-number is any contiguous sequence of digits from the original number. For example, the sub-numbers of 135 are 1, 3, 5, 13, 35, 135. The digit product of a number is the product of all its individual digits.

For a number with n digits, there are n*(n+1)/2 possible sub-numbers. We need to calculate the digit product for each sub-number and verify that all products are distinct.

Example Problem

If the input is n = 235, the sub-numbers are [2, 3, 5, 23, 35, 235] with digit products [2, 3, 5, 6, 15, 30]. Since all products are unique, the output is True.

Algorithm Steps

We'll solve this by following these steps:

  • Convert the number to a string to easily access individual digits
  • Generate all possible sub-numbers
  • Calculate the digit product for each sub-number
  • Use a set to track unique products and detect duplicates
  • Return False if any duplicate is found, otherwise True

Implementation

Helper Function for Digit Product

def dig_prod(digits):
    """Calculate the product of all digits in the list."""
    product = 1
    for d in digits:
        product *= d
    return product

Main Solution

def dig_prod(digits):
    """Calculate the product of all digits in the list."""
    product = 1
    for d in digits:
        product *= d
    return product

def solve(num):
    """Check if all sub-numbers have distinct digit products."""
    num_str = str(num)
    length = len(num_str)
    digits = []
    
    # Convert string digits to integers
    for i in range(length):
        digits.append(int(num_str[i]))
    
    prod_set = set()
    
    # Generate all sub-numbers and calculate their digit products
    for i in range(length):
        for j in range(i, length):
            # Get sub-array from index i to j (inclusive)
            sub_digits = digits[i:j+1]
            item = dig_prod(sub_digits)
            
            # Check if this product already exists
            if item in prod_set:
                return False
            else:
                prod_set.add(item)
    
    return True

# Test the function
n = 235
result = solve(n)
print(f"Number: {n}")
print(f"All sub-numbers have distinct digit products: {result}")
Number: 235
All sub-numbers have distinct digit products: True

Testing with Another Example

def dig_prod(digits):
    product = 1
    for d in digits:
        product *= d
    return product

def solve(num):
    num_str = str(num)
    length = len(num_str)
    digits = []
    
    for i in range(length):
        digits.append(int(num_str[i]))
    
    prod_set = set()
    sub_numbers = []
    products = []
    
    for i in range(length):
        for j in range(i, length):
            sub_digits = digits[i:j+1]
            sub_number = int(''.join(map(str, sub_digits)))
            item = dig_prod(sub_digits)
            
            sub_numbers.append(sub_number)
            products.append(item)
            
            if item in prod_set:
                return False, sub_numbers, products
            else:
                prod_set.add(item)
    
    return True, sub_numbers, products

# Test with number 122
n = 122
result, subs, prods = solve(n)
print(f"Number: {n}")
print(f"Sub-numbers: {subs}")
print(f"Digit products: {prods}")
print(f"All distinct: {result}")
Number: 122
Sub-numbers: [1, 12, 122, 2, 22, 2]
Digit products: [1, 2, 4, 2, 4, 2]
All distinct: False

How It Works

The algorithm uses a nested loop to generate all possible sub-numbers. For each starting position i, it considers all ending positions j from i to the end of the number. This ensures we examine every contiguous subsequence of digits.

The digit product function multiplies all digits in a given sequence. We use a set to efficiently track which products we've already seen, allowing for O(1) duplicate detection.

Time Complexity

The time complexity is O(n³) where n is the number of digits, since we have O(n²) sub-numbers and each digit product calculation takes O(n) time in the worst case.

Conclusion

This solution efficiently checks if all sub-numbers of a given number have unique digit products by generating all possible sub-numbers and using a set to detect duplicate products. The approach works well for numbers with a reasonable number of digits.

---
Updated on: 2026-03-25T14:15:07+05:30

299 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements