Find two numbers whose sum is N and does not contain any digit as K

Given an integer N, the task is to find two numbers a and b such that a + b = N, where a and b doesn’t contain any of the digits as K. Print -1 if not possible.
Examples:
Input: N = 100, K = 0
Output: 1 99
Explanation:
1 + 99 = 100 and none of the numbers has 0 in it. Input: N = 123456789, K = 2
Output: 3456790 119999999Â
Explanation:
3456790 + 119999999 = 123456789 and none of the numbers has 2 in it.
Approach: The idea is to iterate a loop from i = 1 to n-1, and check if i and (n – i) do not contain K. If it doesn’t contain any digit as then print the two numbers and break out of the loop.Â
For Example: N = 11, k = 0
i = 1, N – 1 = 10 but 10 contains 0, so increment i
i = 2, N – 1 = 9, so print this i and n – i.
Below is the implementation of the above approach:
C++
// C++ program for // the above approach#include<bits/stdc++.h>using namespace std;Â
int freqCount(string str, char k){Â Â Â Â int count = 0;Â Â Â Â for(int i = 0; Â Â Â Â Â Â Â Â Â Â Â Â i < str.size(); i++)Â Â Â Â {Â Â Â Â Â Â Â Â if (str[i] == k)Â Â Â Â Â Â Â Â count++;Â Â Â Â }Â Â Â Â return count;}Â
// Function to find two // numbers whose sum// is N and do not // contain any digit as kvoid findAandB(int n, int k){    int flag = 0;         // Check every number i and (n-i)    for(int i = 1; i < n; i++)    {        // Check if i and n-i doesn't        // contain k in them print i and n-i        if (freqCount(to_string(i),                      (char)(k + 48)) == 0 and             freqCount(to_string(n - i),                      (char)(k + 48)) == 0)        {            cout << "(" << i << ", "                 << n - i << ")";            flag = 1;            break;        }    }         // Check if flag is 0     // then print -1    if (flag == 0)        cout << -1;}Â
// Driver Codeint main(){          // Given N and K    int N = 100;    int K = 0;         // Function call    findAandB(N, K);    return 0;}Â
// This code is contributed by Rajput-Ji |
Java
// Java program for the above approachimport java.util.*;Â
class GFG{Â
static int freqCount(String str, char k){Â Â Â Â int count = 0;Â Â Â Â for(int i = 0; i < str.length(); i++)Â Â Â Â {Â Â Â Â Â Â Â Â if (str.charAt(i) == k)Â Â Â Â Â Â Â Â Â Â Â Â count++;Â Â Â Â }Â Â Â Â return count;}Â
// Function to find two numbers // whose sum is N and do not// contain any digit as kstatic void findAandB(int n, int k){Â Â Â Â int flag = 0;Â
    // Check every number i and (n-i)    for(int i = 1; i < n; i++)     {                 // Check if i and n-i doesn't        // contain k in them print i and n-i        if (freqCount(Integer.toString(i),                     (char)(k + 48)) == 0 &&            freqCount(Integer.toString(n - i),                     (char)(k + 48)) == 0)         {            System.out.print("(" + i + ", " +                              (n - i) + ")");            flag = 1;            break;        }    }Â
    // Check if flag is 0    // then print -1    if (flag == 0)        System.out.print(-1);}Â
// Driver codepublic static void main(String[] args){Â
    // Given N and K    int N = 100;    int K = 0;Â
    // Function call    findAandB(N, K);}}Â
// This code is contributed by offbeat |
Python
# Python program for the above approachÂ
# Function to find two numbers whose sum# is N and do not contain any digit as kdef findAandB(n, k):Â Â Â Â Â Â Â flag = 0Â
    # Check every number i and (n-i)    for i in range(1, n):Â
        # Check if i and n-i doesn't        # contain k in them print i and n-i        if str(i).count(chr(k + 48)) == 0 \        and str(n-i).count(chr(k + 48)) == 0:            print(i, n-i)            flag = 1            breakÂ
    # check if flag is 0 then print -1    if(flag == 0):        print(-1)Â
# Driver Codeif __name__ == '__main__':     # Given N and K    N = 100    K = 0         # Function Call    findAandB(N, K) |
C#
// C# program for the // above approachusing System;class GFG{Â
static int freqCount(String str, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â char k){Â Â int count = 0;Â Â Â Â Â for(int i = 0; i < str.Length; i++)Â Â {Â Â Â Â if (str[i] == k)Â Â Â Â Â Â count++;Â Â }Â Â Â Â Â return count;}Â
// Function to find two numbers // whose sum is N and do not// contain any digit as kstatic void findAandB(int n, int k){Â Â int flag = 0;Â
  // Check every number i and (n-i)  for(int i = 1; i < n; i++)   {    // Check if i and n-i doesn't    // contain k in them print i and n-i    if (freqCount(i.ToString(),                 (char)(k + 48)) == 0 &&        freqCount((n-i).ToString(),                  (char)(k + 48)) == 0)     {      Console.Write("(" + i + ", " +                    (n - i) + ")");      flag = 1;      break;    }  }Â
  // Check if flag is 0  // then print -1  if (flag == 0)    Console.Write(-1);}Â
// Driver codepublic static void Main(String[] args){Â Â // Given N and KÂ Â int N = 100;Â Â int K = 0;Â
  // Function call  findAandB(N, K);}}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â
// Javascript program for // the above approachÂ
function freqCount(str, k){Â Â Â Â var count = 0;Â Â Â Â for(var i = 0; Â Â Â Â Â Â Â Â Â Â Â Â i < str.length; i++)Â Â Â Â {Â Â Â Â Â Â Â Â if (str[i] == k)Â Â Â Â Â Â Â Â count++;Â Â Â Â }Â Â Â Â return count;}Â
// Function to find two // numbers whose sum// is N and do not // contain any digit as kfunction findAandB(n, k){    var flag = 0;         // Check every number i and (n-i)    for(var i = 1; i < n; i++)    {        // Check if i and n-i doesn't        // contain k in them print i and n-i        if (freqCount(i.toString(),                      String.fromCharCode(k + 48)) == 0 &&             freqCount((n - i).toString(),                      String.fromCharCode(k + 48)) == 0)        {            document.write( "(" + i + ", "                 + (n - i) + ")");            flag = 1;            break;        }    }         // Check if flag is 0     // then print -1    if (flag == 0)        cout + -1;}Â
// Driver Code// Given N and Kvar N = 100;var K = 0;Â
// Function callfindAandB(N, K);Â
// This code is contributed by rrrtnx.</script> |
(1, 99)
Â
Time Complexity: O(N*L) where L is the length of N
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



