@@ -0,0 +1,1398 @@
+import java.io.*;%0Aimport java.util.*;%0Aimport java.text.*;%0Aimport java.math.*;%0Aimport java.util.regex.*;%0A%0Apublic class Solution %7B%0A%0A  /**%0A   * The gotcha of this problem if figuring out how to swap in O(1) time and how to maintain index%0A   * after swapping. We know that permutation is largest when first element is n, second is n - 1,%0A   * etc. Once we update element at 0 to be 'n' we need to swap elements. For this purpose we%0A   * maintain index 'tmp' where each element maps to an original index. After each permutation%0A   * swap, we need to update 'tmp' indices.%0A   */%0A  private static void largestPermutation(int%5B%5D arr, int k) %7B%0A    int%5B%5D tmp = new int%5Barr.length + 1%5D;%0A    for (int i = 0; i %3C arr.length; i++) %7B%0A      tmp%5Barr%5Bi%5D%5D = i;%0A    %7D%0A    int n = arr.length;%0A    for (int i = 0; i %3C arr.length; i++) %7B%0A      if (arr%5Bi%5D == n) %7B%0A        n--;%0A      %7D else if (arr%5Bi%5D %3C n && k %3E 0) %7B%0A        arr%5Btmp%5Bn%5D%5D = arr%5Bi%5D;%0A        tmp%5Barr%5Bi%5D%5D = tmp%5Bn%5D;%0A        arr%5Bi%5D = n;%0A        tmp%5Bn%5D = i;%0A        n--;%0A        k--;%0A      %7D%0A    %7D%0A  %7D%0A%0A  public static void main(String%5B%5D args) %7B%0A    Scanner in = new Scanner(System.in);%0A    int n = in.nextInt();%0A    int k = in.nextInt();%0A    int%5B%5D arr = new int%5Bn%5D;%0A    for (int i = 0; i %3C n; i++) %7B%0A      arr%5Bi%5D = in.nextInt();%0A    %7D%0A%0A    largestPermutation(arr, k);%0A    for (int a : arr) %7B%0A      System.out.print(a + %22 %22);%0A    %7D%0A    System.out.println();%0A  %7D%0A%7D%0A

add solution for largest-permutation