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

Difficulty Level Easy
Frequently asked in HRT
algorithms coding Interview interviewprep LeetCode LeetCodeSolutions MathViews 1810

The problem Convert Integer to the Sum of Two No-Zero Integers Leetcode Solution asked us to partition the given integer. We should partition the given integer into two numbers. There is a constraint imposed on these two integers. These two integers should not contain the digit 0. For a better understanding, we will take a look at a few examples.

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

n = 2
[1,1]

Explanation: The two integers in the output are 1 and 1. The integers can be the same but they must not have digit 0. The constraint is met in the output. The sum of both the integers is also equal to the input. Thus the output is correct.

n = 1010
[11, 999]

Explanation: The output is correct because the sum of both the integers is equal to 1010. And they also do not have any digit equal to 0.

Approach for Convert Integer to the Sum of Two No-Zero Integers Leetcode Solution

The problem asked us to partition the given input into two integers. The condition which should be met is already stated in the description above. The first condition is that the sum of the integers must be equal to the given integer. The two integers that will be returned as the output should not contain any 0 digits. To solve the problem, we run a loop over the first integer that ranges from 1 to n. The second integer can be deduced from the first relation. Then we check if both the integers satisfy the second condition.

For checking the second condition, we create a custom function that takes an integer as input. It returns true or false depending on whether the given integer contains 0 or not. For checking that we simply keep on removing digits one by one from the end.

Code

C++ code to Convert Integer to the Sum of Two No-Zero Integers Leetcode Solution

#include <bits/stdc++.h>
using namespace std;

bool check(int n){
    while(n>0){
        if(n%10 == 0)
            return false;
        n/=10;
    }
    return true;
}

vector<int> getNoZeroIntegers(int n) {
    for(int i=1;i<n;i++){
        if(check(i) && check(n-i))
            return {i, n-i};
    }
    return {-1,-1};
}

int main(){
    int input = 1010;
    vector<int> output = getNoZeroIntegers(input);
    cout<<output[0]<<" "<<output[1];
}
11 999

Java code to Convert Integer to the Sum of Two No-Zero Integers Leetcode Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
  private static boolean check(int n){
        while(n>0){
            if(n%10 == 0)return false;
            n/=10;
        }
        return true;
    }
    
    public static int[] getNoZeroIntegers(int n) {
        for(int i=1;i<n;i++){
            if(check(i) && check(n-i))
                return new int[]{i, n-i};
        }
        return new int[]{-1,-1};
    }
    
  public static void main (String[] args) throws java.lang.Exception{
    int input = 1010;
      int[] output = getNoZeroIntegers(input);
      System.out.print(output[0]+" "+output[1]);
  }
}
11 999

Complexity Analysis

Time Complexity

O(NlogN), where N is the integer given as input. The log has base 10 because our function that checks whether the integer contains 0 or not works in logN time.

Space Complexity

O(1), we do not store any information. And entire algorithm uses a constant number of variables. Thus the space complexity is constant.

Translate »