You are given N natural numbers and K swaps are allowed. Determine the largest…
- User
You are given N natural numbers and K swaps are allowed. Determine the largest permutation that you can attain. INPUT: First line contains N and k next line has N spaced integers
OUTPUT: Print the largest permutation array.
0<N<10^5 0<K<10^5
SAMPLE INPUT 5 1 4 2 3 5 1
SAMPLE OUTPUT 5 2 3 4 1
Assistant
n, k = map(int, input().split()) arr = list(map(int, input().split())) sorted_arr = sorted(arr, reverse=True) value_to_index = {value: idx for idx, value in enumerate(arr)} for i in range(n): if k <= 0: break desired = sorted_arr[i] if arr[i] == desired: continue # Find current position of the desired value pos = value_to_index[desired] # Swap the elements in the array arr[i], arr[pos] = arr[pos], arr[i] # Update the indices in the dictionary value_to_index[arr[pos]] = pos value_to_index[desired] = i k -= 1 print(' '.join(map(str, arr)))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.