Dada una array arr de tamaño N , la tarea es ordenar los dígitos de cada elemento de la array y luego ordenar la array en orden no decreciente. Imprime la array después de ordenar.
Ejemplos:
Entrada: arr[] = {514, 34, 41, 39}
Salida: 41 43 93 541
Explicación:
Ordenar cada elemento de la array: arr[] = {145, 34, 14, 39}
Ordenar la array: arr[] = {14, 34, 39, 145}Entrada: arr[] = {3, 1, 9, 4}
Salida: 1 3 4 9
Enfoque: siga los pasos a continuación para resolver este problema:
- Cree una función toString , que aceptará el número entero N como parámetro y devolverá N en forma de string.
- Pase cada elemento de la array arr en la función toString para convertirlo en una string. Ordene esa string y vuelva a convertirla en un número entero. Después de esto, reemplace cada elemento con el entero convertido en la array arr .
- Ordene la array arr e imprima la respuesta de acuerdo con la observación anterior.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to print array in non-decreasing order void printArray(int arr[], int N) { for (int i = 0; i < N; i++) cout << arr[i] << " "; } // Function to convert an integer to string string toString(int N) { string s; while (N > 0) { s += (N % 10) + '0'; N /= 10; } if (s.size() == 0) { s = "0"; } return s; } // Function to sort each element of the array // in non-descreasing order of its digits void digitSort(int arr[], int N) { // Traversing each element to sort for (int i = 0; i < N; i++) { // Converting number to string string s = toString(arr[i]); // Sorting string sort(s.begin(), s.end()); // Converting string to integer arr[i] = stoi(s); } // Sorting array sort(arr, arr + N); // Printing array printArray(arr, N); } // Driver Code int main() { int arr[] = { 514, 34, 41, 39 }; int N = sizeof(arr) / sizeof(arr[0]); digitSort(arr, N); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to print array in non-decreasing order static void printArray(int arr[], int N) { for (int i = 0; i < N; i++) System.out.print(arr[i] + " "); } // Function to convert an integer to String static String toString(int N) { String s = ""; while (N > 0) { s += String.valueOf((N % 10)) + '0'; N /= 10; } if (s.length() == 0) { s = "0"; } return s; } // Function to sort each element of the array // in non-descreasing order of its digits static void digitSort(int arr[], int N) { // Traversing each element to sort for (int i = 0; i < N; i++) { // Converting number to String String s = toString(arr[i]); // Sorting String s = sort(s); // Converting String to integer arr[i] = Integer.valueOf(s); } // Sorting array Arrays.sort(arr); // Printing array printArray(arr, N); } static String sort(String inputString) { // convert input string to char array char tempArray[] = inputString.toCharArray(); // sort tempArray Arrays.sort(tempArray); // return new sorted string return new String(tempArray); } // Driver Code public static void main(String[] args) { int arr[] = { 514, 34, 41, 39 }; int N = arr.length; digitSort(arr, N); } } // This code is contributed by shikhasingrajput
Python3
# python program for the above approach # Function to print array in non-decreasing order def printArray(arr, N): for i in range(0, N): print(arr[i], end=" ") # Function to convert an integer to string def toString(N): s = "" while (N > 0): s += chr(int(N % 10) + ord('0')) N /= 10 if (len(s) == 0): s = "0" return s # Function to sort each element of the array # in non-descreasing order of its digits def digitSort(arr, N): # Traversing each element to sort for i in range(0, N): # Converting number to string s = toString(arr[i]) # Sorting string s = list(s) s.sort() s = ''.join(s) # Converting string to integer arr[i] = int(s) # Sorting array arr.sort() # Printing array printArray(arr, N) # Driver Code if __name__ == "__main__": arr = [514, 34, 41, 39] N = len(arr) digitSort(arr, N) # This code is contributed by rakeshsahni
C#
// Java program for the above approach using System; using System.Collections; class GFG { // Function to print array in non-decreasing order static void printArray(int []arr, int N) { for (int i = 0; i < N; i++) Console.Write(arr[i] + " "); } // Function to convert an integer to string static String toString(int N) { string s = ""; while (N > 0) { int val = N % 10; s += val.ToString(); N /= 10; } if (s.Length == 0) { s = "0"; } return s; } // Function to sort each element of the array // in non-descreasing order of its digits static void digitSort(int []arr, int N) { int []ans = new int[N]; // Traversing each element to sort for (int i = 0; i < N; i++) { // Converting number to string String s = toString(arr[i]); char []ch = s.ToCharArray(); Array.Sort(ch); ans[i] = Int32.Parse(String.Join("",ch)); // Converting string to integer arr[i] = Int32.Parse(s); } // Sorting array Array.Sort(ans); // Printing array printArray(ans, N); } // Driver Code public static void Main() { int []arr = { 514, 34, 41, 39 }; int N = arr.Length; digitSort(arr, N); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript program for the above approach // Function to print array in // non-decreasing order function printArray(arr, N) { for(let i = 0; i < N; i++) document.write(arr[i] + " "); } // Function to convert an integer to string function toString(N) { let s = ""; while (N > 0) { s = s + (N % 10).toString(); N = Math.floor(N / 10); } if (s.length == 0) { s = "0"; } return s; } // Function to sort each element of the array // in non-descreasing order of its digits function digitSort(arr, N) { // Traversing each element to sort for(let i = 0; i < N; i++) { // Converting number to string let s = arr[i].toString(); // Sorting string st = s.split('') st.sort(function (a, b){ return a.charCodeAt(0) - b.charCodeAt(0); }); s = ''; for(let i = 0; i < st.length; i++) { s = s + st[i]; } // Converting string to integer arr[i] = parseInt(s); } // Sorting array arr.sort(function(a, b){ return a - b }) // Printing array printArray(arr, N); } // Driver Code let arr = [ 514, 34, 41, 39 ]; let N = arr.length; digitSort(arr, N); // This code is contributed by Potta Lokesh </script>
14 34 39 145
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por sushmitamittal1329 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA