Given an array arr[] of integer elements, the task is to arrange them in such a way that these numbers form the smallest possible number.
For example, if the given array is {5, 6, 2, 9, 21, 1} then the arrangement will be 1212569.
Examples:
Input: arr[] = {5, 6, 2, 9, 21, 1}
Output: 1212569
Input: arr[] = {1, 2, 1, 12, 33, 211, 50}
Output: 111221123350
Approach: If all the given numbers are of at most one digit then the simple approach is sorting all numbers in ascending order. But if there is some number which have more than a single-digit then this approach will not work.
Therefore, we have to sort the array by comparing any two elements in the following way:
If the elements are A and B, then compare (A + B) with (B + A) where + represents concatenation.
Below is the implementation of the above approach:
C++
#include <algorithm>
#include <iostream>
using namespace std;
void printArr( int arr[], int n)
{
for ( int i = 0; i < n; i++)
cout << arr[i];
}
bool compare( int num1, int num2)
{
string A = to_string(num1);
string B = to_string(num2);
return (A + B) <= (B + A);
}
void printSmallest( int N, int arr[])
{
sort(arr, arr + N, compare);
printArr(arr, N);
}
int main()
{
int arr[] = { 5, 6, 2, 9, 21, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
printSmallest(N, arr);
return 0;
}
|
Java
class GFG
{
public static void printArr( int [] arr, int n)
{
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i]);
}
public static int compare( int num1, int num2)
{
String A = Integer.toString(num1);
String B = Integer.toString(num2);
return (A+B).compareTo(B+A);
}
public static void printSmallest( int N, int [] arr)
{
for ( int i = 0 ; i < N; i++)
{
for ( int j = i + 1 ; j < N; j++)
{
if (compare(arr[i], arr[j]) > 0 )
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printArr(arr, N);
}
public static void main(String[] args)
{
int [] arr = { 5 , 6 , 2 , 9 , 21 , 1 };
int N = arr.length;
printSmallest(N, arr);
}
}
|
Python3
def printArr(arr, n):
for i in range ( 0 , n):
print (arr[i], end = "")
def compare(num1, num2):
A = str (num1)
B = str (num2)
return int (A + B) < = int (B + A)
def sort(arr):
for i in range ( len (arr)):
for j in range (i + 1 , len (arr)):
if compare(arr[i], arr[j]) = = False :
arr[i], arr[j] = arr[j], arr[i]
def printSmallest(N, arr):
sort(arr)
printArr(arr, N)
if __name__ = = "__main__" :
arr = [ 5 , 6 , 2 , 9 , 21 , 1 ]
N = len (arr)
printSmallest(N, arr)
|
C#
using System;
class GFG
{
public static void printArr( int [] arr, int n)
{
for ( int i = 0; i < n; i++)
Console.Write(arr[i]);
}
public static int compare( int num1, int num2)
{
String A = num1.ToString();
String B = num2.ToString();
return (A+B).CompareTo(B+A);
}
public static void printSmallest( int N, int [] arr)
{
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
if (compare(arr[i], arr[j]) > 0)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printArr(arr, N);
}
public static void Main(String[] args)
{
int [] arr = { 5, 6, 2, 9, 21, 1 };
int N = arr.Length;
printSmallest(N, arr);
}
}
|
PHP
<?php
function printArr( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
echo $arr [ $i ];
}
function compare( $num1 , $num2 )
{
$A = (string) $num1 ;
$B = (string) $num2 ;
if ((int)( $A . $B ) <= (int)( $B . $A ))
{
return true;
}
else
return false;
}
function sort_arr( $arr )
{
for ( $i = 0; $i < count ( $arr ) ; $i ++)
{
for ( $j = $i + 1; $j < count ( $arr ) ; $j ++)
{
if (compare( $arr [ $i ], $arr [ $j ]) == false)
{
$temp = $arr [ $i ] ;
$arr [ $i ] = $arr [ $j ] ;
$arr [ $j ] = $temp ;
}
}
}
return $arr ;
}
function printSmallest( $N , $arr )
{
$arr = sort_arr( $arr );
printArr( $arr , $N );
}
$arr = array (5, 6, 2, 9, 21, 1 );
$N = count ( $arr );
printSmallest( $N , $arr );
?>
|
Javascript
<script>
function printArr(arr,n)
{
for (let i = 0; i < n; i++)
document.write(arr[i]);
}
function compare(num1,num2)
{
let A = num1.toString();
let B = num2.toString();
return (A + B).localeCompare(B + A);
}
function printSmallest(N,arr)
{
for (let i = 0; i < N; i++)
{
for (let j = i + 1; j < N; j++)
{
if (compare(arr[i], arr[j]) > 0)
{
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printArr(arr,N);
}
let arr = [ 5, 6, 2, 9, 21, 1 ];
let N = arr.length;
printSmallest(N,arr);
</script>
|
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Approach 2: Implementing a custom quicksort algorithm
Another way to solve the problem is to implement a custom quicksort algorithm that uses the same comparison function as in approach 1. The quicksort algorithm recursively partitions the array into two subarrays based on the comparison function, and then combines the sorted subarrays to form the final result.
C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool compare(string a, string b) {
return (a+b) < (b+a);
}
int partition(vector<string>& arr, int low, int high) {
string pivot = arr[high];
int i = low - 1;
for ( int j = low; j <= high - 1; j++) {
if (compare(arr[j], pivot)) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i+1], arr[high]);
return i+1;
}
void quicksort(vector<string>& arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}
string smallestNumber(vector< int >& nums) {
vector<string> numStrs;
for ( int num : nums) {
numStrs.push_back(to_string(num));
}
quicksort(numStrs, 0, numStrs.size()-1);
string result = "" ;
for (string numStr : numStrs) {
result += numStr;
}
return result;
}
int main() {
vector< int > nums = { 5, 6, 2, 9, 21, 1 };
cout << smallestNumber(nums) << endl;
return 0;
}
|
Time Complexity: O(nlogn)
Auxiliary Space: O(n)