Convert Integer to the Sum of Two No-Zero Integers
LeetCode 1317. Convert Integer to the Sum of Two No-Zero Integers gives you a positive integer n and asks you to return any pair of positive integers [a, b] such that a + b = n and neither a nor b contains the digit 0 in its decimal representation. A "no-zero integer" is simply a positive integer with no zero digits.
The problem guarantees a solution exists for every valid input.
Why this problem matters
This is a warm-up problem that tests two things: your ability to enumerate candidates and your ability to check a digit-level property of a number. Both of these are building blocks you will see in harder problems. If you can split a number into parts and inspect individual digits without hesitation, problems like Happy Number and Palindrome Number become much easier.
The key insight
You do not need anything clever here. Start with a = 1 and b = n - 1, then increment a (and decrement b) until you find a pair where neither number contains the digit 0. Since the problem guarantees a valid pair exists, the scan will always terminate.
The only helper you need is a function that checks whether a number contains the digit 0. You can do this by repeatedly extracting the last digit with % 10 and checking if it equals zero.
The solution
def get_no_zero_integers(n):
def has_zero(x):
while x > 0:
if x % 10 == 0:
return True
x //= 10
return False
for a in range(1, n):
b = n - a
if not has_zero(a) and not has_zero(b):
return [a, b]
The outer loop tries every candidate a from 1 up to n - 1. For each candidate, it computes b = n - a and checks both numbers for zero digits. The first valid pair gets returned immediately.
You could also check for zeros using string conversion: "0" not in str(a). Both approaches work. The modular arithmetic version is worth practicing because it shows up in many other digit-manipulation problems.
Visual walkthrough
getNoZeroIntegers(n) on two examples:Step 1: n = 11, try a = 1
1 (ok)1, 0 (has 0)a = 1 is fine, but b = 10 contains the digit 0. Skip this pair.
Step 2: n = 11, try a = 2
2 (ok)9 (ok)Neither 2 nor 9 contains the digit 0. We found our answer for n = 11.
Step 3: New example, n = 1010, try a = 1
1 (ok)1, 0, 0, 9 (has 0)b = 1009 contains zeros. Keep scanning.
Step 4: n = 1010, try a = 9
9 (ok)1, 0, 0, 1 (has 0)b = 1001 still contains zeros. Continue scanning a = 10, 11, ...
Step 5: n = 1010, try a = 11
1, 1 (ok)9, 9, 9 (ok)Neither 11 nor 999 contains the digit 0. Return [11, 999].
For any n, at least one split exists where neither number contains the digit 0.
For n = 11, the answer comes on the second try. For n = 1010, it takes a few more iterations, but the scan still finishes quickly. In practice, valid pairs appear early because most numbers do not contain zeros.
Complexity analysis
| Approach | Time | Space |
|---|---|---|
| Linear scan | O(n * d) | O(1) |
Here d is the number of digits in n (proportional to log base 10 of n). In the worst case, you scan through all values of a from 1 to n - 1, and each candidate requires O(d) time to check its digits. In practice the scan terminates much sooner, but O(n * d) is the theoretical upper bound.
The space complexity is O(1) because you only use a few integer variables regardless of input size.
The building blocks
1. Digit inspection with modular arithmetic
Checking individual digits of a number by repeatedly dividing by 10 and inspecting the remainder. This is the same pattern you use in Happy Number (summing squared digits), Palindrome Number (comparing digits), and Add Digits (reducing to a single digit).
def has_zero(x):
while x > 0:
if x % 10 == 0:
return True
x //= 10
return False
2. Brute-force pair enumeration
When a problem asks you to find two numbers that sum to a target and satisfy some condition, the simplest approach is to try every possible split. This pattern appears in Two Sum (with a hash map optimization) and in partition problems. Here, the condition is unusual enough (no zero digits) that a simple linear scan is the cleanest solution.
for a in range(1, n):
b = n - a
if valid(a) and valid(b):
return [a, b]
Edge cases
- n = 2: The only valid split is [1, 1]. Both are single-digit numbers with no zeros.
- n = 11: As shown above, [2, 9] is the first valid pair since [1, 10] fails (10 contains a 0).
- Large n with many zeros: For example, n = 10000. The scan skips candidates where
bcontains zeros (like 9999 being paired with 1, which works immediately since neither has a zero). Valid pairs tend to appear early. - n = 1111: Both a = 1 and b = 1110 fail because 1110 has zeros. But a = 2 and b = 1109 also fail (1109 has a 0). The scan continues until it finds a pair like [111, 1000]... which also fails. Eventually [112, 999] works since neither contains a zero.
From understanding to recall
This problem is simple enough that you might think you do not need to practice it. But the building blocks inside it, digit inspection and candidate enumeration, appear in dozens of harder problems. If you can extract digits with divmod without thinking about it, you free up mental energy for the harder parts of those problems.
That is what spaced repetition solves. CodeBricks breaks this problem into its building blocks and drills them at increasing intervals. You type each piece from scratch until the patterns become automatic. When a harder problem like Happy Number or Palindrome Number asks you to inspect digits, you will already have the muscle memory.
Related posts
- Happy Number - Digit manipulation combined with cycle detection
- Palindrome Number - Checking digit properties without string conversion
- Add Digits - Another digit-based math problem using repeated division
Practice these related problems and you will notice the same digit extraction pattern in each one.