Find two numbers with sum N such that neither of them contains digit K

Given a number N and a digit K (1 < K <= 9), the task is to find two integers A and B such that A + B = N and digit K is not present in either A or B.

Examples: 

Input: N = 443, K = 4 
Output: 256, 187 
Explanation: 
Sum = 256 + 187 = 443 = N 
Also, K(4) is not present in 256 or 187 
Hence, 256 and 187 are valid output.

Input: N = 678, K = 9 
Output: 311, 367 
Explanation: 
Sum = 311 + 367 = 678 = N 
Also, K(9) is not present in 311 or 367 
Hence, 311 and 367 are valid output. 

Naive Approach: 
A simple approach is to run a loop and consider each element (say A) from 1 to N-1. Since the sum of A and B has to be N, corresponding B will be (N – A). Now check if both A and B contain K or not, if no, then print A and B. This approach will fail for larger inputs like N = 1018



Time complexity: O(N) 
Auxiliary Space: O(1)

Efficient Approach: 
The given problem can be solved much more efficiently. The idea is to break each digit D of N into two parts D1 and D2 such that D1 + D2 = D. Take two strings A and B, A will contain all D1’s, and string B will contain all D2’s. 
For example, N = 4467 and K = 4 

D    D1    D2
4    2    2
4    2    2
6    6    0
7    7    0    

Here, A will be 2267 and B will be 2200.
Detailed steps of this approach are as follows: 
1. Traverse N and consider each of its digit D
2. If digit D is same as K, break it into D1 = D / 2 and D2 = D / 2 + D % 2. 
D % 2 is added to D2 to ensure that D1 + D2 = D for both even and odd D. 
3. Else, break it into D1 = D and D2 = 0. 
4. Keep appending D1’s to string A and D2’s to string B. 
5. Finally, print string A and B, after removing leading zeros(if any).

Below is the implementation of the above approach: 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of
// the above approach
 
#include <iostream>
using namespace std;
 
string removeLeadingZeros(string str)
{
    // Count leading zeros
    int i = 0;
    int n = str.length();
    while (str[i] == '0' && i < n)
        i++;
 
    // It removes i characters
    // starting from index 0
    str.erase(0, i);
 
    return str;
}
 
void findPairs(int sum, int K)
{
 
    string A, B;
    A = "";
    B = "";
 
    string N = to_string(sum);
    int n = N.length();
 
    // Check each digit of the N
    for (int i = 0; i < n; i++) {
 
        int D = N[i] - '0';
 
        // If digit is K break it
        if (D == K) {
            int D1, D2;
            D1 = D / 2;
 
            // For odd numbers
            D2 = D / 2 + D % 2;
 
            // Add D1 to A and D2 to B
            A = A + char(D1 + '0');
            B = B + char(D2 + '0');
        }
 
        // If the digit is not K,
        // no need to break
        // string D in A and 0 in B
        else {
            A = A + char(D + '0');
            B = B + '0';
        }
    }
 
    // Remove leading zeros
    A = removeLeadingZeros(A);
    B = removeLeadingZeros(B);
 
    // Print the answer
    cout << A << ", " << B << endl;
}
 
// Driver code
int main()
 
{
    int N = 33673;
    int K = 3;
 
    findPairs(N, K);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
class GFG{
 
static String removeLeadingZeros(String str)
{
     
    // Count leading zeros
    int i = 0;
    int n = str.length();
     
    while (str.charAt(i) == '0' && i < n)
        i++;
 
    // It removes i characters
    // starting from index 0
    str = str.substring(i);
 
    return str;
}
 
static void findPairs(int sum, int K)
{
 
    String A, B;
    A = "";
    B = "";
 
    String N = String.valueOf(sum);
    int n = N.length();
 
    // Check each digit of the N
    for(int i = 0; i < n; i++)
    {
       int D = N.charAt(i) - '0';
        
       // If digit is K break it
       if (D == K)
       {
           int D1, D2;
           D1 = D / 2;
            
           // For odd numbers
           D2 = D / 2 + D % 2;
            
           // Add D1 to A and D2 to B
           A = A + (char)(D1 + '0');
           B = B + (char)(D2 + '0');
       }
        
       // If the digit is not K,
       // no need to break
       // String D in A and 0 in B
       else
       {
           A = A + (char)(D + '0');
           B = B + '0';
       }
    }
     
    // Remove leading zeros
    A = removeLeadingZeros(A);
    B = removeLeadingZeros(B);
 
    // Print the answer
    System.out.print(A + ", " + B + "\n");
}
 
// Driver code
public static void main(String[] args)
{
    int N = 33673;
    int K = 3;
 
    findPairs(N, K);
}
}
 
// This code is contributed by sapnasingh4991

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the
# above approach
def removeLeadingZeros(Str):
     
    # Count leading zeros
    i = 0
    n = len(Str)
       
    while (Str[i] == '0' and i < n):
        i += 1
   
    # It removes i characters
    # starting from index 0
    Str = Str[i:]
    return Str
   
def findPairs(Sum, K):
  
    A = ""
    B = ""
   
    N = str(Sum)
    n = len(N)
 
    # Check each digit of the N
    for i in range(n):
     
       D = int(N[i]) - int('0')
          
       # If digit is K break it
       if (D == K):
           D1 = D // 2;
              
           # For odd numbers
           D2 = (D // 2) + (D % 2)
 
           # Add D1 to A and D2 to B
           A = A + (str)(D1 + int('0'))
           B = B + (str)(D2 + int('0'))
            
       # If the digit is not K,
       # no need to break
       # String D in A and 0 in B
       else:
           A = A + (str)(D + int('0'))
           B = B + '0'
     
    # Remove leading zeros
    A = removeLeadingZeros(A)
    B = removeLeadingZeros(B)
   
    # Print the answer
    print(A + ", " + B)
   
# Driver code
N = 33673
K = 3
 
findPairs(N, K)
 
# This code is contributed divyeshrabadiya07

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
 
class GFG{
  
static String removeLeadingZeros(String str)
{
      
    // Count leading zeros
    int i = 0;
    int n = str.Length;
      
    while (str[i] == '0' && i < n)
        i++;
  
    // It removes i characters
    // starting from index 0
    str = str.Substring(i);
  
    return str;
}
  
static void findPairs(int sum, int K)
{
  
    String A, B;
    A = "";
    B = "";
  
    String N = String.Join("", sum);
    int n = N.Length;
  
    // Check each digit of the N
    for(int i = 0; i < n; i++)
    {
       int D = N[i] - '0';
         
       // If digit is K break it
       if (D == K)
       {
           int D1, D2;
           D1 = D / 2;
             
           // For odd numbers
           D2 = D / 2 + D % 2;
             
           // Add D1 to A and D2 to B
           A = A + (char)(D1 + '0');
           B = B + (char)(D2 + '0');
       }
         
       // If the digit is not K,
       // no need to break
       // String D in A and 0 in B
       else
       {
           A = A + (char)(D + '0');
           B = B + '0';
       }
    }
      
    // Remove leading zeros
    A = removeLeadingZeros(A);
    B = removeLeadingZeros(B);
  
    // Print the answer
    Console.Write(A + ", " + B + "\n");
}
  
// Driver code
public static void Main(String[] args)
{
    int N = 33673;
    int K = 3;
  
    findPairs(N, K);
}
}
 
// This code is contributed by sapnasingh4991

chevron_right


Output: 

11671, 22002

 

Time complexity: O(M) 
Auxiliary Space: O(M) 
Here, M is the number of digits in N.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.