Question d’entretien chez MakeMyTrip

An array is given. With k swaps, find the maximum number that can be formed using those array elements.

Réponse à la question d'entretien

Utilisateur anonyme

11 août 2016

Find the maximum number in sub array arr[0, k]. Suppose it was found at index i. If it is greater than arr[0], insert it at 0 just like we do in insertion sort. This will cost us i swaps. With remaining k-i swaps, apply similar logic to subarray starting from index 1 and so on. So when remaining swaps reach 0, current array should be the result.