Unlock problem(Hashing)

My code is giving a right answer for the sample case but is failing to get submitted

hello @rahul.gupta03111999
go with greedy .
i,e first try to place biggest number at oth position then next biggest at 1st and similary for other.this will ensure that the formed number after atmost k swaps will me maximum.

image

that highlighted line needs correction.in that line u need to find best position of highest number

Still one test case is failing
https://ide.codingblocks.com/s/355649

what issue u r getting , is it tle or runerror.

TLE I think is the reason

@rahul.gupta03111999

this loop is increasing the complexity.
use hashing.

refer this( logic u already know)->

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n,k;
    cin >> n >> k;
    vector<int> v;
    unordered_map<int,int> m ;
    for(int i=0;i<n;i++){
        int x;
        cin >> x;
        v.push_back(x);
        m[v[i]] = i ;
    }
    for(int i=0;i<n && k>0;i++){
        int x = n - i;
        if(v[i] == x){
            //Already correct element
            continue;
        }
        int index = m[x];
        m[v[i]] = index ;
        m[x] = i;
        swap(v[i],v[index]);
        k--;
    }
    for(int i=0;i<n;i++){
        cout << v[i] << " ";
    }

    return 0;
}