text: Divide a number into two parts such that sum of digits is maximum | C ++ implementation of above approach ; Returns sum of digits of x ; Returns closest number to x in terms of 9 's. ; Driver code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfDigitsSingle ( int x ) { int ans = 0 ; while ( x ) { ans += x % 10 ; x /= 10 ; } return ans ; } int closest ( int x ) { int ans = 0 ; while ( ans * 10 + 9 <= x ) ans = ans * 10 + 9 ; return ans ; } int sumOfDigitsTwoParts ( int N ) { int A = closest ( N ) ; return sumOfDigitsSingle ( A ) + sumOfDigitsSingle ( N - A ) ; } int main ( ) { int N = 35 ; cout << sumOfDigitsTwoParts ( N ) ; return 0 ; }