Unlock Problem Hashing

Only 1 out of 4 test cases passed.
What is wrong in my code?

#include<iostream>
#include<unordered_map>
using namespace std;

int main() {

	int n,k;
	cin>>n>>k;

	int arr[100000];
	unordered_map<int,int> m;
	for(int i=0; i<n; i++){
		cin>>arr[i];

		m[arr[i]] = i;	// key:element, val:index
	}

	for(int i=n; i>0 and k>0; i--,k--){
		// current position and best position of current natural number 'i'
		int curr_pos = m[i];
		int best_pos = n-i;
		
		// swap current element with element at its BEST position
		swap(arr[curr_pos], arr[best_pos]);
		k--;

		// update in map
		m[i] = best_pos;
		m[arr[curr_pos]] = curr_pos;
	}

	for(int i=0; i<n; i++){
		cout<<arr[i]<<" ";
	}

	return 0;
}

@anuragdeol2017 you are making 2 mistakes

  1. if current_pos==best_pos then we have to do nothing and do not waste k here just continue
    2.you are doing k-- in every iteration of for loop we have to decrease k when we swap elements do not decrement k in for loop with i
    https://ide.codingblocks.com/s/624849
    please check this modified code and if clear mark it as resolved

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.