C++, concise code, O(n)
The requirement of k distinct distance can be achieved from 1, 2, ..., k+1 (<= n), by the following strategy:\n\n1, k+1, 2, k, 3, k-1 ...;\nThe distance of this
The requirement of k distinct distance can be achieved from 1, 2, ..., k+1 (<= n), by the following strategy:\n```\n1, k+1, 2, k, 3, k-1 ...;\nThe distance of this sequence is k, k-1, k-2, ..., 2, 1\n```\nThen append the remaining numbers to the list.\n```\nclass Solution {\npublic:\n    vector<int> constructArray(int n, int k) {\n        int l = 1, r = k+1;\n        vector<int> ans;\n        while (l <= r) {\n            ans.push_back(l++);\n            if (l <= r) ans.push_back(r--);\n        }\n        for (int i = k+2; i <= n; i++)\n            ans.push_back(i);\n        return ans;\n    }\n};\n```
[]