Similar Problems

Similar Problems not available

Convert Integer To The Sum Of Two No Zero Integers - Leetcode Solution

Companies:

LeetCode:  Convert Integer To The Sum Of Two No Zero Integers Leetcode Solution

Difficulty: Easy

Topics: math  

Problem Statement:

Given an integer n, return any array consisting of two distinct integers such that they sum up to n, and that the two integers do not contain the digit 0.

If there are multiple valid solutions, return any of them.

Example 1:

Input: n = 2 Output: [1,1] Explanation: A 1 followed by a 0 is not a valid answer because the second integer must be non-zero. Example 2:

Input: n = 11 Output: [2,9] Example 3:

Input: n = 10000 Output: [1,9999] Example 4:

Input: n = 69 Output: [1,68] Example 5:

Input: n = 1010 Output: [11,999]

Approach:

To solve this problem, we will follow the below steps:

Step 1: We will traverse through all the numbers starting from 1 until n-1 Step 2: For each number, we will check if it does not contain the digit 0. If it does, we will skip that number and move to the next number. Step 3: If the number does not contain 0, we will check if n minus that number also does not contain the digit 0. If it does not contain, then we have found our answer, and we can return the array containing the number and n minus that number. Step 4: If we are not able to find any such number, we can simply return -1.

Pseudo Code:

func getNoZeroIntegers(n int) []int { for i:=1; i<n; i++{ if !containsZero(i) && !containsZero(n-i){ return []int{i, n-i} } } return []int{-1} }

func containsZero(n int) bool { for n > 0 { if n%10 == 0 { return true } n /= 10 } return false }

Time Complexity:

The time complexity of this algorithm is O(n), where n is the given integer.

Space Complexity:

The space complexity of this algorithm is O(1), as we are not using any extra data structure apart from the given input.

Conclusion:

We can solve the Convert Integer To The Sum Of Two No Zero Integers problem by traversing through all numbers between 1 to n-1 and checking if a number and its complement both don't contain any 0's. If we find such a pair of numbers, we can return them as the answer.

Convert Integer To The Sum Of Two No Zero Integers Solution Code

1