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
import sys n, k = map(int, sys.stdin.readline().split()) arr = list(map(int, sys.stdin.readline().split())) sorted_desc = sorted(arr, reverse=True) value_to_index = {num: idx for idx, num in enumerate(arr)} for i in range(n): if k <= 0: break current_val = arr[i] desired_val = sorted_desc[i] if current_val == desired_val: continue # Find the current index of the desired value j = value_to_index[desired_val] # Swap the elements arr[i], arr[j] = arr[j], arr[i] # Update the indices in the dictionary value_to_index[current_val] = j value_to_index[desired_val] = 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.