@@ -0,0 +1,1588 @@
+//https://www.hackerrank.com/challenges/largest-permutation%0Aimport java.io.*;%0A%0Apublic class Solution %7B%0A  public static void main(String%5B%5D args) throws IOException %7B%0A    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));%0A%0A    //Get input%0A    String%5B%5D line = br.readLine().split(%22 %22);%0A    final int N = Integer.parseInt(line%5B0%5D);%0A    final int K = Integer.parseInt(line%5B1%5D);%0A    final int%5B%5D A = new int%5BN%5D;%0A    line = br.readLine().split(%22 %22);%0A    for(int i = 0; i %3C N; ++i)%7B%0A      A%5Bi%5D = Integer.parseInt(line%5Bi%5D);%0A    %7D%0A    line = null;%0A%0A    //Create an array to show where each number currently is%0A    final int%5B%5D pos = new int%5BN+1%5D;%0A    for(int i = 0; i %3C N; ++i)%7B%0A      pos%5BA%5Bi%5D%5D = i;%0A    %7D%0A    %0A    //Make at most K swaps to sort list in reverse chronological order:%0A    //  For each natural number 'i' from N to 1...%0A    for(int i = N, swaps = K; i %3E 0; --i)%7B%0A      %0A      //Get the current position%0A      final int actualPos = pos%5Bi%5D;%0A      %0A      //Get the expected position when sorted in reverse%0A      final int expectedPos = N - i;%0A      %0A      //If 'i' is not in the correct place...%0A      if(actualPos != expectedPos)%7B%0A        %0A        //Swap whatever's in i's place with i%0A        pos%5BA%5BexpectedPos%5D%5D = actualPos;%0A        A%5BactualPos%5D = A%5BexpectedPos%5D;%0A        A%5BexpectedPos%5D = i;%0A        %0A        //Stop sorting if we're out of swaps%0A        if(--swaps %3C 1)%7B%0A          break;%0A        %7D%0A      %7D%0A    %7D%0A    %0A    //Print output%0A    StringBuffer sb = new StringBuffer();%0A    for(int i = 0; i %3C N; sb.append(A%5Bi++%5D + %22 %22))%7B%7D%0A    System.out.print(sb);%0A  %7D%0A%7D%0A

Create largest-permutation.java