Problem :

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


My Solution :

#!/usr/bin/env python3


def largest_permutation(k, arr):
indexes = [0]*(n+1)
for i, a in enumerate(arr):
indexes[a] = i
for i in range(n):
if k == 0:
break
if arr[i] != n-i:
j = indexes[n-i]
arr[i], arr[j] = n-i, arr[i]
indexes[arr[j]] = j
k -= 1
return ' '.join(map(str, arr))


n, k = map(int, input().split())
arr = list(map(int, input().rstrip().split()))
result = largest_permutation(k, arr)
print(result)