Ordenar números dados para formar el número más pequeño

Dada una array arr[] de elementos enteros, la tarea es ordenarlos de tal manera que estos números formen el número más pequeño posible. 
Por ejemplo, si el arreglo dado es {5, 6, 2, 9, 21, 1} entonces el arreglo será 1212569.

Ejemplos: 

Entrada: arr[] = {5, 6, 2, 9, 21, 1} 
Salida: 1212569

Entrada: arr[] = {1, 2, 1, 12, 33, 211, 50} 
Salida: 111221123350  

Enfoque: si todos los números dados tienen como máximo un dígito, entonces el enfoque simple es ordenar todos los números en orden ascendente. Pero si hay algún número que tiene más de un dígito, este enfoque no funcionará. 
Por lo tanto, tenemos que ordenar la array comparando dos elementos de la siguiente manera: 
si los elementos son A y B , entonces compare (A + B) con (B + A) donde + representa la concatenación .
A continuación se muestra la implementación del enfoque anterior: 

C++

// C++ implementation of the approach
#include <algorithm>
#include <iostream>
using namespace std;
 
// Utility function to print
// the contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i];
}
 
// A comparison function that return true
// if 'AB' is smaller than 'BA' when
// we concatenate two numbers 'A' and 'B'
// For example, it will return true if
// we pass 12 and 24 as arguments.
// This function will be used by sort() function
bool compare(int num1, int num2)
{
    // to_string function is predefined function
    // to convert a number in string
 
    // Convert first number to string format
    string A = to_string(num1);
 
    // Convert second number to string format
    string B = to_string(num2);
 
    // Check if 'AB' is smaller or 'BA'
    // and return bool value since
    // comparison operator '<=' returns
    // true or false
    return (A + B) <= (B + A);
}
 
// Function to print the arrangement
// with the smallest value
void printSmallest(int N, int arr[])
{
    // If we pass the name of the comparison
    // function it will sort the array
    // according to the compare function
    sort(arr, arr + N, compare);
 
    // Print the sorted array
    printArr(arr, N);
}
 
// Driver code
int main()
{
    int arr[] = { 5, 6, 2, 9, 21, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
    printSmallest(N, arr);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
 
    // Utility function to print
    // the contents of an array
    public static void printArr(int[] arr, int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i]);
    }
 
    // A comparison function that return negative
    // if 'AB' is smaller than 'BA' when
    // we concatenate two numbers 'A' and 'B'
    // For example, it will return negative value if
    // we pass 12 and 24 as arguments.
    // This function will be used during sort
    public static int compare(int num1, int num2)
    {
 
        // toString function is predefined function
        // to convert a number in string
 
        // Convert first number to string format
        String A = Integer.toString(num1);
 
        // Convert second number to string format
        String B = Integer.toString(num2);
         
        // Check if 'AB' is smaller or 'BA'
        // and return integer value
        return (A+B).compareTo(B+A);
    }
 
    // Function to print the arrangement
    // with the smallest value
    public static void printSmallest(int N, int[] arr)
    {
 
        // Sort using compare function which
        // is defined above
        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;
                }
            }
        }
 
        // Print the sorted array
        printArr(arr, N);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 5, 6, 2, 9, 21, 1 };
        int N = arr.length;
        printSmallest(N, arr);
    }
}
 
// This code is contributed by
// sanjeev2552

Python3

# Python3 implementation of the approach
 
# Utility function to print
# the contents of an array
def printArr(arr, n):
 
    for i in range(0, n):
        print(arr[i], end = "")
 
# A comparison function that return true
# if 'AB' is smaller than 'BA' when
# we concatenate two numbers 'A' and 'B'
# For example, it will return true if
# we pass 12 and 24 as arguments.
# This function will be used by sort() function
def compare(num1, num2):
 
    # Convert first number to string format
    A = str(num1)
 
    # Convert second number to string format
    B = str(num2)
 
    # Check if 'AB' is smaller or 'BA'
    # and return bool value since
    # comparison operator '<=' returns
    # true or false
    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]
 
# Function to print the arrangement
# with the smallest value
def printSmallest(N, arr):
 
    # If we pass the name of the comparison
    # function it will sort the array
    # according to the compare function
    sort(arr)
 
    # Print the sorted array
    printArr(arr, N)
 
# Driver code
if __name__ == "__main__":
 
    arr = [5, 6, 2, 9, 21, 1]
    N = len(arr)
    printSmallest(N, arr)
 
# This code is contributed by Rituraj Jain

C#

// C# implementation for above approach
using System;
 
class GFG
{
 
    // Utility function to print
    // the contents of an array
    public static void printArr(int[] arr, int n)
    {
        for (int i = 0; i < n; i++)
            Console.Write(arr[i]);
    }
 
    // A comparison function that return negative
    // if 'AB' is smaller than 'BA' when
    // we concatenate two numbers 'A' and 'B'
    // For example, it will return negative value if
    // we pass 12 and 24 as arguments.
    // This function will be used during sort
    public static int compare(int num1, int num2)
    {
 
        // toString function is predefined function
        // to convert a number in string
 
        // Convert first number to string format
        String A = num1.ToString();
 
        // Convert second number to string format
        String B = num2.ToString();
         
        // Check if 'AB' is smaller or 'BA'
        // and return integer value
        return (A+B).CompareTo(B+A);
    }
 
    // Function to print the arrangement
    // with the smallest value
    public static void printSmallest(int N, int[] arr)
    {
 
        // Sort using compare function which
        // is defined above
        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;
                }
            }
        }
 
        // Print the sorted array
        printArr(arr, N);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int[] arr = { 5, 6, 2, 9, 21, 1 };
        int N = arr.Length;
        printSmallest(N, arr);
    }
}
 
// This code is contributed by Rajput-Ji

PHP

<?php
// PHP implementation of the approach
 
// Utility function to print
// the contents of an array
function printArr($arr, $n)
{
    for ($i = 0; $i < $n; $i++)
        echo $arr[$i];
}
 
// A comparison function that return true
// if 'AB' is smaller than 'BA' when
// we concatenate two numbers 'A' and 'B'
// For example, it will return true if
// we pass 12 and 24 as arguments.
// This function will be used by sort() function
function compare($num1, $num2)
{
    // to_string function is predefined function
    // to convert a number in string
 
    // Convert first number to string format
    $A = (string)$num1 ;
 
    // Convert second number to string format
    $B = (string)$num2 ;
 
    // Check if 'AB' is smaller or 'BA'
    // and return bool value since
    // comparison operator '<=' returns
    // true or false
    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 to print the arrangement
// with the smallest value
function printSmallest($N, $arr)
{
    // If we pass the name of the comparison
    // function it will sort the array
    // according to the compare function
    $arr = sort_arr($arr);
 
    // Print the sorted array
    printArr($arr, $N);
}
 
    // Driver code
    $arr = array(5, 6, 2, 9, 21, 1 );
    $N = count($arr);
    printSmallest($N, $arr);
 
    // This code is contributed by Ryuga
 
?>

Javascript

<script>
    // Javascript implementation of the approach
 
    // Utility function to print
    // the contents of an array
    function printArr(arr,n)
    {
        for (let i = 0; i < n; i++)
            document.write(arr[i]);
    }
 
    // A comparison function that return true
    // if 'AB' is smaller than 'BA' when
    // we concatenate two numbers 'A' and 'B'
    // For example, it will return true if
    // we pass 12 and 24 as arguments.
    // This function will be used by sort() function
    function compare(num1,num2)
    {
        // to_string function is predefined function
        // to convert a number in string
 
        // Convert first number to string format
        let A = num1.toString();
 
        // Convert second number to string format
        let B = num2.toString();
 
        // Check if 'AB' is smaller or 'BA'
        // and return bool value since
        // comparison operator '<=' returns
        // true or false
        return (A + B).localeCompare(B + A);
    }
 
    // Function to print the arrangement
    // with the smallest value
    function printSmallest(N,arr)
    {
        // If we pass the name of the comparison
        // function it will sort the array
        // according to the compare function
        // Sort using compare function which
        // is defined above
        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;
                }
            }
        }
 
        // Print the sorted array
        printArr(arr,N);
    }
 
    // Driver code
 
    let arr = [ 5, 6, 2, 9, 21, 1 ];
    let N = arr.length;
    printSmallest(N,arr);
</script>
Producción: 

1212569

 

Complejidad de tiempo: O (nlogn)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Vivek.Pandit y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *