VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-sum-two-numbers-formed-digits-array-2/

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


  • Courses
  • Tutorials
  • Interview Prep

Minimum sum of two numbers formed from digits of an array

Last Updated : 23 Jul, 2025

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

Min Heap-based Greedy Approach - O(n * log n) and O(n) space

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.


Output
82

More Efficient Approaches :

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

Comment