# Minimum sum of two numbers formed from digits of an array

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

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Since we want to minimize the sum of two numbers to be formed, we must divide all digits in two halves and assign half-half digits to them. We also need to make sure that the leading digits are smaller.
We build a Min Heap with the elements of the given array, which takes O(n) worst time. Now we retrieve min values (2 at a time) of array, by polling from the Priority Queue and append these two min values to our numbers, till the heap becomes empty, i.e., all the elements of array get exhausted. We return the sum of two formed numbers, which is our required answer. Overall complexity is O(nlogn) as push() operation takes O(logn) and it’s repeated n times.

## C/C++

 `// C++ program to find minimum sum of two numbers ` `// formed from all digits in a given array. ` `#include ` `using` `namespace` `std; ` ` `  `// Returns sum of two numbers formed ` `// from all digits in a[] ` `int` `minSum(``int` `arr[], ``int` `n) ` `{ ` `    ``// min Heap ` `    ``priority_queue <``int``, vector<``int``>, greater<``int``> > pq; ` ` `  `    ``// to store the 2 numbers formed by array elements to ` `    ``// minimize the required sum ` `    ``string num1, num2; ` ` `  `    ``// Adding elements in Priority Queue ` `    ``for``(``int` `i=0; i

## Java

 `// Java program to find minimum sum of two numbers ` `// formed from all digits in a given array. ` `import` `java.util.PriorityQueue; ` ` `  `class` `MinSum ` `{ ` `    ``// Returns sum of two numbers formed ` `    ``// from all digits in a[] ` `    ``public` `static` `long` `solve(``int``[] a) ` `    ``{ ` `        ``// min Heap ` `        ``PriorityQueue pq = ``new` `PriorityQueue(); ` ` `  `        ``// to store the 2 numbers formed by array elements to ` `        ``// minimize the required sum ` `        ``StringBuilder num1 = ``new` `StringBuilder(); ` `        ``StringBuilder num2 = ``new` `StringBuilder(); ` ` `  `        ``// Adding elements in Priority Queue ` `        ``for` `(``int` `x : a) ` `            ``pq.add(x); ` ` `  `        ``// checking if the priority queue is non empty ` `        ``while` `(!pq.isEmpty()) ` `        ``{ ` `            ``num1.append(pq.poll()+ ``""``); ` `            ``if` `(!pq.isEmpty()) ` `                ``num2.append(pq.poll()+ ``""``); ` `        ``} ` ` `  `        ``// the required sum calculated ` `        ``long` `sum = Long.parseLong(num1.toString()) + ` `                   ``Long.parseLong(num2.toString()); ` ` `  `        ``return` `sum; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `        ``int` `arr[] = {``6``, ``8``, ``4``, ``5``, ``2``, ``3``}; ` `        ``System.out.println(``"The required sum is "``+ solve(arr)); ` `    ``} ` `} `

Output:

```The required sum is 604
```

Anothor method:
We can follow another approach also like this, as we need two numbers such that their sum is minimum, then we would also need two minimum numbers. If we arrange our array in ascending order then we can two digits that will form the smallest numbers,
e.g, 2 3 4 5 6 8, now we can get two numbers starting from 2 and 3. Now first part is done. Now we have to form such that they would contain small digits, i.e pick digits alternatively from array extend your two numbers.
i.e 246, 358. Now if we see analyze this, then we can pick even indexed numbers for num1 and odd number for num2.

Below is the implementation:

## C++

 `// C++ program to find minimum sum of two numbers ` `// formed from all digits in a given array. ` `#include ` `using` `namespace` `std; ` ` `  `// Returns sum of two numbers formed ` `// from all digits in a[] ` `int` `minSum(``int` `a[], ``int` `n){ ` `     `  `    ``// sort the elements ` `    ``sort(a,a+n); ` `     `  `    ``int` `num1 = 0; ` `    ``int` `num2 = 0; ` `    ``for``(``int` `i = 0;i

## Java

 `import` `java.util.Arrays; ` `//Java program to find minimum sum of two numbers ` `//formed from all digits in a given array. ` `public` `class` `AQRQ { ` ` `  `    ``//Returns sum of two numbers formed ` `    ``//from all digits in a[] ` `    ``static` `int` `minSum(``int` `a[], ``int` `n){ ` `       `  `     ``// sort the elements ` `     ``Arrays.sort(a); ` `       `  `     ``int` `num1 = ``0``; ` `     ``int` `num2 = ``0``; ` `     ``for``(``int` `i = ``0``;i

## Python3

 `# Python 3 program to find minimum  ` `# sum of two numbers formed  ` `# from all digits in an given array ` ` `  `# Returns sum of two numbers formed  ` `# from all digits in a[] ` `def` `minSum(a, n): ` `     `  `    ``# sorted the elements ` `    ``a ``=` `sorted``(a) ` `    ``num1, num2 ``=` `0``, ``0` `     `  `    ``for` `i ``in` `range``(n): ` `        ``if` `i ``%` `2` `=``=` `0``: ` `            ``num1 ``=` `num1 ``*` `10` `+` `a[i] ` `        ``else``: ` `            ``num2 ``=` `num2 ``*` `10` `+` `a[i] ` `     `  `    ``return` `num2 ``+` `num1      ` `     `  `# Driver code ` `arr ``=` `[``5``, ``3``, ``0``, ``7``, ``4``] ` `n ``=` `len``(arr) ` `print``(``"The required sum is"``, ` `             ``minSum(arr, n)) ` `     `  `# This code is contributed ` `# by Mohit kumar 29 `

## C#

 `// C# a program to find minimum sum of two numbers ` `//formed from all digits in a given array. ` ` `  `using` `System; ` ` `  `public` `class` `GFG{ ` `     `  `    ``//Returns sum of two numbers formed ` `    ``//from all digits in a[] ` `    ``static` `int` `minSum(``int` `[]a, ``int` `n){ ` `     `  `    ``// sort the elements ` `    ``Array.Sort(a); ` `     `  `    ``int` `num1 = 0; ` `    ``int` `num2 = 0; ` `    ``for``(``int` `i = 0;i

## PHP

 ` `

Output:

```The required sum is 82
```

Time Complexity :
O(nLogN)

This article is contributed by Prakhar. 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.