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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Returns sum of digits of x
int 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 code
int main()
{
    int N = 35;
    cout << sumOfDigitsTwoParts(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
// Returns sum of digits of x
import 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 code
public static void main(String args[])
{
    int N = 35;
    System.out.print(sumOfDigitsTwoParts(N));
}
}
  
// This code is contributed by
// Subhadeep Gupta

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of above approach 
  
#  Returns sum of digits of x 
def sumOfDigitsSingle(x) :
    ans = 0
    while x :
        ans += x % 10
        x //= 10
  
    return ans
  
# Returns closest number to x in terms of 9's
def 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 Code
if __name__ == "__main__" :
  
    N = 35
    print(sumOfDigitsTwoParts(N))
  
# This code is contributed by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 code
public static void Main()
{
    int N = 35;
    Console.Write(sumOfDigitsTwoParts(N));
}
}
  

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach
  
// Returns sum of digits of x
function 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
?>

chevron_right


Output:

17


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.