Convert Integer to the Sum of Two No-Zero Integers

You want to find two integers a and b such that they are “No-Zero integers” (they don’t contain any 0 in their decimal representation), and their sum equals the given integer n. You must return these two integers as a list [a, b].

Solution Approach

  1. Loop Through Possible Values of a: Start with a as 1, and calculate the corresponding value of b as n - a.
  2. Check No-Zero Condition: If both a and b do not contain the digit 0, you have found a valid solution.
  3. Return the Solution: Return the list [a, b] if a valid solution is found.

Now, let’s translate this approach into code:

1
2
3
4
5
6
7
class Solution:
    def getNoZeroIntegers(self, n: int) -> List[int]:
        for a in range(1, n):
            b = n - a
            # Check if both 'a' and 'b' do not contain the digit '0'
            if '0' not in str(a) and '0' not in str(b):
                return [a, b]

Key Takeaways

  • The code iterates through possible values of a and calculates corresponding b.
  • It checks the No-Zero condition by converting a and b to strings and ensures that they do not contain the character ‘0’.
  • It returns the first valid pair [a, b] found, meeting the problem’s constraints.

This approach ensures that a valid solution is found as per the constraints given, and it returns that solution.