Given an array of digits (values are from 0 to 9), find the minimum possible sum of two numbers formed from digits of the array. All digits of given array must be used to form the two numbers.

**Examples :**

Input: [6, 8, 4, 5, 2, 3] Output: 604 The minimum sum is formed by numbers 358 and 246 Input: [5, 3, 0, 7, 4] Output: 82 The minimum sum is formed by numbers 35 and 047

A minimum number will be formed from set of digits when smallest digit appears at most significant position and next smallest digit appears at next most significant position ans so on..

The idea is to sort the array in increasing order and build two numbers by alternating picking digits from the array. So first number is formed by digits present in odd positions in the array and second number is formed by digits from even positions in the array. Finally, we return the sum of first and second number.

Below is the implementation of above idea.

## C/C++

`// C++ program to find minimum sum of two numbers ` `// formed from digits of the array. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find and return minimum sum of ` `// two numbers formed from digits of the array. ` `int` `solve(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// sort the array ` ` ` `sort(arr, arr + n); ` ` ` ` ` `// let two numbers be a and b ` ` ` `int` `a = 0, b = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `// fill a and b with every alternate digit ` ` ` `// of input array ` ` ` `if` `(i & 1) ` ` ` `a = a*10 + arr[i]; ` ` ` `else` ` ` `b = b*10 + arr[i]; ` ` ` `} ` ` ` ` ` `// return the sum ` ` ` `return` `a + b; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = {6, 8, 4, 5, 2, 3}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `cout << ` `"Sum is "` `<< solve(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum sum of two numbers ` `// formed from digits of the array. ` `import` `java.util.Arrays; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find and return minimum sum of ` ` ` `// two numbers formed from digits of the array. ` ` ` `static` `int` `solve(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` ` ` `// sort the array ` ` ` `Arrays.sort(arr); ` ` ` ` ` `// let two numbers be a and b ` ` ` `int` `a = ` `0` `, b = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// fill a and b with every alternate ` ` ` `// digit of input array ` ` ` `if` `(i % ` `2` `!= ` `0` `) ` ` ` `a = a * ` `10` `+ arr[i]; ` ` ` `else` ` ` `b = b * ` `10` `+ arr[i]; ` ` ` `} ` ` ` ` ` `// return the sum ` ` ` `return` `a + b; ` ` ` `} ` ` ` ` ` `//driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `arr[] = {` `6` `, ` `8` `, ` `4` `, ` `5` `, ` `2` `, ` `3` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.print(` `"Sum is "` ` ` `+ solve(arr, n)); ` ` ` `} ` `} ` ` ` `//This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

` ` `# Python3 program to find minimum sum of two ` `# numbers formed from digits of the array. ` ` ` `# Function to find and return minimum sum of ` `# two numbers formed from digits of the array. ` `def` `solve(arr, n): ` ` ` ` ` `# sort the array ` ` ` `arr.sort() ` ` ` ` ` `# let two numbers be a and b ` ` ` `a ` `=` `0` `; b ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Fill a and b with every alternate ` ` ` `# digit of input array ` ` ` `if` `(i ` `%` `2` `!` `=` `0` `): ` ` ` `a ` `=` `a ` `*` `10` `+` `arr[i] ` ` ` `else` `: ` ` ` `b ` `=` `b ` `*` `10` `+` `arr[i] ` ` ` ` ` `# return the sum ` ` ` `return` `a ` `+` `b ` ` ` `# Driver code ` `arr ` `=` `[` `6` `, ` `8` `, ` `4` `, ` `5` `, ` `2` `, ` `3` `] ` `n ` `=` `len` `(arr) ` `print` `(` `"Sum is "` `, solve(arr, n)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum ` `// sum of two numbers formed ` `// from digits of the array. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find and return ` ` ` `// minimum sum of two numbers ` ` ` `// formed from digits of the array. ` ` ` `static` `int` `solve(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` `// sort the array ` ` ` `Array.Sort(arr); ` ` ` ` ` `// let two numbers be a and b ` ` ` `int` `a = 0, b = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `// fill a and b with every alternate digit ` ` ` `// of input array ` ` ` `if` `(i % 2 != 0) ` ` ` `a = a * 10 + arr[i]; ` ` ` `else` ` ` `b = b * 10 + arr[i]; ` ` ` `} ` ` ` ` ` `// return the sum ` ` ` `return` `a + b; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `[]arr = {6, 8, 4, 5, 2, 3}; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(` `"Sum is "` `+ solve(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum ` `// sum of two numbers formed ` `// from digits of the array. ` ` ` `// Function to find and return ` `// minimum sum of two numbers ` `// formed from digits of the array. ` `function` `solve(` `$arr` `, ` `$n` `) ` `{ ` ` ` `// sort the array ` ` ` `sort(` `$arr` `); sort(` `$arr` `, ` `$n` `); ` ` ` ` ` `// let two numbers be a and b ` ` ` `$a` `= 0; ` `$b` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `// fill a and b with every ` ` ` `// alternate digit of input array ` ` ` `if` `(` `$i` `& 1) ` ` ` `$a` `= ` `$a` `* 10 + ` `$arr` `[` `$i` `]; ` ` ` `else` ` ` `$b` `= ` `$b` `* 10 + ` `$arr` `[` `$i` `]; ` ` ` `} ` ` ` ` ` `// return the sum ` ` ` `return` `$a` `+ ` `$b` `; ` `} ` ` ` `// Driver code ` `$arr` `= ` `array` `(6, 8, 4, 5, 2, 3); ` `$n` `= sizeof(` `$arr` `); ` `echo` `"Sum is "` `, solve(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

Sum is 604

This article is contributed by **Aditya Goel**. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

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:

- Minimum sum of two numbers formed from digits of an array
- Minimum sum of two numbers formed from digits of an array in O(n)
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count numbers formed by given two digit with sum having given digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Minimum digits to be removed to make either all digits or alternating digits same
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Sum of all numbers that can be formed with permutations of n digits
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Minimum number of digits to be removed so that no two consecutive digits are same
- N digit numbers divisible by 5 formed from the M digits
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Count of distinct numbers formed by shuffling the digits of a large number N
- Sum of two numbers where one number is represented as array of digits
- Smallest number with given sum of digits and sum of square of digits
- Recursive sum of digits of a number formed by repeated appends
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Minimize the sum of squares of sum of N/2 paired formed by N numbers