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 = {v: i for i, v in enumerate(arr)} for i in range(n): if k <= 0: break current = arr[i] target = sorted_arr[i] if current == target: continue pos = value_to_index[target] # Swap the elements in the array arr[i], arr[pos] = arr[pos], arr[i] # Update the indices in the dictionary value_to_index[target] = i value_to_index[current] = pos 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.