#C5000. Smallest Sequence with Limited Adjacent Swaps
Smallest Sequence with Limited Adjacent Swaps
Smallest Sequence with Limited Adjacent Swaps
You are given an integer \(n\), an integer \(k\) representing the maximum number of allowed adjacent swaps, and an array \(arr\) of \(n\) integers. Your task is to obtain the lexicographically smallest sequence possible by performing at most \(k\) swaps on adjacent elements.
Algorithm: For each element in the array, try to swap it with its left neighbor if it is smaller, and continue doing so until you either run out of swaps (i.e., \(k = 0\)) or the left neighbor is no longer greater than the element. Print the resulting array.
Note: The sequence comparison is done lexicographically.
inputFormat
The input is given via standard input and consists of two lines:
- The first line contains two integers \(n\) and \(k\) separated by space, where \(n\) is the number of elements in the array and \(k\) is the maximum number of adjacent swaps allowed.
- The second line contains \(n\) space-separated integers representing the array \(arr\).
outputFormat
Output the lexicographically smallest sequence obtained as a single line of \(n\) space-separated integers.
## sample5 3
4 3 2 1 5
2 3 4 1 5