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.
Input: N = 443, K = 4
Output: 256, 187
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
Sum = 311 + 367 = 678 = N
Also, K(9) is not present in 311 or 367
Hence, 311 and 367 are valid output.
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)
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:
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.
- 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 email@example.com. 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.