Dada una array arr[] de N enteros no negativos, la tarea es ordenar estos enteros según el producto de sus dígitos.
Ejemplos:
Entrada: arr[] = {12, 10, 102, 31, 15}
Salida: 10 102 12 31 15
10 -> 1 * 0 = 0
102 -> 1 * 0 * 2 = 0
12 -> 1 * 2 = 2
31 -> 3 * 1 = 3
15 -> 1 * 5 = 5
Entrada: arr[] = {12, 10}
Salida: 10 12
Enfoque: La idea es almacenar cada elemento con su producto de dígitos en un par de vectores y luego ordenar todos los elementos del vector de acuerdo con los productos de dígitos almacenados. Finalmente, imprima los elementos en orden.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the product // of the digits of n int productOfDigit(int n) { int product = 1; while (n > 0) { product *= n % 10; n = n / 10; } return product; } // Function to sort the array according to // the product of the digits of elements void sortArr(int arr[], int n) { // Vector to store the digit product // with respective elements vector<pair<int, int> > vp; // Inserting digit product with elements // in the vector pair for (int i = 0; i < n; i++) { vp.push_back(make_pair(productOfDigit(arr[i]), arr[i])); } // Sort the vector, this will sort the pair // according to the product of the digits sort(vp.begin(), vp.end()); // Print the sorted vector content for (int i = 0; i < vp.size(); i++) cout << vp[i].second << " "; } // Driver code int main() { int arr[] = { 12, 10, 102, 31, 15 }; int n = sizeof(arr) / sizeof(arr[0]); sortArr(arr, n); return 0; }
Python3
# Python3 implementation of the approach # Function to return the product # of the digits of n def productOfDigit(n) : product = 1; while (n > 0) : product *= (n % 10); n = n // 10; return product; # Function to sort the array according to # the product of the digits of elements def sortArr(arr, n) : # Vector to store the digit product # with respective elements vp = []; # Inserting digit product with elements # in the vector pair for i in range(n) : vp.append((productOfDigit(arr[i]), arr[i])); # Sort the vector, this will sort the pair # according to the product of the digits vp.sort(); # Print the sorted vector content for i in range(len(vp)) : print(vp[i][1], end = " "); # Driver code if __name__ == "__main__" : arr = [ 12, 10, 102, 31, 15 ]; n = len(arr); sortArr(arr, n); # This code is contributed by AnkitRai01
Javascript
<script> // Javascript implementation of the // above approach // Function to return the product // of the digits of n function productOfDigit(n) { var product = 1; while (n > 0) { product *= n % 10; n = Math.floor(n / 10); } return product; } // Function to sort the array according to // the product of the digits of elements function sortArr(arr, n) { // Vector to store the digit product // with respective elements var vp = new Array(n); // Loop to create 2D array using 1D array for (var i = 0; i < vp.length; i++) { vp[i] = []; } // Inserting digit product with elements // in the vector pair for (var i = 0; i < n; i++) { vp[i].push(productOfDigit(arr[i])); vp[i].push(arr[i]); } // Sort the vector, this will sort the pair // according to the product of the digits vp.sort(); // Print the sorted vector content for (var i = 0; i < n; i++) document.write(vp[i][1] + " "); } // Driver code var arr = [ 12, 10, 102, 31, 15]; var n = arr.length; sortArr(arr, n); // This code is contributed by ShubhamSingh10 </script>
Producción:
10 102 12 31 15
Complejidad de tiempo: O (nlogn)
Espacio Auxiliar: O(n)