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