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 = nThe 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 <= 104This approach involves iterating from 1 to n-1 and checking if both the iterated number and the complement to n are no-zero integers. The solution stops once such a pair is found.
The function isNoZero checks if the number contains a '0'. The main logic then iterates through possible pairs, checking each until a valid pair is found.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n). Space Complexity: O(1).
This approach uses a random shuffling of digits to attempt different combinations as a and b until a valid pair is found. While fun, it is generally less efficient than the iterative method.
This randomized approach in Python tries different random numbers, ensuring they are no-zero integers until a valid pair is found.
Average Time Complexity: O(1) in the best case, O(n) or higher in the worst case due to randomness. Space Complexity: O(1).
| Approach | Complexity |
|---|---|
| Iterative Checking Method | Time Complexity: O(n). Space Complexity: O(1). |
| Randomized Shuffling Method | Average Time Complexity: O(1) in the best case, O(n) or higher in the worst case due to randomness. Space Complexity: O(1). |
TWO SUM II - Amazon Coding Interview Question - Leetcode 167 - Python • NeetCode • 460,623 views views
Watch 9 more video solutions →Practice Convert Integer to the Sum of Two No-Zero Integers with our built-in code editor and test cases.
Practice on FleetCode