Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

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

Code Implementation

class Solution:
    def getNoZeroIntegers(self, n: int) -> [int]:
        def has_zero(num):
            return '0' in str(num)
        for a in range(1, n):
            b = n - a
            if not has_zero(a) and not has_zero(b):
                return [a, b]
      
class Solution {
public:
    vector<int> getNoZeroIntegers(int n) {
        auto hasZero = [](int num) {
            while (num) {
                if (num % 10 == 0) return true;
                num /= 10;
            }
            return false;
        };
        for (int a = 1; a < n; ++a) {
            int b = n - a;
            if (!hasZero(a) && !hasZero(b))
                return {a, b};
        }
        return {};
    }
};
      
class Solution {
    public int[] getNoZeroIntegers(int n) {
        for (int a = 1; a < n; a++) {
            int b = n - a;
            if (!hasZero(a) && !hasZero(b)) {
                return new int[]{a, b};
            }
        }
        return new int[0];
    }
    private boolean hasZero(int num) {
        while (num > 0) {
            if (num % 10 == 0) return true;
            num /= 10;
        }
        return false;
    }
}
      
var getNoZeroIntegers = function(n) {
    function hasZero(num) {
        return num.toString().includes('0');
    }
    for (let a = 1; a < n; a++) {
        let b = n - a;
        if (!hasZero(a) && !hasZero(b)) {
            return [a, b];
        }
    }
};
      

Problem Description

Given an integer n, your task is to find two positive integers a and b such that:

  • a + b = n
  • Neither a nor b contains the digit zero in its decimal representation (i.e., both are "no-zero integers")
  • Return any valid pair [a, b] that satisfies these conditions

You can assume that at least one valid solution exists for every input n >= 2. The returned pair should be in any order, and you do not need to consider uniqueness or all possible solutions—just one valid answer.

Thought Process

The problem asks us to split a given number into two positive parts, ensuring neither part contains the digit zero. The most direct way is to try every possible split (a from 1 up to n-1), check if both numbers have no zeros, and return the first valid pair.

At first glance, this seems like a brute-force problem—simply check all possibilities. However, since numbers containing zero are relatively rare compared to all numbers in the range, we can expect to find a solution quickly for any reasonable input. There's no need for a more complex or optimized algorithm.

If you imagine the numbers as tickets, and only some tickets are "bad" (contain zero), you just keep drawing tickets until you get two "good" ones that add up to the target.

Solution Approach

  • Step 1: Iterate a from 1 to n-1. For each a, calculate b = n - a.
  • Step 2: For both a and b, check if their decimal representation contains the digit zero. This can be done by converting the number to a string and checking for '0', or by repeatedly dividing by 10 and checking each digit.
  • Step 3: As soon as you find a pair where both numbers are "no-zero integers," return them.

This approach is simple and effective because:

  • There are always many valid pairs for any reasonable n.
  • Checking for the digit zero is fast and easy.
  • We stop as soon as we find the first valid pair, so the loop doesn't run for all n in practice.

Example Walkthrough

Let's say n = 11.

  1. Try a = 1, b = 10: 10 contains a zero, so skip.
  2. Try a = 2, b = 9: Both 2 and 9 have no zeros. This is a valid pair!

The algorithm returns [2, 9]. If a = 3, b = 8 is also valid, but since we return the first pair found, [2, 9] is our answer.

For a larger n, say n = 101:

  • Try a = 1, b = 100 (100 contains zero)
  • Try a = 2, b = 99 (both are valid!)

Again, [2, 99] is returned.

Time and Space Complexity

  • Brute-force approach:
    • We may need to check up to n-1 pairs in the worst case.
    • For each pair, checking for zero digits is O(\log n) (number of digits in n).
    • Total worst-case time: O(n \log n).
    • However, in practice: We almost always find a valid pair within the first few tries, so the average time is much less.
    • Space complexity: O(1) (constant), since we only use a few variables.
  • Optimized approach: There is no significant optimization beyond the above, as the brute-force is efficient for the problem's constraints.

Summary

To solve the "Convert Integer to the Sum of Two No-Zero Integers" problem, we simply try each possible split of n into two positive integers and check if both numbers avoid the digit zero. As soon as we find a valid pair, we return it. This direct approach is both simple and efficient, leveraging the fact that valid pairs are common. The elegance of the solution lies in its straightforwardness and the use of basic number and string operations.