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 strings A and B, after removing leading zeros(if any).
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
string removeLeadingZeros(string str)
{
int i = 0;
int n = str.length();
while (str[i] == '0' && i < n)
i++;
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();
for ( int i = 0; i < n; i++) {
int D = N[i] - '0' ;
if (D == K) {
int D1, D2;
D1 = D / 2;
D2 = D / 2 + D % 2;
A = A + char (D1 + '0' );
B = B + char (D2 + '0' );
}
else {
A = A + char (D + '0' );
B = B + '0' ;
}
}
A = removeLeadingZeros(A);
B = removeLeadingZeros(B);
cout << A << ", " << B << endl;
}
int main()
{
int N = 33673;
int K = 3;
findPairs(N, K);
return 0;
}
|
Java
class GFG{
static String removeLeadingZeros(String str)
{
int i = 0 ;
int n = str.length();
while (str.charAt(i) == '0' && i < n)
i++;
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();
for ( int i = 0 ; i < n; i++)
{
int D = N.charAt(i) - '0' ;
if (D == K)
{
int D1, D2;
D1 = D / 2 ;
D2 = D / 2 + D % 2 ;
A = A + ( char )(D1 + '0' );
B = B + ( char )(D2 + '0' );
}
else
{
A = A + ( char )(D + '0' );
B = B + '0' ;
}
}
A = removeLeadingZeros(A);
B = removeLeadingZeros(B);
System.out.print(A + ", " + B + "\n" );
}
public static void main(String[] args)
{
int N = 33673 ;
int K = 3 ;
findPairs(N, K);
}
}
|
Python3
def removeLeadingZeros( Str ):
i = 0
n = len ( Str )
while ( Str [i] = = '0' and i < n):
i + = 1
Str = Str [i:]
return Str
def findPairs( Sum , K):
A = ""
B = ""
N = str ( Sum )
n = len (N)
for i in range (n):
D = int (N[i]) - int ( '0' )
if (D = = K):
D1 = D / / 2 ;
D2 = (D / / 2 ) + (D % 2 )
A = A + ( str )(D1 + int ( '0' ))
B = B + ( str )(D2 + int ( '0' ))
else :
A = A + ( str )(D + int ( '0' ))
B = B + '0'
A = removeLeadingZeros(A)
B = removeLeadingZeros(B)
print (A + ", " + B)
N = 33673
K = 3
findPairs(N, K)
|
C#
using System;
class GFG{
static String removeLeadingZeros(String str)
{
int i = 0;
int n = str.Length;
while (str[i] == '0' && i < n)
i++;
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;
for ( int i = 0; i < n; i++)
{
int D = N[i] - '0' ;
if (D == K)
{
int D1, D2;
D1 = D / 2;
D2 = D / 2 + D % 2;
A = A + ( char )(D1 + '0' );
B = B + ( char )(D2 + '0' );
}
else
{
A = A + ( char )(D + '0' );
B = B + '0' ;
}
}
A = removeLeadingZeros(A);
B = removeLeadingZeros(B);
Console.Write(A + ", " + B + "\n" );
}
public static void Main(String[] args)
{
int N = 33673;
int K = 3;
findPairs(N, K);
}
}
|
Javascript
<script>
function removeLeadingZeros(str)
{
var i = 0;
var n = str.length;
while (str[i] == '0' && i < n)
i++;
return str;
}
function findPairs(sum, K)
{
var A, B;
A = "" ;
B = "" ;
var N = (sum.toString());
var n = N.length;
for ( var i = 0; i < n; i++) {
var D = N[i] - '0' ;
if (D == K) {
var D1, D2;
D1 = parseInt(D / 2);
D2 = parseInt(D / 2) + D % 2;
A = A +
String.fromCharCode(D1 + '0' .charCodeAt(0));
B = B +
String.fromCharCode(D2 + '0' .charCodeAt(0));
}
else {
A = A +
String.fromCharCode(D + '0' .charCodeAt(0));
B = B + '0' ;
}
}
A = removeLeadingZeros(A);
B = removeLeadingZeros(B);
document.write( A + ", " + B );
}
var N = 33673;
var K = 3;
findPairs(N, K);
</script>
|
Time complexity: O(M)
Auxiliary Space: O(M)
Here, M is the number of digits in N.
Last Updated :
26 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...