Given an array arr[] of N non-negative integers, the task is to sort these integers according to the product of their digits.
Examples:
Input: arr[] = {12, 10, 102, 31, 15}
Output: 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
Input: arr[] = {12, 10}
Output: 10 12
Approach: The idea is to store each element with its product of digits in a vector pair and then sort all the elements of the vector according to the digit products stored. Finally, print the elements in order.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int productOfDigit( int n)
{
int product = 1;
while (n > 0) {
product *= n % 10;
n = n / 10;
}
return product;
}
void sortArr( int arr[], int n)
{
vector<pair< int , int > > vp;
for ( int i = 0; i < n; i++) {
vp.push_back(make_pair(productOfDigit(arr[i]), arr[i]));
}
sort(vp.begin(), vp.end());
for ( int i = 0; i < vp.size(); i++)
cout << vp[i].second << " " ;
}
int main()
{
int arr[] = { 12, 10, 102, 31, 15 };
int n = sizeof (arr) / sizeof (arr[0]);
sortArr(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int productOfDigit( int n)
{
int product = 1 ;
while (n > 0 ) {
product *= n % 10 ;
n /= 10 ;
}
return product;
}
static void sortArr( int [] arr, int n)
{
ArrayList<ArrayList<Integer> > vp
= new ArrayList<ArrayList<Integer> >();
for ( int i = 0 ; i < n; i++) {
vp.add( new ArrayList<Integer>());
}
for ( int i = 0 ; i < n; i++) {
ArrayList<Integer> l1 = vp.get(i);
l1.add(productOfDigit(arr[i]));
l1.add(arr[i]);
vp.set(i, l1);
}
Collections.sort(
vp, new Comparator<ArrayList<Integer> >() {
public int compare(ArrayList<Integer> o1,
ArrayList<Integer> o2)
{
if (o1.get( 0 ) != o2.get( 0 ))
return o1.get( 0 ).compareTo(
o2.get( 0 ));
return o1.get( 1 ).compareTo(o2.get( 1 ));
}
});
for ( int i = 0 ; i < n; i++)
System.out.print(vp.get(i).get( 1 ) + " " );
}
public static void main(String[] args)
{
int [] arr = { 12 , 10 , 102 , 31 , 15 };
int n = arr.length;
sortArr(arr, n);
}
}
|
Python3
def productOfDigit(n) :
product = 1 ;
while (n > 0 ) :
product * = (n % 10 );
n = n / / 10 ;
return product;
def sortArr(arr, n) :
vp = [];
for i in range (n) :
vp.append((productOfDigit(arr[i]), arr[i]));
vp.sort();
for i in range ( len (vp)) :
print (vp[i][ 1 ], end = " " );
if __name__ = = "__main__" :
arr = [ 12 , 10 , 102 , 31 , 15 ];
n = len (arr);
sortArr(arr, n);
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
static int productOfDigit( int n)
{
int product = 1;
while (n > 0) {
product *= n % 10;
n /= 10;
}
return product;
}
static void sortArr( int [] arr, int n)
{
List<List< int > > vp = new List<List< int > >();
for ( var i = 0; i < n; i++) {
vp.Add( new List< int >());
}
for ( var i = 0; i < n; i++) {
vp[i].Add(productOfDigit(arr[i]));
vp[i].Add(arr[i]);
}
vp = vp.OrderBy(x => x[0])
.ThenBy(x => x[1])
.ToList();
for ( int i = 0; i < n; i++)
Console.Write(vp[i][1] + " " );
}
public static void Main( string [] args)
{
int [] arr = { 12, 10, 102, 31, 15 };
int n = arr.Length;
sortArr(arr, n);
}
}
|
Javascript
<script>
function productOfDigit(n)
{
var product = 1;
while (n > 0) {
product *= n % 10;
n = Math.floor(n / 10);
}
return product;
}
function sortArr(arr, n)
{
var vp = new Array(n);
for ( var i = 0; i < vp.length; i++) {
vp[i] = [];
}
for ( var i = 0; i < n; i++) {
vp[i].push(productOfDigit(arr[i]));
vp[i].push(arr[i]);
}
vp.sort();
for ( var i = 0; i < n; i++)
document.write(vp[i][1] + " " );
}
var arr = [ 12, 10, 102, 31, 15];
var n = arr.length;
sortArr(arr, n);
</script>
|
Time Complexity: O(nlogn)
Auxiliary Space: O(n), since n extra space has been taken.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
06 Sep, 2022
Like Article
Save Article