Minimum sum of two numbers formed from digits of an array
Last Updated :
15 Jul, 2025
Given an array arr[] of digits (values are from 0 to 9), find the minimum possible sum of two numbers formed using all digits of the array.
Note: We need to return the minimum possible sum as a string.
Examples:
Input: arr[] = [6, 8, 4, 5, 2, 3, 0]
Output: "604"
Explanation: The minimum sum is formed by numbers 0358 and 246.
Input: arr[] = [5, 3, 0, 7, 4]
Output: "82"
Explanation: The minimum sum is formed by numbers 35 and 047.
[Naive Approach] Using Sorting - O(nlogn) time and O(n) space
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 and so on. The idea is to sort the array in increasing order and build the first number by digits present in odd positions and second number by digits from even positions in the array. So, that the first two smallest digits are placed in the most significant positions of both numbers, the next two smallest digits are placed in the second most significant positions, and this pattern continues for the remaining digits.
Illustration:
C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// Function to add two strings and return the result
string addString(string &s1, string &s2) {
int i = s1.length() - 1 ;
int j = s2.length() - 1 ;
// initial carry is zero
int carry = 0 ;
// we will calculate and store the
// resultant sum in reverse order in res
string res = "" ;
while (i >= 0 || j >= 0 || carry > 0) {
int sum = carry ;
if (i >= 0)
sum += (s1[i] - '0') ;
if (j >= 0)
sum += (s2[j] - '0') ;
res.push_back(sum % 10 + '0') ;
carry = sum / 10 ;
i-- ;
j-- ;
}
// remove leading zeroes which are currently
// at the back due to reversed string res
while (!res.empty() && res.back() == '0')
res.pop_back() ;
// reverse our final string
reverse(res.begin(), res.end()) ;
return res ;
}
// Function to find and return minimum sum of
// two numbers formed from digits of the array.
string minSum(vector<int> &arr) {
sort(arr.begin(), arr.end()) ;
// Two String for storing the two minimum numbers
string s1 = "", s2 = "" ;
for (int i = 0; i < arr.size(); i++) {
if (i % 2 == 0)
s1.push_back(arr[i] + '0') ;
else
s2.push_back(arr[i] + '0') ;
}
return addString(s1, s2);
}
int main() {
vector<int> arr = {6, 8, 4, 5, 2, 3, 0};
cout << minSum(arr);
return 0;
}
Java
import java.util.Arrays;
class GfG {
// Function to add two numeric strings and return the sum as a string
static String addString(String s1, String s2) {
int i = s1.length() - 1;
int j = s2.length() - 1;
int carry = 0;
String res = "";
while (i >= 0 || j >= 0 || carry > 0) {
int sum = carry;
if (i >= 0)
sum += s1.charAt(i--) - '0';
if (j >= 0)
sum += s2.charAt(j--) - '0';
res = (sum % 10) + res;
carry = sum / 10;
}
// Remove leading zeros
res = res.replaceFirst("^0+(?!$)", "");
return res;
}
// Function to find minimum sum of two numbers formed from digits
static String minSum(int[] arr) {
Arrays.sort(arr);
String s1 = "", s2 = "";
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0)
// add to first number
s1 += arr[i];
else
// add to second number
s2 += arr[i];
}
return addString(s1, s2);
}
public static void main(String[] args) {
int[] arr = {6, 8, 4, 5, 2, 3, 0};
System.out.println(minSum(arr));
}
}
Python
# Function to add two numbers and return the result
def addNumbers(l1, l2):
i = len(l1) - 1
j = len(l2) - 1
# initial carry is zero
carry = 0
# we will calculate and store the
# resultant sum in reverse order in res
res = []
while i >= 0 or j >= 0 or carry > 0:
total = carry
if i >= 0:
total += l1[i]
if j >= 0:
total += l2[j]
res.append(str(total % 10))
carry = total // 10
i -= 1
j -= 1
# remove leading zeroes which are currently
# at the back due to reversed string res
while len(res) > 0 and res[-1] == '0':
res.pop()
# reverse our final result
res = res[::-1]
return ''.join(res)
# Function to find and return minimum sum of
# two numbers formed from digits of the array.
def minSum(arr):
arr.sort()
# Two Lists for storing the two minimum numbers
l1 = []
l2 = []
for i in range(len(arr)):
if i % 2 == 0:
l1.append(arr[i])
else:
l2.append(arr[i])
return addNumbers(l1, l2)
if __name__ == '__main__':
arr = [6, 8, 4, 5, 2, 3, 0]
print(minSum(arr))
C#
using System;
class GfG{
// Function to add two numeric strings and return the result as a string
static string addString(string s1, string s2){
int i = s1.Length - 1;
int j = s2.Length - 1;
int carry = 0;
string res = "";
// Loop until both strings are processed and no carry remains
while (i >= 0 || j >= 0 || carry > 0){
int sum = carry;
// Add current digit from s1 if any
if (i >= 0) sum += s1[i--] - '0';
// Add current digit from s2 if any
if (j >= 0) sum += s2[j--] - '0';
// Add least significant digit of sum to result
res = (sum % 10) + res;
carry = sum / 10;
}
// Remove any leading zeros (except when result is "0")
int k = 0;
while (k < res.Length - 1 && res[k] == '0') k++;
return res.Substring(k);
}
// Function to find and return minimum possible sum of two numbers
// formed by using all digits in the array
static string minSum(int[] arr){
// Sort the digits in ascending order
Array.Sort(arr);
string s1 = "";
string s2 = "";
// Distribute digits alternately between s1 and s2
for (int i = 0; i < arr.Length; i++)
{
if (i % 2 == 0)
s1 += arr[i];
else
s2 += arr[i];
}
// Add the two numbers and return their sum as a string
return addString(s1, s2);
}
static void Main(string[] args)
{
int[] arr = { 6, 8, 4, 5, 2, 3, 0 };
Console.WriteLine(minSum(arr));
}
}
JavaScript
// Function to add two lists and return the result
function addNumbers(l1, l2) {
let i = l1.length - 1;
let j = l2.length - 1;
// initial carry is zero
let carry = 0;
// we will calculate and store the
// resultant sum in reverse order in res
let res = [];
while (i >= 0 || j >= 0 || carry > 0) {
let sum = carry;
if (i >= 0)
sum += l1[i] - '0';
if (j >= 0)
sum += l2[j] - '0';
res.push(sum % 10);
carry = Math.floor(sum / 10);
i--;
j--;
}
// remove leading zeroes which are currently
// at the back due to reversed list res
while (res.length > 0 && res[res.length - 1] === 0)
res.pop();
// reverse our final list and convert it to a string
return res.reverse().join('');
}
// Function to find and return minimum sum of
// two numbers formed from digits of the array.
function minSum(arr) {
arr.sort((a, b) => a - b);
// Two lists for storing the two minimum numbers
let l1 = [], l2 = [];
for (let i = 0; i < arr.length; i++) {
if (i % 2 === 0)
l1.push(arr[i]);
else
l2.push(arr[i]);
}
return addNumbers(l1, l2);
}
// Driver Code
let arr = [6, 8, 4, 5, 2, 3, 0];
console.log(minSum(arr));
[Expected Approach] Using Count Array - O(n) time and O(n) space
The idea is to use a count array to track the frequency of each digit in the input array, eliminating the need for sorting. By iterating through the count array from 0 to 9 and alternately distributing digits to two strings while avoiding leading zeros, we can efficiently create the two smallest numbers that can be formed using all the input digits.
C++
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// Function to add two strings and return the result
string addString(string &s1, string &s2) {
int i = s1.length() - 1;
int j = s2.length() - 1;
// initial carry is zero
int carry = 0;
// we will calculate and store the
// resultant sum in reverse order in res
string res = "";
while (i >= 0 || j >= 0 || carry > 0) {
int sum = carry;
if (i >= 0)
sum += (s1[i] - '0');
if (j >= 0)
sum += (s2[j] - '0');
res.push_back(sum % 10 + '0');
carry = sum / 10;
i--;
j--;
}
// remove leading zeroes which are currently
// at the back due to reversed string res
while (!res.empty() && res.back() == '0')
res.pop_back();
// reverse our final string
reverse(res.begin(), res.end());
return res;
}
// Function to find minimum sum using count array approach
string minSum(vector<int> &arr) {
// Count array to store frequency of each digit
vector<int> count(10, 0);
// Count occurrences of each digit
for (int num : arr) {
count[num]++;
}
// Two strings for storing the two minimum numbers
string s1 = "", s2 = "";
// Flag to alternate between s1 and s2
bool firstNum = true;
// Traverse count array from 0 to 9
for (int digit = 0; digit < 10; digit++) {
// Add digit to appropriate string
// while it has occurrences
while (count[digit]--) {
if (firstNum) {
// Avoid leading zeros for both numbers
if (!(s1.empty() && digit == 0))
s1.push_back(digit + '0');
firstNum = false;
} else {
if (!(s2.empty() && digit == 0))
s2.push_back(digit + '0');
firstNum = true;
}
}
}
// Handle case where s1 or s2 might be empty
if (s1.empty()) s1 = "0";
if (s2.empty()) s2 = "0";
return addString(s1, s2);
}
int main() {
vector<int> arr = {6, 8, 4, 5, 2, 3, 0};
cout << minSum(arr) << endl;
return 0;
}
Java
import java.io.IOException;
class GfG {
// Function to add two strings and return the result
static String addString(String s1, String s2) {
int i = s1.length() - 1;
int j = s2.length() - 1;
// initial carry is zero
int carry = 0;
// we will calculate and store the
// resultant sum in reverse order in res
StringBuilder res = new StringBuilder();
while (i >= 0 || j >= 0 || carry > 0) {
int sum = carry;
if (i >= 0)
sum += (s1.charAt(i) - '0');
if (j >= 0)
sum += (s2.charAt(j) - '0');
res.append(sum % 10);
carry = sum / 10;
i--;
j--;
}
// remove leading zeroes which are currently
// at the back due to reversed string res
while (res.length() > 0 && res.charAt(res.length() - 1) == '0')
res.deleteCharAt(res.length() - 1);
// reverse our final string
res.reverse();
return res.length() == 0 ? "0" : res.toString();
}
// Function to find minimum sum using count array approach
static String minSum(int[] arr) {
// Count array to store frequency of each digit
int[] count = new int[10];
// Count occurrences of each digit
for (int num : arr) {
count[num]++;
}
// Two strings for storing the two minimum numbers
StringBuilder s1 = new StringBuilder();
StringBuilder s2 = new StringBuilder();
// Flag to alternate between s1 and s2
boolean firstNum = true;
// Traverse count array from 0 to 9
for (int digit = 0; digit < 10; digit++) {
// Add digit to appropriate string
// while it has occurrences
while (count[digit]-- > 0) {
if (firstNum) {
// Avoid leading zeros for both numbers
if (!(s1.length() == 0 && digit == 0))
s1.append(digit);
firstNum = false;
} else {
if (!(s2.length() == 0 && digit == 0))
s2.append(digit);
firstNum = true;
}
}
}
// Handle case where s1 or s2 might be empty
if (s1.length() == 0) s1.append("0");
if (s2.length() == 0) s2.append("0");
return addString(s1.toString(), s2.toString());
}
public static void main(String[] args) {
int[] arr = {6, 8, 4, 5, 2, 3, 0};
System.out.println(minSum(arr));
}
}
Python
# Function to add two strings and return the result
def addString(s1, s2):
i = len(s1) - 1
j = len(s2) - 1
# initial carry is zero
carry = 0
# we will calculate and store the
# resultant sum in reverse order in res
res = []
while i >= 0 or j >= 0 or carry > 0:
total = carry
if i >= 0:
total += int(s1[i])
if j >= 0:
total += int(s2[j])
res.append(str(total % 10))
carry = total // 10
i -= 1
j -= 1
# remove leading zeroes which are currently
# at the back due to reversed string res
while res and res[-1] == '0':
res.pop()
# reverse our final string
return ''.join(reversed(res)) if res else "0"
# Function to find minimum sum using count array approach
def minSum(arr):
# Count array to store frequency of each digit
count = [0] * 10
# Count occurrences of each digit
for num in arr:
count[num] += 1
# Two strings for storing the two minimum numbers
s1 = []
s2 = []
# Flag to alternate between s1 and s2
firstNum = True
# Traverse count array from 0 to 9
for digit in range(10):
while count[digit] > 0:
if firstNum:
if not (len(s1) == 0 and digit == 0):
s1.append(str(digit))
firstNum = False
else:
if not (len(s2) == 0 and digit == 0):
s2.append(str(digit))
firstNum = True
count[digit] -= 1
# Handle case where s1 or s2 might be empty
if not s1:
s1.append("0")
if not s2:
s2.append("0")
return addString(''.join(s1), ''.join(s2))
if __name__ == "__main__":
arr = [6, 8, 4, 5, 2, 3, 0]
print(minSum(arr))
C#
using System;
using System.Collections.Generic;
class GfG {
// Function to add two strings and return the result
static string addString(string s1, string s2) {
int i = s1.Length - 1;
int j = s2.Length - 1;
// initial carry is zero
int carry = 0;
// we will calculate and store the
// resultant sum in reverse order in res
List<char> res = new List<char>();
while (i >= 0 || j >= 0 || carry > 0) {
int sum = carry;
if (i >= 0)
sum += (s1[i] - '0');
if (j >= 0)
sum += (s2[j] - '0');
res.Add((char)(sum % 10 + '0'));
carry = sum / 10;
i--;
j--;
}
// remove leading zeroes which are currently
// at the back due to reversed string res
while (res.Count > 0 && res[res.Count - 1] == '0')
res.RemoveAt(res.Count - 1);
// reverse our final string
res.Reverse();
return res.Count == 0 ? "0" : new string(res.ToArray());
}
// Function to find minimum sum using count array approach
static string minSum(int[] arr) {
// Count array to store frequency of each digit
int[] count = new int[10];
// Count occurrences of each digit
foreach (int num in arr) {
count[num]++;
}
// Two strings for storing the two minimum numbers
string s1 = "";
string s2 = "";
// Flag to alternate between s1 and s2
bool firstNum = true;
// Traverse count array from 0 to 9
for (int digit = 0; digit < 10; digit++) {
// Add digit to appropriate string
// while it has occurrences
while (count[digit]-- > 0) {
if (firstNum) {
// Avoid leading zeros for both numbers
if (!(s1.Length == 0 && digit == 0))
s1 += digit.ToString();
firstNum = false;
} else {
if (!(s2.Length == 0 && digit == 0))
s2 += digit.ToString();
firstNum = true;
}
}
}
// Handle case where s1 or s2 might be empty
if (s1.Length == 0) s1 = "0";
if (s2.Length == 0) s2 = "0";
return addString(s1, s2);
}
static void Main() {
int[] arr = { 6, 8, 4, 5, 2, 3, 0 };
Console.WriteLine(minSum(arr));
}
}
JavaScript
// Function to add two strings and return the result
function addString(s1, s2) {
let i = s1.length - 1;
let j = s2.length - 1;
// initial carry is zero
let carry = 0;
// we will calculate and store the
// resultant sum in reverse order in res
let res = [];
while (i >= 0 || j >= 0 || carry > 0) {
let total = carry;
if (i >= 0) total += parseInt(s1[i]);
if (j >= 0) total += parseInt(s2[j]);
res.push(total % 10);
carry = Math.floor(total / 10);
i--;
j--;
}
// remove leading zeroes which are currently
// at the back due to reversed string res
while (res.length > 0 && res[res.length - 1] === 0) {
res.pop();
}
// reverse our final string
return res.reverse().join('') || "0";
}
// Function to find minimum sum using count array approach
function minSum(arr) {
// Count array to store frequency of each digit
let count = new Array(10).fill(0);
// Count occurrences of each digit
for (let num of arr) {
count[num]++;
}
// Two strings for storing the two minimum numbers
let s1 = [];
let s2 = [];
// Flag to alternate between s1 and s2
let firstNum = true;
// Traverse count array from 0 to 9
for (let digit = 0; digit < 10; digit++) {
while (count[digit]-- > 0) {
if (firstNum) {
if (!(s1.length === 0 && digit === 0))
s1.push(digit);
firstNum = false;
} else {
if (!(s2.length === 0 && digit === 0))
s2.push(digit);
firstNum = true;
}
}
}
// Handle case where s1 or s2 might be empty
if (s1.length === 0) s1.push(0);
if (s2.length === 0) s2.push(0);
return addString(s1.join(''), s2.join(''));
}
// Driver Code
let arr = [6, 8, 4, 5, 2, 3, 0];
console.log(minSum(arr));
Minimum sum of two numbers formed from digits of an array
Visit Course
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem