Convert Integer to the Sum of Two No-Zero Integers — LeetCode 1317 Python Solution

EasyMath
Problem
#1317
Reading time
2 min

The problem

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.

Example

Input
n = 2
Output
[1,1]
Explanation
Let a = 1 and b = 1.

Python solution

Python
class Solution:
    def getNoZeroIntegers(self, n: int) -> List[int]:
        for a in count(1):
            b = n - a
            if "0" not in f"{a}{b}":
                return [a, b]

Complexity

MeasureComplexity
TimeO(n \times \log n), where n is the integer given in the problem
SpaceO(\log n) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1317. Convert Integer to the Sum of Two No-Zero Integers is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1317. Convert Integer to the Sum of Two No-Zero Integers?
LeetCode 1317. Convert Integer to the Sum of Two No-Zero Integers is rated Easy on LeetCode.
What is the time complexity of LeetCode 1317. Convert Integer to the Sum of Two No-Zero Integers?
The Python solution on this page runs in O(n \times \log n), where n is the integer given in the problem.
What is the space complexity of LeetCode 1317. Convert Integer to the Sum of Two No-Zero Integers?
The Python solution on this page uses O(\log n) auxiliary space.
What topics does LeetCode 1317. Convert Integer to the Sum of Two No-Zero Integers cover?
LeetCode 1317. Convert Integer to the Sum of Two No-Zero Integers is tagged Math on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview