Sort an array in increasing order of their Multiplicative Persistence
Last Updated :
20 Jan, 2023
Given an array arr[] consisting of N positive integers, the task is to sort the array in increasing order with respect to the count of steps required to obtain a single-digit number by multiplying its digits recursively for each array element. If any two numbers have the same count of steps, then print the smaller number first.
Examples:
Input: arr[] = {39, 999, 4, 9876}
Output: 4 9876 39 999
Explanation:
Following are the number of steps required to reduce every array element to 0:
- For arr[0] (= 39): The element 39 will reduce as 39 ? 27 ? 14 ? 4. Therefore, the number of steps required is 3.
- For arr[1] (= 999): The element 999 will reduce as 999 ? 729 ? 126 ? 12 ? 2. Therefore, the number of steps required is 4.
- For arr[2] (= 4): The element 4 is already a single-digit number. Therefore, the number of steps required is 0.
- For arr[3] (= 9876): The element 9876 will reduce as 9876 ? 3024 ? 0. Therefore, the number of steps required is 2.
According to the given criteria the elements in increasing order of count of steps required to reduce them into single-digit number is {4, 9876, 29, 999}
Input: arr[] = {1, 27, 90}
Output: 1 90 27
Approach: The given problem can be solved by finding the count of steps required to obtain a single-digit number by multiplying its digits recursively for each array element and then sort the array in increasing order using the comparator function. Follow the steps to solve the problem:
- Declare a comparator function, cmp(X, Y) that takes two elements as a parameter and perform the following steps:
- Iterate a loop until X becomes a single-digit number and update the value of X to the product of its digit.
- Repeat the above step for the value Y.
- If the value of X is less than Y, then return true. Otherwise, return false.
- Sort the given array arr[] by using the above comparator function as sort(arr, arr + N, cmp).
- After completing the above steps, print the array arr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countReduction( int num)
{
int ans = 0;
while (num >= 10) {
int temp = num;
num = 1;
while (temp > 0) {
int digit = temp % 10;
temp = temp / 10;
num *= digit;
}
ans++;
}
return ans;
}
bool compare( int x, int y)
{
int x1 = countReduction(x);
int y1 = countReduction(y);
if (x1 < y1)
return true ;
return false ;
}
void sortArray( int a[], int n)
{
sort(a, a + n, compare);
for ( int i = 0; i < n; i++) {
cout << a[i] << " " ;
}
}
int main()
{
int arr[] = { 39, 999, 4, 9876 };
int N = sizeof (arr) / sizeof (arr[0]);
sortArray(arr, N);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static int countReduction( int num)
{
int ans = 0 ;
while (num >= 10 )
{
int temp = num;
num = 1 ;
while (temp > 0 ) {
int digit = temp % 10 ;
temp = temp / 10 ;
num *= digit;
}
ans++;
}
return ans;
}
static void sortArray(Integer a[], int n)
{
Arrays.sort(a, new Comparator<Integer>(){
public int compare(Integer x, Integer y)
{
int x1 = countReduction(x);
int y1 = countReduction(y);
if (x1 < y1)
return - 1 ;
return 1 ;
}
});
for ( int i = 0 ; i < n; i++)
{
System.out.print(a[i] + " " );
}
}
public static void main (String[] args)
{
Integer arr[] = { 39 , 999 , 4 , 9876 };
int N = arr.length;
sortArray(arr, N);
}
}
|
Python3
import functools
def CountReduction(num):
ans = 0
while num > = 10 :
temp = num
num = 1
while temp > 0 :
digit = temp % 10
temp = temp / / 10
num * = digit
ans + = 1
return ans
def SortArray(a):
a.sort(key = CountReduction)
print (a)
arr = [ 39 , 999 , 4 , 9876 ]
SortArray(arr)
|
C#
using System;
using System.Linq;
class Program
{
static int CountReduction( int num)
{
int ans = 0;
while (num >= 10)
{
int temp = num;
num = 1;
while (temp > 0)
{
int digit = temp % 10;
temp = temp / 10;
num *= digit;
}
ans++;
}
return ans;
}
static void SortArray( int [] a, int n)
{
Array.Sort(a, (x, y) =>
{
int x1 = CountReduction(x);
int y1 = CountReduction(y);
if (x1 < y1)
return -1;
return 1;
});
Console.WriteLine( string .Join( " " , a.Select(x => x.ToString())));
}
static void Main( string [] args)
{
int [] arr = { 39, 999, 4, 9876 };
int N = arr.Length;
SortArray(arr, N);
}
}
|
Javascript
<script>
function countReduction(num) {
let ans = 0;
while (num >= 10) {
let temp = num;
num = 1;
while (temp > 0) {
let digit = temp % 10;
temp = Math.floor(temp / 10);
num *= digit;
}
ans++;
}
return ans;
}
function compare(x, y) {
let x1 = countReduction(x);
let y1 = countReduction(y);
if (x1 < y1)
return -1;
return 1;
}
function sortArray(a, n) {
a.sort(compare);
for (let i = 0; i < n; i++) {
document.write(a[i] + " " );
}
}
let arr = [39, 999, 4, 9876];
let N = arr.length;
sortArray(arr, N);
</script>
|
Time Complexity: O(N * log(N) * log(X)), where X is the largest element in the array, arr[]
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...