Preview:
#include <iostream>
#include <queue>
using namespace std;
 
int main() 
{
  int n, k;
  cin >> n >> k;
  
  int a[n];
  for(int i = 0; i < n; ++i)
  {
    cin >> a[i];
  }
  
  for(int i = 1; i < n; ++i)
  {
    int key = a[i];
    int j = i-1;
    
    while(j >= max(0, i-k) && a[j] > key)
    {
      a[j+1] = a[j];
      --j;
    }
    a[j+1] = key;
  }
  
  for(int i = 0; i < n; ++i)
  {
    cout << a[i] << " ";
  }
  
  return 0;
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter