667: Solution with step by step explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Create an empty list called "ans".\n\n2. Add integers 1 through (n-k) 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Create an empty list called "ans".\n\n2. Add integers 1 through (n-k) to the "ans" list using the range() function.\n\n3. For each value of i in the range of 0 to k-1:\na. Calculate the difference between i and the nearest even number using the floor division operator "//". This value represents the distance of the current element from the previous element in the result array.\nb. If i is even, append the value n minus the calculated difference plus 1 to the "ans" list. This will ensure that the difference between this value and the previous value is equal to the difference calculated in step 3a.\nc. If i is odd, append the value n-k plus the calculated difference to the "ans" list. This will ensure that the difference between this value and the previous value is equal to the difference calculated in step 3a.\n\n4. Return the "ans" list.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n  def constructArray(self, n: int, k: int) -> List[int]:\n    ans = list(range(1, n - k + 1))\n    for i in range(k):\n        diff = i // 2 + 1\n        if i % 2 == 0:\n            ans.append(n - diff + 1)\n        else:\n            ans.append(n - k + diff)\n\n    return ans\n\n```
['Array', 'Math', 'Python', 'Python3']