Only one test case passing unlock

import java.util.*;
public class Main {
public static void main(String args[]) {

	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int k = sc.nextInt();
	HashMap<Integer, Integer> vi = new HashMap<>();
	ArrayList<Integer> a = new ArrayList<>();
	for (int i = 0; i < n; i++) {
		a.add(sc.nextInt());
	}
	for (int i = 0; i < n; i++)
    {
      vi.put(a.get(i), i);
    }
	if (k >= n) {
		Collections.sort(a,Collections.reverseOrder());
		for (int x : a) {
			System.out.print(x + " ");
		}
	} else {
		for (int i = n; i > 0; i--) {
			if (k > 0) {
				int ii = vi.get(i);
				int bi = n - i;
				if (ii != bi) {
					vi.put(i, bi);
					int e = a.get(bi);
					vi.put(e, ii);
					int t = a.get(bi);
		            a.set(bi, a.get(ii));
		            a.set(ii, t);
				}

				k--;
			}
		}
		for (int x : a) {
			System.out.print(x + " ");
		}
	}
}
}

your code fails for the sample test case too
The following problem can be easily solved by using hashmap or unordered map. The hint to solve the problem is that the given array will always contain permutation of the first N natural numbers. So we will prepare a map which will contain element as the key and the index at which it is present as the value.

Algorithm

  1. Create the map by filling the number as the key and the index at which it is present as the value
  2. Iterate over the whole array.
  3. For element at a particular index m we will check if it is present at its best spot or not.
  4. The best spot of any number num is the N-num index of the array.
  5. If the number is not present at its best spot then we will swap the number with element present at that spot.
  6. As we have stored the index of every element in the map we can easily retrieve that from the map to get the swap done.
    you can see this: