Giving WA on All test cases

I am using greedy approach as said in the hint, but it is giving WA on all test cases . Here is the code :

#include
using namespace std;

int pos[100001] = {0};

int main()
{
int n,k;
cin>>n>>k;
int a[n];
for(int i = 0; i<n;++i)cin>>a[i];
for(int i =0; i<n;++i)
{
pos[a[i]] = i;
}
int i = 0, j = n;
for(int i = 0; i<n && k;++i)
{
if(pos[j] > pos[a[i]])
{
// swap
swap(a[i],a[pos[j]]);
swap(pos[a[i]],pos[j]);
k–;
}
j–;
}
for(int x : a)cout<<x<<" ";
cout<<endl;
return 0;
}

@dev_rob
we dont have to swap with the element on the pointer j we have to bring the correct element on its correct position say at position i the correct element is n-i where i starts from 0 and we have to check if the n-i element is already at that position then we have to do nothing and continue
if not
then find the position of n-i element and then swap these two values and also do not forgot to change their indices in the map for further operations
Coding Blocks IDE
this is the changed code it will work fine and if your dout still exists let me know and if clear mark it as resolved and rate my experience

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.