Maximizing by swapping Input
An integer n in the range 100 to 10^18 and an integer k in the range 1 to 18, inclusive.
The swap operation
A swap chooses two digits at different positions and exchanges their positions, as long as the swap does not result in a leading zero. For example if we start with the integer 12345 we can perform one swap to make it 12543.
Task
Output the largest number you can get after exactly k swaps.
Examples
n = 1283, k = 2. Output: 8312
n = 510, k = 1. Output: 501
n = 501, k = 2. Output: 501
n = 999, k = 3. Output: 999
n = 12345678987654321, k = 1. Output: 92345678187654321
Python 3, 194 190 196 193 175 164 162 bytes



def f(n,k):
 for i in range(len(n)-1):
  y=max(n[i:]);j=n[::-1].index(y)
  if(y&gt;n[i])*k:n[i],n[~j]=y,n[i];k-=1
 if len(n)-len({*n})&lt;k%2:n[-2:]=n[:-3:-1]
 return n


Try it online!

Pretty tough to golf this challenge in a real world language - I like it :)

After golfing down to 190 bytes (and below..), I found some cases where the algorithm would not find the maximum possible value. Bugfixing forced me back up to 196 193 bytes :/

... then I found some ways to better handle branching on k and went down below 180 :)

After the comment from Chas Brown, switching from integer to list I/O saved 28 bytes! (And more golfing another 2..)

Then, another bug: the algorithm incorrectly handles remaining swaps, if there are equal digits present. The fix cost 19 bytes.

Algorithm:


For each digit, starting from the leftmost one, swap it with the rightmost maximum digit, if that one is bigger. First line of the for-loop finds the maximum and its index. Second line does the swap.
If there are k' swaps left, swap two equal digits, or the two rightmost k' times. I.e. if  there are two equal digits present, or if k' is even, no more swaps have to be done. Else, just swap the last two digits once.