Https://ide.codingblocks.com/s/163457

what i am doing wrong

Sarthak, use this approach in your code,
int pos[n+1];
for(int i=0;i<n;i++)
{
pos[ar[i]]=i;
}
for(int i=0;i<n && K ;i++)
{
if(ar[i]==n-i)
{
continue;
}
int temp=pos[n-i];
pos[ar[i]]=pos[n-i];
pos[n-i]=i;
swap(ar[temp],ar[i]);
–K;
}