Java Solution | Constant Space | Simple Approach
Suppose n = 6, k = 4\nWe will form array as :\narr[0] = 1,\narr[1] = 1 + k(=4) = 5, and k--,\narr[2] =  5 - k(=3) = 2, and k--,\narr[3] = 2 + k(=2)= 4,  and k--
Suppose n = 6, k = 4\nWe will form array as :\narr[0] = 1,\narr[1] = 1 + k(=4) = 5, and k--,\narr[2] =  5 - k(=3) = 2, and k--,\narr[3] = 2 + k(=2)= 4,  and k--,\narr[4] = 4 - k(=1) = 3, and k-- (Here k = 0) after this we will fill remaining numbers normally\narr[5] = 6\nSo array formed : [1, 5, 2, 4, 3, 6] after following operation it will be [4, 3, 2, 1, 3] which contains exactly 4 distinct numbers.\n```\nclass Solution {\n    public int[] constructArray(int n, int k) {\n        int[] arr = new int[n];\n        arr[0] = 1;\n        int x = 1 + k;\n        for(int i=1 ; i<arr.length ; i++){\n            arr[i] = (k <= 0) ? ++x : (i % 2 == 0) ? arr[i-1] - k : arr[i-1] + k;\n            k--;\n        }\n        return arr;\n    }\n}\n```\nTime Complexity : O(n)\nAuxiliary Space : O(1)\n**Please upvote if you like the implementation**\n
['Math', 'Java']