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 = 4Output:256, 187Explanation:

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 = 9Output:311, 367Explanation:

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 = 10^{18}.

**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 D24 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++

`// 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*

*filter_none*

## Java

`// 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*

*filter_none*

## Python3

`# 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*

*filter_none*

## C#

`// 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*

*filter_none*

**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.

## Recommended Posts:

- Minimum number of integers required such that each Segment contains at least one of them
- Count of N-digit numbers having digit XOR as single digit
- Maximise the sum of two Numbers using at most one swap between them
- Sum of all N-digit palindromic numbers which doesn't contains 0 and are divisible by 9
- Check whether it is possible to join two points given on circle such that distance between them is k
- Convert a number of length N such that it contains any one digit at least 'K' times
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Find two numbers made up of a given digit such that their difference is divisible by N
- Count numbers in a range with digit sum divisible by K having first and last digit different
- GCD of two numbers when one of them can be very large
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Count numbers up to N which contains at least one repeated digit
- Largest number less than N with digit sum greater than the digit sum of N
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Find nth number that contains the digit k or divisible by k.
- Count pairs in an array such that the absolute difference between them is ≥ K
- Count the pairs in an array such that the difference between them and their indices is equal
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence

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.