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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Sum of all numbers that can be formed with permutations of n digits
- Count numbers formed by given two digit with sum having given digits
- N digit numbers divisible by 5 formed from the M digits
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- 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
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Minimum number of consecutive sequences that can be formed in an array
- Minimum length of the reduced Array formed using given operations
- Find last k digits in product of an array numbers
- Sum of two numbers where one number is represented as array of digits
- Count of numbers in Array ending with digits of number N
- Count the number of digits of palindrome numbers in an array
- Remove minimum numbers from the array to get minimum OR value
- Smallest multiple of N formed using the given set of digits
- Greatest number less than equal to B that can be formed from the digits of A
Improved By : nitin mittal