Divide a number into two parts such that sum of digits is maximum | Returns sum of digits of x ; Returns closest number to x in terms of 9 's. ; Driver code
function sumOfDigitsSingle ( x ) { let ans = 0 ; while ( x ) { ans += x % 10 ; x = Math . floor ( x / 10 ) ; } return ans ; } function closest ( x ) { let ans = 0 ; while ( ans * 10 + 9 <= x ) ans = ans * 10 + 9 ; return ans ; } function sumOfDigitsTwoParts ( N ) { let A = closest ( N ) ; return sumOfDigitsSingle ( A ) + sumOfDigitsSingle ( N - A ) ; } let N = 35 ; document . write ( sumOfDigitsTwoParts ( N ) ) ;