Find two numbers with sum N such that neither of them contains digit K | JavaScript 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
function removeLeadingZeros ( str ) { var i = 0 ; var n = str . length ; while ( str [ i ] == ' ' && 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 ] - ' ' ; if ( D == K ) { var D1 , D2 ; D1 = parseInt ( D / 2 ) ; D2 = parseInt ( D / 2 ) + D % 2 ; A = A + String . fromCharCode ( D1 + ' ' . charCodeAt ( 0 ) ) ; B = B + String . fromCharCode ( D2 + ' ' . charCodeAt ( 0 ) ) ; } else { A = A + String . fromCharCode ( D + ' ' . charCodeAt ( 0 ) ) ; B = B + ' ' ; } } A = removeLeadingZeros ( A ) ; B = removeLeadingZeros ( B ) ; document . write ( A + " " + B ) ; } var N = 33673 ; var K = 3 ; findPairs ( N , K ) ;