Problem

No-Zero integer is a positive integer that does not contain any0 in its decimal representation.

Given an integer n, return a list of two integers [a, b] where :

  • a and b are No-Zero integers.
  • a + b = n

The test cases are generated so that there is at least one valid solution. If there are many valid solutions, you can return any of them.

Examples

Example 1

1
2
3
4
Input: n = 2
Output: [1,1]
Explanation: Let a = 1 and b = 1.
Both a and b are no-zero integers, and a + b = 2 = n.

Example 2

1
2
3
4
5
Input: n = 11
Output: [2,9]
Explanation: Let a = 2 and b = 9.
Both a and b are no-zero integers, and a + b = 11 = n.
Note that there are other valid answers as [8, 3] that can be accepted.

Constraints

  • 2 <= n <= 10^4

Solution

Method 1 – Brute Force Search 1

Intuition

A no-zero integer is one that does not contain the digit 0. We can try all possible pairs (a, b) such that a + b = n and check if both a and b are no-zero integers. Since n is small, this brute-force approach is efficient enough.

Approach

  1. Iterate a from 1 to n-1.
  2. For each a, let b = n - a.
  3. Check if both a and b are no-zero integers (i.e., their decimal representation does not contain ‘0’).
  4. Return the first valid pair [a, b].

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
public:
    vector<int> getNoZeroIntegers(int n) {
        for (int a = 1; a < n; ++a) {
            int b = n - a;
            if (to_string(a).find('0') == string::npos && to_string(b).find('0') == string::npos)
                return {a, b};
        }
        return {};
    }
};
1
2
3
4
5
6
7
8
9
func getNoZeroIntegers(n int) []int {
    for a := 1; a < n; a++ {
        b := n - a
        if !strings.Contains(strconv.Itoa(a), "0") && !strings.Contains(strconv.Itoa(b), "0") {
            return []int{a, b}
        }
    }
    return nil
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    public int[] getNoZeroIntegers(int n) {
        for (int a = 1; a < n; ++a) {
            int b = n - a;
            if (!String.valueOf(a).contains("0") && !String.valueOf(b).contains("0"))
                return new int[]{a, b};
        }
        return new int[0];
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    fun getNoZeroIntegers(n: Int): IntArray {
        for (a in 1 until n) {
            val b = n - a
            if (!a.toString().contains('0') && !b.toString().contains('0'))
                return intArrayOf(a, b)
        }
        return intArrayOf()
    }
}
1
2
3
4
5
6
7
class Solution:
    def getNoZeroIntegers(self, n: int) -> list[int]:
        for a in range(1, n):
            b = n - a
            if '0' not in str(a) and '0' not in str(b):
                return [a, b]
        return []
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
impl Solution {
    pub fn get_no_zero_integers(n: i32) -> Vec<i32> {
        for a in 1..n {
            let b = n - a;
            if !a.to_string().contains('0') && !b.to_string().contains('0') {
                return vec![a, b];
            }
        }
        vec![]
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    getNoZeroIntegers(n: number): number[] {
        for (let a = 1; a < n; ++a) {
            const b = n - a;
            if (!a.toString().includes('0') && !b.toString().includes('0'))
                return [a, b];
        }
        return [];
    }
}

Complexity

  • ⏰ Time complexity: O(n * log n), as for each a, we check up to n pairs and string conversion/check takes O(log n) time.
  • 🧺 Space complexity: O(log n), for string conversion of numbers.