Used HashMap But Then Also Showing Time Limit Error

//Need Guidance

public static void code(int n, int k, int[] arr){

	HashMap<Integer, Integer> map = new HashMap<>();  // Key = index & Value = index value
	
	for(int i=0; i< arr.length ; i++){
		map.put( i, arr[i]);
	}
	
	int j =n, i=0;
	while(i<k){
		int val = map.get(n-j);
		
		if(val != j){
			
			Set<Map.Entry<Integer, Integer>> set = map.entrySet();
			for(Map.Entry<Integer, Integer> entry : set){
				
				if(entry.getValue() == j){
					
					int key = entry.getKey();
					map.put(key, map.get(n-j));
					map.put(n-j, j);
					i++;
					
					break;
				}
			}
		}
		j--;
	}
	
	for(int iter =0; iter < n ; iter++){
		
		System.out.print(map.get(iter)+" ");
	}
	
}

@Vikas-G-141583313319037,

https://ide.codingblocks.com/s/218250 Corrected code.

Instead of storing your answer in the map, swap elements in the array and keep a count.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.