Minimum sum of two numbers formed from digits of an array in O(n)
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 the given array must be used to form the two numbers.
Examples:
Input: arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
246 + 358 = 604
Input: arr[] = {5, 3, 0, 7, 4}
Output: 82
Approach: A minimum number will be formed from the set of digits when smallest digit appears at the most significant position and next to smallest digit appears at next most significant position and so on…
The idea is to build two numbers by alternating picking digits from the array (assuming it is sorted in ascending). So the first number is formed by digits present in odd positions in the array and the second number is formed by digits from even positions in the array. Finally, we return the sum of the first and second number. In order to reduce the time complexity, the array can be sorted in O(n) using the frequency array of digits as every element of the original array is a single digit i.e. there can be at most 10 distinct elements.
Below is the implementation of the above approach:
C++
// C++ implementation of above approach #include<bits/stdc++.h> using namespace std; // Function to return the required minimum sum int minSum(vector<int> arr, int n) { // Array to store the // frequency of each digit int MAX = 10; int *freq = new int[MAX]; for (int i = 0; i < n; i++) { // Store count of every digit freq[arr[i]]++; } // Update arr[] such that it is // sorted in ascending int k = 0; for (int i = 0; i < MAX; i++) { // Adding elements in arr[] // in sorted order for (int j = 0; j < freq[i]; j++) { arr[k++] = i; } } int num1 = 0; int num2 = 0; // Generating numbers alternatively for (int i = 0; i < n; i++) { if (i % 2 == 0) num1 = num1 * MAX + arr[i]; else num2 = num2 * MAX + arr[i]; } // Return the minimum possible sum return num1 + num2; } // Driver code int main(void) { vector<int>arr = { 6, 8, 4, 5, 2, 3 }; int n = arr.size(); cout << minSum(arr, n); } // This code is contributed by ankush_953 |
Java
// Java implementation of above approach public class GFG { public static final int MAX = 10; // Function to return the required minimum sum static int minSum(int arr[], int n) { // Array to store the // frequency of each digit int freq[] = new int[MAX]; for (int i = 0; i < n; i++) { // Store count of every digit freq[arr[i]]++; } // Update arr[] such that it is // sorted in ascending int k = 0; for (int i = 0; i < MAX; i++) { // Adding elements in arr[] // in sorted order for (int j = 0; j < freq[i]; j++) { arr[k++] = i; } } int num1 = 0; int num2 = 0; // Generating numbers alternatively for (int i = 0; i < n; i++) { if (i % 2 == 0) num1 = num1 * MAX + arr[i]; else num2 = num2 * MAX + arr[i]; } // Return the minimum possible sum return num1 + num2; } // Driver code public static void main(String[] args) { int arr[] = { 6, 8, 4, 5, 2, 3 }; int n = arr.length; System.out.print(minSum(arr, n)); } } |
Python3
# Python implementation of above approach # Function to return the required minimum sum def minSum(arr, n): # Array to store the # frequency of each digit MAX = 10 freq = [0]*MAX for i in range(n): # Store count of every digit freq[arr[i]] += 1 # Update arr[] such that it is # sorted in ascending k = 0 for i in range(MAX): # Adding elements in arr[] # in sorted order for j in range(0,freq[i]): arr[k] = i k += 1 num1 = 0 num2 = 0 # Generating numbers alternatively for i in range(n): if i % 2 == 0: num1 = num1 * MAX + arr[i] else: num2 = num2 * MAX + arr[i] # Return the minimum possible sum return num1 + num2 # Driver code arr = [ 6, 8, 4, 5, 2, 3 ] n = len(arr); print(minSum(arr, n)) #This code is contributed by ankush_953 |
C#
// C# implementation of above approach using System; class GFG { public static int MAX = 10; // Function to return the required minimum sum static int minSum(int[] arr, int n) { // Array to store the // frequency of each digit int[] freq = new int[MAX]; for (int i = 0; i < n; i++) { // Store count of every digit freq[arr[i]]++; } // Update arr[] such that it is // sorted in ascending int k = 0; for (int i = 0; i < MAX; i++) { // Adding elements in arr[] // in sorted order for (int j = 0; j < freq[i]; j++) { arr[k++] = i; } } int num1 = 0; int num2 = 0; // Generating numbers alternatively for (int i = 0; i < n; i++) { if (i % 2 == 0) num1 = num1 * MAX + arr[i]; else num2 = num2 * MAX + arr[i]; } // Return the minimum possible sum return num1 + num2; } // Driver code static public void Main() { int[] arr = { 6, 8, 4, 5, 2, 3 }; int n = arr.Length; Console.WriteLine(minSum(arr, n)); } } // This code is contributed by jit_t. |
604
Time Complexity: O(n)
Recommended Posts:
- Minimum sum of two numbers formed from digits of an array
- 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
- Count numbers formed by given two digit with sum having given digits
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Sum of two numbers where one number is represented as array of digits
- Find last k digits in product of an array numbers
- Count the number of digits of palindrome numbers in an array
- Maximum possible time that can be formed from four digits
- Smallest multiple of N formed using the given set of digits
- Find the largest number that can be formed with the given digits
- Count of alphabets whose ASCII values can be formed with the digits of N
- Minimum number of digits to be removed so that no two consecutive digits are same
- Minimum and Maximum prime numbers in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



