Help me in solving SWAPNUM31 problem

My issue

My code

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--){
        int n, k;
        cin>>n>>k;
        vector<int> arr;
        arr.push_back(-300);
        for(int i=1; i<=n; i++){
        	int a;
            cin>>a;
            arr.push_back(a);
        }
        vector<int> temp;
        int i=1;
        while(i<=n-k){
            temp.push_back(arr[i]);
            i++;
        }
        i=k+1;
        while(i<=n){
            temp.push_back(arr[i]);
            i++;
        }
        sort(temp.begin(), temp.end());
        vector<int> ans;
        i=0;
        while(i<=temp.size()/2-1){
            ans.push_back(temp[i]);
            i++;
        }
        for(int j=n-k+1; j<=k; j++){
            ans.push_back(arr[j]);
        }
        while(i<=temp.size()-1){
            ans.push_back(temp[i]);
            i++;
        }
        for(int j=0; j<n; j++){
            cout<<ans[j]<<" ";
        }cout<<endl;
    }
}

Problem Link: SWAPNUM31 Problem - CodeChef

ok so I was not able to solve this question, but I came towards an interesting observation which you may or may not have used here.

Observation : A<------ (gap >= k) ---------->B<------- (gap < k) ------>C
for a case like this, even though we cant swap B and C directly, we can swap it through a third number.

sort all elements of array except between indices n-k and k-1 (both inclusive)