Maximum permutation by k swaps

the hint says expects greedy solution…but how can greedy work??
i tried a recursive solution like backtracking but it gave tle

also, what is wrong with this approach??

for(int i=0; i<n; i++)

{

    if(k<=0)	break;
	int maxidx = i;
	for(int j=i+1; j<n; j++)
	{
		if(arr[j]>arr[maxidx])
			maxidx = j;
	}
	if(maxidx!=i)
	{
		k--;
		swap(arr[i], arr[maxidx]);
	}
}

@shameek.agarwal
Hello Shameek,
ur solution time complexity is O(n^2)

image

clearly it will not pass because constraint is high.

so try to optimise ur approach.
hint -> hashing

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.