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;
}