What is problem in my code

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();
int arr[]=new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
Unlock(arr,k);
for(int i:arr) {
System.out.print(i+" ");
}
}

private static void Unlock(int[] arr, int k) {
	int kvalue=1;
	HashMap<Integer,Integer> map=new HashMap<>();
	for(int i=0;i<arr.length;i++) {
		map.put(arr[i], i);
	}
	for(int i=arr.length;i>0;i--) {
		int inx=map.get(i);
	int best=(arr.length)-(i);
	map.put(i, best);
	map.put(arr[best],inx);
	if(inx!=best) {
		
		int temp=arr[inx];
		 arr[inx]=arr[best];
		 arr[best]=temp;
		 if(kvalue==k) {
				return;
			}
		  
	}
	}
	
}

}

@sksumitkumardiwaker,

Suggested approach:

  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.

@sksumitkumardiwaker,
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.

I used same approach but test case 3 pass another test case give wrong ans so check my code

@sksumitkumardiwaker,
Corrected code: https://ide.codingblocks.com/s/237162

There was an error in swapping the elements.