Can you please tell me what is wrong in my logic

Ques:-https://www.hackerrank.com/challenges/largest-permutation/problem

Ans:-vector largestPermutation(int k, vector arr) {
int i=0;
int n=arr.size();
while(k–){
int max=*max_element(arr.begin()+i,arr.end());
auto it=find(arr.begin()+i,arr.end(),max);
swap(arr[i],arr[it-arr.begin()]);
i++;
}
return arr;
}

Please tell me where my logic is wrong

hello @manikjindal49

pls share ur code using cb ide.

ur logic looks correct. u need to handle one corner cases . where k > n
in that case u will run out of array so probably u will get a segementation fault
also the time complexity is O(k * n) which will give tle.

check -> unlock problem of ur course module (in hashing section) .
this is exaclty same
use hashing and make it efficient