Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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

Naive Approach: The idea is to pick a number continuously from 0 to N, that will be our first number. Then another number will be the N-first number. Then find the maximum possible value of SumOfDigits(first number) + SumOfDigits(second number).

Steps:

  1. Initialize a variable “ans” with the value INT_MIN. It will contain the final answer
  2. Iterate over the range from 0 to N and perform the following steps:
    • Pick a number one by one the number picked by the loop will be our first number and the second number will be the N – first number.
    • Now find the sum of digits in both numbers and maximize the value in the variable ans.
  3. After the above steps, print the value stored in ans as the result.

Code:

C++




// 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;
}
 
// Function to find the sum of digits
// of two parts
int sumOfDigitsTwoParts(int N)
{
    // To store answer
    int ans = INT_MIN;
 
    for (int i = 0; i <= N; i++) {
        // Find sum of Digits of both
        // first number and second number
        int temp = sumOfDigitsSingle(i)
                   + sumOfDigitsSingle(N - i);
 
        // Update ans with maximum
        // sum of digits
        ans = max(ans, temp);
    }
    return ans;
}
 
// Driver Code
int main()
{
    int N = 35;
    cout << sumOfDigitsTwoParts(N);
 
    return 0;
}


Java




// Java implementation of above approach
import java.util.*;
public class GFG {
    // Returns sum of digits of x
    static int sumOfDigitsSingle(int x)
    {
        int ans = 0;
        while (x != 0) {
            ans += x % 10;
            x /= 10;
        }
        return ans;
    }
 
    // Function to find the sum of digits of two parts
    static int sumOfDigitsTwoParts(int N)
    {
        // To store the answer
        int ans = Integer.MIN_VALUE;
 
        for (int i = 0; i <= N; i++) {
            // Find the sum of digits of both
            // first number and second number
            int temp = sumOfDigitsSingle(i)
                       + sumOfDigitsSingle(N - i);
 
            // Update ans with the maximum
            // sum of digits
            ans = Math.max(ans, temp);
        }
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 35;
        System.out.println(sumOfDigitsTwoParts(N));
    }
}


Python3




# Returns sum of digits of x
def sumOfDigitsSingle(x):
    ans = 0
    while x:
        ans += x % 10
        x //= 10
    return ans
 
# Function to find the sum of digits
# of two parts
 
 
def sumOfDigitsTwoParts(N):
    # To store answer
    ans = float('-inf')
 
    for i in range(N + 1):
        # Find sum of Digits of both
        # first number and second number
        temp = sumOfDigitsSingle(i) + sumOfDigitsSingle(N - i)
 
        # Update ans with maximum
        # sum of digits
        ans = max(ans, temp)
 
    return ans
 
 
# Driver Code
N = 35
print(sumOfDigitsTwoParts(N))


C#




using System;
 
class GFG
{
    // Returns sum of digits of x
    static int SumOfDigitsSingle(int x)
    {
        int ans = 0;
        while (x != 0)
        {
            ans += x % 10;
            x /= 10;
        }
        return ans;
    }
 
    // Function to find the sum of digits
    // of two parts
    static int SumOfDigitsTwoParts(int N)
    {
        // To store the answer
        int ans = int.MinValue;
 
        for (int i = 0; i <= N; i++)
        {
            // Find the sum of digits of both
            // the first number and the second number
            int temp = SumOfDigitsSingle(i)
                       + SumOfDigitsSingle(N - i);
 
            // Update ans with the maximum
            // sum of digits
            ans = Math.Max(ans, temp);
        }
        return ans;
    }
 
    // Driver Code
    static void Main()
    {
        int N = 35;
        Console.WriteLine(SumOfDigitsTwoParts(N));
    }
}
 
// This code is contributed by shivamgupta310570


Javascript




// JavaScript implementation of above approach
 
// Returns sum of digits of x
function sumOfDigitsSingle(x)
{
    let ans = 0;
    while (x) {
        ans += x % 10;
        x = Math.floor(x / 10);
    }
    return ans;
}
 
// Function to find the sum of digits
// of two parts
function sumOfDigitsTwoParts(N)
{
    // To store answer
    let ans = Number.MIN_VALUE;
 
    for (let i = 0; i <= N; i++) {
        // Find sum of Digits of both
        // first number and second number
        let temp = sumOfDigitsSingle(i)
                + sumOfDigitsSingle(N - i);
 
        // Update ans with maximum
        // sum of digits
        ans = Math.max(ans, temp);
    }
    return ans;
}
 
// Driver Code
    let N = 35;
    console.log(sumOfDigitsTwoParts(N));


Output

17





Time Complexity: O(N), because of the loop from 0 to N
Auxiliary Space: O(1), because no extra space has been used

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, the Smallest Number that is closest to 35 is 29. 

  • Define a function sumOfDigitsSingle that takes an integer x as input and returns the sum of its digits.
  • Define a function closest that takes an integer x as input and returns the closest number to x in terms of 9’s. This is done by iteratively multiplying ans by 10 and adding 9 until ans * 10 + 9 is greater than x.
  • Define a function sumOfDigitsTwoParts that takes an integer N as input and calculates the sum of the digits of N by splitting it into two parts – the first part is the closest number to N in terms of 9’s, obtained using the closest function, and the second part is the remaining digits. The sum of the digits of these two parts is calculated using the sumOfDigitsSingle function and added together to get the final result.
  • In the main function, define an integer N and call the sumOfDigitsTwoParts function with N as input. 
  • Print the output.

C++




// 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;
}


C




// C implementation of above approach
#include <stdio.h>
 
// 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;
    printf("%d",sumOfDigitsTwoParts(N));
    return 0;
}
 
// This code is contributed by kothavvsaakash.


Java




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


Python3




# 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


C#




// 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));
}
}


Javascript




<script>
 
// JavaScript implementation of above approach
 
// Returns sum of digits of x
function 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 code
let N = 35;
 
document.write(sumOfDigitsTwoParts(N));
 
// This code is contributed by Surbhi Tyagi.
 
</script>


PHP




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


Output

17





Time Complexity: O(log10N)
Auxiliary Space: O(1)



Last Updated : 25 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads