Given an integer **N**, the task is to find two positive integers **A** and **B** such that **A + B = N** and the sum of digits of **A** and **B** is minimum. Print the sum of digits of **A** and **B**.

**Examples:**

Input:N = 16

Output:7

(10 + 6) = 16 and (1 + 0 + 6) = 7

is minimum possible.

Input:N = 1000

Output:10

(900 + 100) = 1000

**Approach:** If **N** is a power of **10** then the answer will be **10** otherwise the answer will be the sum of digits of **N**. It is clear that the answer can not be smaller than the sum of digits of **N** because the sum of digits decreases whenever a carry is generated. Moreover, when **N** is a power of **10**, obviously the answer can not be **1**, so the answer will be **10**. Because **A** or **B** can not be **0** as both of them must be positive numbers.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum ` `// possible sum of digits of A ` `// and B such that A + B = n ` `int` `minSum(` `int` `n) ` `{ ` ` ` `// Find the sum of digits of n ` ` ` `int` `sum = 0; ` ` ` `while` `(n > 0) { ` ` ` `sum += (n % 10); ` ` ` `n /= 10; ` ` ` `} ` ` ` ` ` `// If num is a power of 10 ` ` ` `if` `(sum == 1) ` ` ` `return` `10; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 1884; ` ` ` ` ` `cout << minSum(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum ` `// possible sum of digits of A ` `// and B such that A + B = n ` `static` `int` `minSum(` `int` `n) ` `{ ` ` ` `// Find the sum of digits of n ` ` ` `int` `sum = ` `0` `; ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` `sum += (n % ` `10` `); ` ` ` `n /= ` `10` `; ` ` ` `} ` ` ` ` ` `// If num is a power of 10 ` ` ` `if` `(sum == ` `1` `) ` ` ` `return` `10` `; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `1884` `; ` ` ` ` ` `System.out.print(minSum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the approach ` ` ` `# Function to return the minimum ` `# possible sum of digits of A ` `# and B such that A + B = n ` `def` `minSum(n) : ` ` ` ` ` `# Find the sum of digits of n ` ` ` `sum` `=` `0` `; ` ` ` `while` `(n > ` `0` `) : ` ` ` `sum` `+` `=` `(n ` `%` `10` `); ` ` ` `n ` `/` `/` `=` `10` `; ` ` ` ` ` `# If num is a power of 10 ` ` ` `if` `(` `sum` `=` `=` `1` `) : ` ` ` `return` `10` `; ` ` ` ` ` `return` `sum` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `1884` `; ` ` ` ` ` `print` `(minSum(n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum ` `// possible sum of digits of A ` `// and B such that A + B = n ` `static` `int` `minSum(` `int` `n) ` `{ ` ` ` `// Find the sum of digits of n ` ` ` `int` `sum = 0; ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `sum += (n % 10); ` ` ` `n /= 10; ` ` ` `} ` ` ` ` ` `// If num is a power of 10 ` ` ` `if` `(sum == 1) ` ` ` `return` `10; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 1884; ` ` ` ` ` `Console.Write(minSum(n)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

21

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimize the number by changing at most K digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Find the average of k digits from the beginning and l digits from the end of the given number
- Minimum number of digits to be removed so that no two consecutive digits are same
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Smallest number with given sum of digits and sum of square of digits
- Check if the sum of digits of number is divisible by all of its digits
- Sum of the digits of square of the given number which has only 1's as its digits
- Minimize the sum of the array according the given condition
- Minimize the value of N by applying the given operations
- Minimize difference after changing all odd elements to even
- Minimize cost to Swap two given Arrays
- Minimize the non-zero elements in the Array by given operation
- Minimize the maximum difference between the heights
- Minimize the cost to split a number

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.