## Friday, June 24, 2016

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

Minimum sum of two numbers formed from digits of an array - GeeksforGeeks
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.

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.
`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;`
`}`
Read full article from Minimum sum of two numbers formed from digits of an array - GeeksforGeeks