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.
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
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.
Below is the implementation of above idea.
Sum is 604
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Minimum sum of two numbers formed from digits of an array in O(n)
- Minimum sum of two numbers formed from digits of an array
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Sum of all numbers that can be formed with permutations of n digits
- N digit numbers divisible by 5 formed from the M digits
- Count numbers formed by given two digit with sum having given digits
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Minimum number of consecutive sequences that can be formed in an array
- Find last k digits in product of an array numbers
- Sum of two numbers where one number is represented as array of digits
- Count the number of digits of palindrome numbers in an array
- Smallest multiple of N formed using the given set of digits
- Find the largest number that can be formed with the given digits
- Greatest number less than equal to B that can be formed from the digits of A
Improved By : nitin mittal