Minimal Sum

Given an array of digits (O is also allowed), what is the minimal sum of two integers that are made of the digits contained in the array? For simplicity assume array elements are single-digit numbers.


Example:

array: [1,2,7,8,9] The min-sum (129 + 78) should be 207.


Input Format:

A single line containing space-separated integers representing the elements of the arr[].


Output Format:

Print the sum.


Sample Input

1,2,7,8,9


Sample Output

207


Solution in C++


#include<bits/stdc++.h>
using namespace std;
int getMinSum(int *arr, int n) {
   sort(arr, arr + n);
   int a = 0;
   int b = 0;
   for (int i = 0; i < n; ++i) {
      if (i % 2 == 0) {
         a = a * 10 + arr[i];
      } else {
         b = b * 10 + arr[i];
      }
   }
   return a + b;
}
int main() {
   int arr[] = {1, 2, 7, 8, 9};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << "Minimum sum = " << getMinSum(arr, n) << endl;
   return 0;
}