class max_perm { // function for computing number of digits in n public static int Num_of_Digits(int n) { int digits = 0; while(n>0) { digits = digits + 1; n = n/10; } return digits; } // function for computing 10^i. public static int Power_of_Ten(int i) { int power = 1; while(i>0) { power = power*10; i = i-1; } return power; } // function for computing the digit of n at 'index' public static int Value_of_Digit_of_n_at_Index(int n, int index) { int power = Power_of_Ten(index-1); n = n/power; return(n%10); } // function for computing the index of MAX Digit in n public static int Max_Digit_Index(int n, int digits) { int max_digit = n%10; int max_digit_index=1; n = n/10; for(int i=2; i<=digits; i = i+1) { int current_digit = n%10; if(current_digit>max_digit) { max_digit = current_digit; max_digit_index = i; } n = n/10; } //System.out.println("Max digit index is "+max_digit_index); // System.out.println("Max digit is "+max_digit); return max_digit_index; } // function for getting the permutation with max digit at MSP public static int Permutation_with_Max_Digit_at_MSI(int n, int digits) { int power = Power_of_Ten(digits-1); int digit_at_MSI = n/power; int max_digit_index = Max_Digit_Index(n,digits); int value_of_max_digit = Value_of_Digit_of_n_at_Index(n,max_digit_index); int new_n=n;; if(max_digit_index != digits) { int difference_of_digits = value_of_max_digit - digit_at_MSI; int lower_power = Power_of_Ten(max_digit_index-1); new_n = n + (power-lower_power)*difference_of_digits; } return new_n; } // function for getting the maximum permutation for integer n public static int Max_Permutation(int n) { int upper_n; int lower_n; int digits = Num_of_Digits(n); int power= Power_of_Ten(digits); for(int i = digits; i>1; i =i-1) { upper_n= n/power; lower_n= n%power; lower_n= Permutation_with_Max_Digit_at_MSI(lower_n,i); n = upper_n*power+ lower_n; power = power/10; } return n; } public static void main(String args[]) { int n = Integer.parseInt(args[0]); System.out.println("The maximum permutation of "+n+" is : "); System.out.println(Max_Permutation(n)); } }