Arrange the first k elements, easy java solution with explanation
We can arrange the first k elements that there difference is n-1, n-2, etc, and there are k-1 unique difference now, then we can make the k-th difference as 1.\
We can arrange the first k elements that there difference is n-1, n-2, etc, and there are k-1 unique difference now, then we can make the k-th difference as 1.\neg. n = 7, k = 4\n(1) make the first k elements starting from 1 (beacuase only 1 and n can form \'n-1\' difference)\n1, 7, 2, 6\n(2) now there are 3 unique difference (6,5,4), we then set all other difference as 1\n1, 7, 2, 6, 5, 4, 3\n\n```\nclass Solution {\n    public int[] constructArray(int n, int k) {\n        int[] res = new int[n];\n        boolean[] visited = new boolean[n + 1];\n        //ensure the first k number has k-1 unique difference\n        int count = k;\n        int diff = n - 1;\n        res[0] = 1;\n        visited[1] = true;\n        for (int i = 1; i < k; i++) {\n            if (res[i-1] + diff > 0 && res[i-1] + diff <= n && !visited[res[i-1]+diff])\n                res[i] = res[i-1] + diff;\n            else\n                res[i] = res[i-1] - diff;\n            diff--;\n            visited[res[i]] = true;\n        }\n        //for the rest numbers, let the difference as 1\n        for (int i = k; i < n; i++) {\n            if (res[i-1] + 1 > n || visited[res[i-1] + 1]) \n                res[i] = res[i-1] - 1;\n            else \n                res[i] = res[i-1] + 1;\n            visited[res[i]] = true;\n        }\n        return res;\n    }\n}\n```
[]