![]() |
VOOZH | about |
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: arr[] = [6, 8, 4, 5, 2, 3]
Output: 604
Explanation: The minimum sum is formed by numbers 358 and 246
Input: arr[] = [5, 3, 0, 7, 4]
Output: 82
Explanation: The minimum sum is formed by numbers 35 and 047
To minimize the sum of two numbers formed by the digits of the array, we divide the digits into two halves and assign half of them to each number, ensuring the leading digits are smaller. We use a Min Heap to efficiently retrieve the two smallest elements at a time, appending them alternately to the two numbers. This process continues until all elements are exhausted.
82
Please refer Minimum sum of two numbers formed from digits of an array for another O(n Log n) approach and a O(n) approach