Problem 1317

Abstract / Summary

Given a positive integer n with no zero digits, find two positive integers without zero digits that add up to n.

Note

The full problem description, including helpful examples, can be found on LeetCode.

Python 1317.py

class Solution:
    def getNoZeroIntegers(self, n: int) -> List[int]:
        # stupid brute force
        a = int(n)
        while True:
            a -= 1
            b  = n - a
            if "0" not in str(a) and "0" not in str(b):
                return [ a, b ]
9 lines | 277 bytes | submitted November 3, 2023 | GitHub   12% comment ratio | tab size 4 | low complexity

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 59% (300k+ submissions).

Typical patterns to solve this problem:
  • Math
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.

See this dedicated page for more details.
LeetCode profile | GitHub repo

Code Walkthrough

Overview

The provided implementation defines a class Solution with a single method getNoZeroIntegers. The method receives an integer n and returns a two-element list [a, b] such that a + b = n and neither a nor b contains the digit 0 in its decimal representation. The algorithm proceeds by decrementing a candidate value a from n-1 downward until the zero-free condition is satisfied for both numbers.

Key Steps

The method begins by copying the input value into a mutable variable.

a = int(n)

A perpetual loop is then entered, guaranteeing that the search continues until a valid pair is found.

while True:

Inside the loop the candidate a is reduced by one on each iteration, and the complementary value b is computed as the difference between the original n and the current a.

a -= 1
b = n - a

The core validation checks whether the string form of each number contains the character '0'. If both checks succeed, the method returns the pair.

if "0" not in str(a) and "0" not in str(b):
    return [a, b]

Because the loop is infinite only until the condition is met, the function exits as soon as the first suitable pair is discovered.

Edge Cases Handled

When n is the smallest possible value that admits a solution (for example n = 2), the algorithm still works correctly. The first decrement yields a = 1 and b = 1; both numbers lack a zero digit, so the pair [1, 1] is returned immediately.

If n itself contains a zero digit, the algorithm does not rely on the representation of n for validation; it only inspects a and b. Consequently, inputs such as n = 101 are processed without special treatment, and the loop will eventually locate a pair like [11, 90]-but since 90 contains a zero, the loop continues until a zero-free complement is found, for instance [11, 90] is rejected and the search proceeds to [12, 89], which satisfies the condition.

The method also gracefully handles large values of n. Even when n has many digits, the conversion to string and the '0' not in check are performed only on the two candidate numbers per iteration, keeping memory usage modest.

Performance Optimizations Observed

Although the algorithm follows a straightforward brute-force pattern, it incorporates a few implicit efficiencies. The decrement of a by a single unit ensures that each possible pair is examined in a deterministic order, avoiding redundant calculations. By computing b as n - a rather than recomputing both numbers from scratch, the code eliminates an extra subtraction operation that would be required if both candidates were generated independently.

The condition if "0" not in str(a) and "0" not in str(b): leverages Python’s fast substring search on the string representation, which is generally faster than manually iterating over each digit. This early-exit check prevents unnecessary work after the first valid pair is found, terminating the loop immediately without scanning the remaining search space.

Finally, the use of a single while True loop with a direct return statement eliminates the need for additional flag variables or post-loop checks, streamlining control flow and reducing overhead.

written by openai gpt

ELI5 - Simplified Explanation

Imagine holding a big number like ten in your hands, and you want to split it into two smaller pieces. The rule is that neither of the pieces can have the number zero hiding inside them. To do this, the computer starts by picking a small piece and checking it. If that piece has a zero, the computer says no and tries a different, slightly smaller piece instead. Then it looks at the other piece that is left over to make sure it does not have a zero either. The computer keeps picking different pairs of pieces, one by one, just like trying on different shoes until it finds a pair that fits perfectly without any sneaky zeros anywhere. Once it finds two pieces that follow the rule, it sets them aside to show you that the puzzle is finished.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 27 | median = 4 | μ = 4.37 | σ = 1.47

Complexity Analysis

not analyzed yet

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
Obviously, I did that after I solved the problem myself.

In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.

And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.

(click on a column header to sort the table)
author
author solutions
www AlgoMonster C++ Java Python TS
WalkCCC C++ Java Python
leetcode.ca C++ Java Go Python Rust TS
Progiez C++ Java Python
github BigEggStudy C#
black-shadows C++
charles-wangkai Java
DimitrisJim C
fartem Ruby
fsq C++
halfrost Go
JoshCrozier JS
keineahnung2345 C++
lkwq007 Python
muhammadfarhankt Go
suxrobGM C#
varunu28 Java
AlgoMonster C++, Python, Java, TypeScript
WalkCCC C++, Python, Java
leetcode.ca C++, Go, Java, Python, Rust, TypeScript
Progiez C++, Java, Python
BigEggStudy C#
black-shadows C++, C++
charles-wangkai Java
DimitrisJim C
fartem Ruby
fsq C++
halfrost Go
JoshCrozier JavaScript
keineahnung2345 C++
lkwq007 Python
muhammadfarhankt Go
suxrobGM C#
varunu28 Java
29 links | 17 authors | 10 languages

Videos

8 videos   did my crawler come up with invalid videos ? drop me a line.

Solved On The Same Day

13 solutions   November 3, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • Tech Stack - my development environment and tools
  • AI - how AI helped me create this website (NOT solving the problems)
  • Progress - progression over time
  • Quality - code quality