Skip to content

1317. Convert Integer to the Sum of Two No-Zero Integers

Difficulty Topics

Description

No-Zero integer is a positive integer that does not contain any 0 in its decimal representation.

Given an integer n, return a list of two integers [A, B] where:

  • A and B are No-Zero integers.
  • A + B = n

The test cases are generated so that there is at least one valid solution. If there are many valid solutions you can return any of them.

 

Example 1:

Input: n = 2
Output: [1,1]
Explanation: A = 1, B = 1. A + B = n and both A and B do not contain any 0 in their decimal representation.

Example 2:

Input: n = 11
Output: [2,9]

 

Constraints:

  • 2 <= n <= 104

Solution

convert-integer-to-the-sum-of-two-no-zero-integers.py
class Solution:
    def getNoZeroIntegers(self, n: int) -> List[int]:

        def findZeroes(n):
            return str(n).count("0") > 0

        a, b = 1, n - 1

        while findZeroes(a) or findZeroes(b):
            if findZeroes(a):
                a += 1
                b -= 1
            if findZeroes(b):
                a += 1
                b -= 1

        return [a,b]