Find two numbers with sum N such that neither of them contains digit K | C ++ implementation of the above approach ; Count leading zeros ; It removes i characters starting from index 0 ; Check each digit of the N ; If digit is K break it ; For odd numbers ; Add D1 to A and D2 to B ; If the digit is not K , no need to break string D in A and 0 in B ; Remove leading zeros ; Print the answer ; Driver code
#include <iostream> NEW_LINE 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 ; }