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

In this guide, you will get 1317. Convert Integer to the Sum of Two No-Zero Integers LeetCode Solution with the best time and space complexity. The solution to Convert Integer to the Sum of Two No-Zero Integers problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Convert Integer to the Sum of Two No-Zero Integers solution in C++
  4. Convert Integer to the Sum of Two No-Zero Integers solution in Java
  5. Convert Integer to the Sum of Two No-Zero Integers solution in Python
  6. Additional Resources
1317. Convert Integer to the Sum of Two No-Zero Integers LeetCode Solution image

Problem Statement of Convert Integer to the Sum of Two No-Zero Integers

No-Zero integer is a positive integer that does not contain any 0 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.

Example 1:

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:

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 <= 104

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(h)
See also  2750. Ways to Split Array Into Good Subarrays LeetCode Solution

1317. Convert Integer to the Sum of Two No-Zero Integers LeetCode Solution in C++

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};
    }

    throw;
  }
};
/* code provided by PROGIEZ */

1317. Convert Integer to the Sum of Two No-Zero Integers LeetCode Solution in Java

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};
    }

    throw new IllegalArgumentException();
  }
}
// code provided by PROGIEZ

1317. Convert Integer to the Sum of Two No-Zero Integers LeetCode Solution in Python

class Solution:
  def getNoZeroIntegers(self, n: int) -> list[int]:
    for A in range(n):
      B = n - A
      if '0' not in str(A) and '0' not in str(B):
        return A, B
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.