package hackerrank;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;

/**
 * https://www.hackerrank.com/challenges/largest-permutation
 */
public class LargestPermutation {
    private static void largestPermutation(int[] a, int k, TreeMap<Integer, Integer> map) {
        int i = 0;
        Iterator<Entry<Integer, Integer>> iter = map.entrySet().iterator();
        while (i < k && i < a.length) {
            Entry<Integer, Integer> entry = iter.next();
            int num = entry.getKey();
            int idx = entry.getValue();
            if (a[i] < num) {
                swap(a, i, idx);
                map.put(a[i], idx);
            }
            i++;
        }
        for (int x = 0; x < a.length; x++) {
            if (x == a.length - 1) {
                System.out.print(a[x]);
            } else {
                System.out.print(a[x] + " ");
            }
        }
        System.out.println();
    }

    private static void swap(int[] a, int i, int j) {
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int[] a = new int[n];
        TreeMap<Integer, Integer> map = new TreeMap<>((x, y) -> Integer.compare(y, x));
        for (int i = 0; i < a.length; i++) {
            int num = in.nextInt();
            a[i] = num;
            map.put(num, i);
        }
        largestPermutation(a, k, map);
    }
}

@@ -608,17 +608,18 @@
 f (a%5Bi%5D 
-%3C
+!=
  num) %7B%0A
@@ -638,17 +638,20 @@
     
-swap(a, i
+map.put(a%5Bi%5D
 , id
@@ -666,36 +666,33 @@
             
-map.put(a%5Bi%5D
+swap(a, i
 , idx);%0A    

Update Largest Permutation