Sort an array in increasing order of their Multiplicative Persistence | Java program for the above approach ; Function to find the number of steps required to reduce a given number to a single - digit number ; Stores the required result ; Iterate until a single digit number is not obtained ; Store the number in a temporary variable ; Iterate over all digits and store their product in num ; Increment the answer by 1 ; Return the result ; Count number of steps required to reduce X and Y into single digits integer ; Return true ; Function to sort the array according to the number of steps required to reduce a given number into a single digit number ; Sort the array using the comparator function ; Print the array after sorting ; Driver code ; Function Call
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 ; } int x1 = countReduction ( x ) ; int y1 = countReduction ( y ) ; if ( x1 < y1 ) return - 1 ; return 1 ; } } ) ; static void sortArray ( Integer a [ ] , int n ) { Arrays . sort ( a , new Comparator < Integer > ( ) { public int compare ( Integer x , Integer y ) { 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 ) ; } }