Domain 1EasyCORE

Convert Integer to the Sum of Two No-Zero Integers

The problem requires us to split a given integer `n` into two positive integers `A` and `B` such that both integers do not contain the digit '0' in their decimal representation. The sum of these two integers should equal `n`. It is guaranteed that there is at least one valid solution.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#Math

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

Model: qwen2.5-coder:14b
Topics: Math
FAANG asked: 0
Acceptance rate: 56.9%


Problem Breakdown

The problem requires us to split a given integer n into two positive integers A and B such that both integers do not contain the digit ‘0’ in their decimal representation. The sum of these two integers should equal n. It is guaranteed that there is at least one valid solution.

Inputs:

  • An integer n (2 <= n <= 10^4)

Outputs:

  • A list of two integers [A, B] where neither A nor B contains the digit ‘0’.

Implicit Constraints or Tricky Edge Cases:

  • The input n is always within the range [2, 10^4].
  • There is at least one valid solution for any given n.
  • The digits in A and B should be non-zero.

Intuition

The brute-force approach would involve iterating through all possible pairs of integers (A, B) such that A + B = n and checking if both A and B are No-Zero integers. This approach is inefficient because it has a time complexity of O(n^2) due to the nested iteration.

A better approach involves leveraging the fact that we can start with a simple pair (1, n-1) and incrementally adjust these values until both numbers are No-Zero integers. The key insight here is that starting from (1, n-1) ensures that we are always checking the smallest possible non-zero values first.

Approach

  1. Initialize A to 1 and B to n - 1.
  2. Check if either A or B contains the digit ‘0’.
  3. If either A or B contains ‘0’, increment A by 1 and decrement B by 1.
  4. Repeat steps 2-3 until both A and B are No-Zero integers.
  5. Return the pair [A, B].

This approach ensures that we find a valid solution efficiently by starting with the smallest possible values and incrementing/decrementing them as needed.

Complexity Analysis

Time Complexity: O(n) in the worst case, where each increment of A and decrement of B results in a check for ‘0’ digits. However, this is generally much faster due to the nature of the problem constraints. Space Complexity: O(1), as we are only using a constant amount of extra space.

Solution

def getNoZeroIntegers(n):
    A = 1
    B = n - 1
    
    while '0' in str(A) or '0' in str(B):
        A += 1
        B -= 1
    
    return [A, B]

Edge Cases Handled

  • Edge Case: n = 2 (smallest possible input)
    • The code correctly returns [1, 1].
  • Edge Case: n = 10000
    • The code correctly returns [1, 9999].
  • Edge Case: n = 69
    • The code correctly returns [1, 68].
  • Edge Case: n = 1010
    • The code correctly returns [11, 999].

The code handles all edge cases by ensuring that both A and B are No-Zero integers before returning them.