Beautiful Arrangement 2
Intuition\nTo solve this problem, the key observation is that we need an array of size n that contains exactly k distinct absolute differences between consecuti
## Intuition\nTo solve this problem, the key observation is that we need an array of size `n` that contains exactly `k` distinct absolute differences between consecutive elements. By starting with a straightforward increasing sequence and then strategically placing the remaining numbers in a pattern that maximizes the diversity of differences, we can achieve the desired result. This involves alternating between large and small values after filling in the first part of the sequence.\n\n## Approach\n1. Start by filling the first `n - k` elements of the array with consecutive numbers from `1` to `n - k`. This part will have a uniform difference of `1` between elements.\n2. To introduce exactly `k` distinct absolute differences, the remaining part of the array is constructed by alternating between the largest remaining values and the smallest possible values. We initialize a variable with the largest value `n` and a flag to switch between adding and subtracting distances.\n3. We begin by inserting `n` and then alternate between subtracting and adding a distance that starts from `k` and decreases with each step. This ensures we create all `k` distinct differences.\n4. Continue this alternating process until the array is completely filled, thereby constructing the desired array with exactly `k` distinct absolute differences.\n\n## Complexity\n- **Time complexity**: The time complexity is \\(O(n)\\) because we iterate over the range of size `n` to fill the result list.\n- **Space complexity**: The space complexity is \\(O(n)\\) as we use a list to store the result of size `n`.\n\n\n# Code\n```python3 []\nclass Solution:\n    def constructArray(self, n: int, k: int) -> List[int]:\n        result = []\n        for i in range(1, n-k+1):\n            result.append(i)\n        secondary_start = n\n        flag = False\n        initail_distance = n - (n-k+1)\n        while (len(result)) < n:\n            result.append(secondary_start)\n            secondary_start = secondary_start + initail_distance if flag else secondary_start - initail_distance\n            initail_distance = initail_distance - 1 \n            flag = not flag\n        return result\n```
['Array', 'Math', 'Python3']