[Python] O(N + limit) time, O(limit) space. Concise explanation
\nfrom collections import defaultdict\n\n\nclass Solution:\n    def minMoves(self, nums: List[int], limit: int) -> int:\n        # let x = nums[i] and y = nums[
```\nfrom collections import defaultdict\n\n\nclass Solution:\n    def minMoves(self, nums: List[int], limit: int) -> int:\n        # let x = nums[i] and y = nums[n - 1 - i]\n        # if the target number is k then:\n        # if x + y == k then it takes 0 moves (1)\n        # If we take 1 move, we can change x from 1 to limit or change y from 1 to limit\n        # if we change x from 1 to limit, we can generate a range of target [y + 1, y + limit]\n        # if we change y from 1 to limit, we can generate a range of target [x + 1, x + limit]\n        # if means that if we can take 1 moves, we can generate target from\n        # (min(x,y) + 1) to (max(x, y) + limit) \n        # so if min(x,y) + 1 <= k <= max(x, y) + limit then it would 1 moves (2)\n        # if neither 1 nor 2 is true then we have to change both x and y to make x + y == k\n        # it takes 2 moves (3)\n        # -----------------------------------------------------------------------------------\n        # for a target k, we want to know the cost to change values of `nums` so that \n        # the target complementary is k\n        # we need to know how many pair x, y such that min(x, y) + 1 <= k <= max(x, y) + limit\n        # these pairs will take either 0 or 1 moves (takes 0 moves if x + y == k)\n        # the rest pairs will take 2 moves\n        # so for a target k, the cost will be:\n        # cost = 2 * the_number_of_intervals_does_not_include_k + \n        #        1 * the_number_of_interval_incude_k\n        #        - the_number_of_pairs_having_x_+_y_==_k\n        # -----------------------------------------------------------------------------------\n        # each pair x, y will generate an interval [min(x,y)+1, max(x,y)+limit]\n        # given a list of intervals computed from each pair\n        # for each value k, we want to know how many intervals contains k\n        # this is a basic problem that can be solved in constant time for each value of k\n       \n        n = len(nums)\n        mid = n // 2\n        # zero_moves[k] will return the number of pairs having x + y == k\n        zero_moves = defaultdict(int)\n        # using start and end array to find the number of intervals include a specific value of k\n        start = [0] * (2 * limit + 1)\n        end = [0] * (2 * limit + 1)\n        res = float(\'inf\')\n        for i in range(mid):\n            x = nums[i]\n            y = nums[n - 1 - i]\n            zero_moves[x + y] += 1\n            start[min(x, y) + 1] += 1\n            end[max(x, y) + limit] += 1\n        # this will keep track of the number of intervals contains target value k\n        intervals = 0\n        for target in range(2, limit * 2 + 1):\n            intervals += start[target]\n            cost = 2 * (mid - intervals) + intervals - zero_moves[target]\n            res = min(res, cost)\n            intervals -= end[target]\n        return res\n\n```
[]