• + 0 comments
    #include <iostream>
    #include <vector>
    using namespace std;
    
    vector<int> absolutePermutation(int n, int k) {
        vector<int> result;
    
        // If k is 0, the permutation is just the natural numbers from 1 to n.
        if (k == 0) {
            for (int i = 1; i <= n; ++i) {
                result.push_back(i);
            }
            return result;
        }
    
        // If n is not divisible by 2 * k, a valid permutation is not possible.
        if (n % (2 * k) != 0) {
            return {-1};
        }
    
        // Construct the permutation in groups of 2 * k.
        for (int i = 1; i <= n; i += 2 * k) {
            // Add the first k numbers in the group shifted by +k.
            for (int j = 0; j < k; ++j) {
                result.push_back(i + j + k);
            }
            // Add the next k numbers in the group shifted by -k.
            for (int j = 0; j < k; ++j) {
                result.push_back(i + j);
            }
        }
    
        return result;
    }
    
    int main() {
        int t;
        cin >> t; // Number of queries
    
        while (t--) {
            int n, k;
            cin >> n >> k;
    
            vector<int> result = absolutePermutation(n, k);
    
            if (result.size() == 1 && result[0] == -1) {
                cout << "-1\n";
            } else {
                for (int num : result) {
                    cout << num << " ";
                }
                cout << "\n";
            }
        }
    
        return 0;
    }