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) pos_dict = {val: idx for idx, val in enumerate(arr)} for i in range(n): if k <= 0: break current_val = arr[i] desired_val = sorted_arr[i] if current_val == desired_val: continue j = pos_dict[desired_val] # Perform the swap arr[i], arr[j] = arr[j], arr[i] # Update the positions in the dictionary pos_dict[desired_val] = i pos_dict[current_val] = j 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.