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
- Count numbers formed by given two digit with sum having given digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Minimum digits to be removed to make either all digits or alternating digits same
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Sum of all numbers that can be formed with permutations of n digits
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Minimum number of digits to be removed so that no two consecutive digits are same
- N digit numbers divisible by 5 formed from the M digits
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Count of distinct numbers formed by shuffling the digits of a large number N
- Sum of two numbers where one number is represented as array of digits
- Smallest number with given sum of digits and sum of square of digits
- Recursive sum of digits of a number formed by repeated appends
- Sum of the products of same placed digits of two numbers
- Find the maximum sum of digits of the product of two numbers
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Find smallest number with given number of digits and sum of digits
Improved By : nitin mittal