We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Convert Integer to the Sum of Two No-Zero Integers

Number: 1440

Difficulty: Easy

Paid? No

Companies: Hudson River Trading


Problem Description

Given a positive integer n, find two positive integers a and b such that both a and b do not contain the digit '0' in their decimal representation and a + b equals n. It is guaranteed that there is at least one such solution.


Key Insights

  • A no-zero integer is defined as a positive integer that does not contain the digit '0'.
  • We can iterate over potential values of a and calculate b = n - a.
  • The main check for each candidate is ensuring that neither a nor b contains the digit '0'.
  • Since n is up to 10⁴, iterating from 1 to n is efficient enough.

Space and Time Complexity

Time Complexity: O(n * log10(n)) in the worst case, because each number is converted to a string for checking zeros (the number of digits is proportional to log10(n)).
Space Complexity: O(1) apart from the space used for output.


Solution

We use an iterative approach by trying each possible value of a starting from 1 up to n-1. For each value of a, we compute b = n - a and then check if both a and b are no-zero integers by converting them to strings and checking for the presence of the character '0'. We stop as soon as we find such a pair and return it.
Key data structures: None required besides simple variables.
Algorithmic approach: Simple iteration and string checking.
Gotchas: Ensure that both parts of the sum do not include the digit '0', and correctly convert numbers to strings for the check.


Code Solutions

def getNoZeroIntegers(n):
    # Iterate over possible values for a from 1 to n-1.
    for a in range(1, n):
        b = n - a  # Calculate b
        # Check if both a and b have no '0' in their string representation.
        if '0' not in str(a) and '0' not in str(b):
            return [a, b]

# Example usage:
print(getNoZeroIntegers(11))  # Might output: [2, 9]
← Back to All Questions