Divide a number into two parts such that sum of digits is maximum - GeeksforGeeks

28 Mar, 2022
Given a number N. The task is to find the maximum possible value of SumOfDigits(A) + SumOfDigits(B) such that A + B = n (0<=A, B<=n).
Examples: 
Input: N = 35
Output: 17
35 = 9 + 26
SumOfDigits(26) = 8, SumOfDigits(9) = 9
So, 17 is the answer.

Input: N = 7
Output: 7
Approach: The idea is to divide the number into two parts A and B such that A is in terms of 9 i.e. closest number to N and B = N-A. For example, N = 35, Smallest Number that is closest to 35 is 29. 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Returns sum of digits of xint sumOfDigitsSingle(int x){    int ans = 0;    while (x) {        ans += x % 10;        x /= 10;    }    return ans;} // Returns closest number to x in terms of 9's.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);} // Driver codeint main(){    int N = 35;    cout << sumOfDigitsTwoParts(N);    return 0;}
// Java implementation of above approach// Returns sum of digits of ximport java.util.*;import java.lang.*;import java.io.*; class GFG{static int sumOfDigitsSingle(int x){    int ans = 0;    while (x != 0)    {        ans += x % 10;        x /= 10;    }    return ans;} // Returns closest number to x// in terms of 9's.static int closest(int x){    int ans = 0;    while (ans * 10 + 9 <= x)        ans = ans * 10 + 9;     return ans;} static int sumOfDigitsTwoParts(int N){    int A = closest(N);    return sumOfDigitsSingle(A) +           sumOfDigitsSingle(N - A);} // Driver codepublic static void main(String args[]){    int N = 35;    System.out.print(sumOfDigitsTwoParts(N));}} // This code is contributed by// Subhadeep Gupta
# Python 3 implementation of above approach #  Returns sum of digits of xdef sumOfDigitsSingle(x) :    ans = 0    while x :        ans += x % 10        x //= 10     return ans # Returns closest number to x in terms of 9'sdef closest(x) :    ans = 0    while (ans * 10 + 9 <= x) :        ans = ans * 10 + 9     return ans def sumOfDigitsTwoParts(N) :    A = closest(N)     return sumOfDigitsSingle(A) + sumOfDigitsSingle(N - A)  # Driver Codeif __name__ == "__main__" :     N = 35    print(sumOfDigitsTwoParts(N)) # This code is contributed by ANKITRAI1
// C# implementation of above approach// Returns sum of digits of x using System;class GFG{static int sumOfDigitsSingle(int x){    int ans = 0;    while (x != 0)    {        ans += x % 10;        x /= 10;    }    return ans;}  // Returns closest number to x// in terms of 9's.static int closest(int x){    int ans = 0;    while (ans * 10 + 9 <= x)        ans = ans * 10 + 9;      return ans;}  static int sumOfDigitsTwoParts(int N){    int A = closest(N);    return sumOfDigitsSingle(A) +           sumOfDigitsSingle(N - A);}  // Driver codepublic static void Main(){    int N = 35;    Console.Write(sumOfDigitsTwoParts(N));}}
<?php// PHP implementation of above approach // Returns sum of digits of xfunction sumOfDigitsSingle($x){    $ans = 0;    while ($x)    {        $ans += $x % 10;        $x /= 10;    }    return $ans;} // Returns closest number// to x in terms of 9's.function closest($x){    $ans = 0;    while ($ans * 10 + 9 <= $x)        $ans = $ans * 10 + 9;     return $ans;} function sumOfDigitsTwoParts($N){    $A = closest($N);    return sumOfDigitsSingle($A) +           sumOfDigitsSingle($N - $A);} // Driver code$N = 35;echo sumOfDigitsTwoParts($N); // This code is contributed// by inder_verma?>
<script> // JavaScript implementation of above approach // Returns sum of digits of xfunction sumOfDigitsSingle(x){    let ans = 0;    while (x)    {        ans += x % 10;        x = Math.floor(x / 10);    }    return ans;} // Returns closest number to x// in terms of 9's.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);} // Driver codelet N = 35; document.write(sumOfDigitsTwoParts(N)); // This code is contributed by Surbhi Tyagi. </script>
17
 
Time Complexity: O(log10N)Auxiliary Space: O(1)
ankthon
tufan_gupta2000
ukasp
inderDuMCA
surbhityagi15
rohitsingh07052
number-digits
number-theory
Mathematical
School Programming
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to find sum of elements in a given array
Operators in C / C++
Euclidean algorithms (Basic and Extended)
Algorithm to solve Rubik's Cube
Write a program to calculate pow(x,n)
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java