python O(len(nums) + limit) runtime, O(limit) space
eventually we want to check for each possible target in the range [1, 2 limit], how much moves we need to make in order to bring all this pairs to this target.\
eventually we want to check for each possible target in the range [1, 2 limit], how much moves we need to make in order to bring all this pairs to this target.\nA trivial algorithm yields O(limit * n) runtime. \nLets optimize it.\n\n\nwe initialize 2 arrays of size 2 * limit.\narray 1: punishFromDown: that will eventually hold the following:\npunishFromDown[i] will hold the number of moves that we need to do in order to bring all the pairs (num1, num2 ) for which such that num1 + num2 < i to i.\n\narray 1: punishFromUp: that will eventually hold the following:\npunishFromUp[i] will hold the number of moves that we need to do in order to bring all the pairs (num1, num2 ) for which such that num1 + num2 > i to i.\n\nwe iterate on each pair (num1, num2) and for each pair we mark 4 targets in the relevant arrays.\n\npoint 1: the minimal target T that is larger than num1 + num2,  such that for any T\' > T  we need to pay 1 move to get the pair to. This is simply num1 + num2 + 1\n*    punishFromDown[num1 + num2 + 1] += 1\n\t\npoint 2: the minimal target T that is larger than num1 + num2,  such that for any T\' > T  we need to pay 2 move to get the pair to. This is max(num1, num2) + limit + 1\n*    punishFromDown[max(num1, num2) + limit + 1] += 1\n   \npoint3 :  the maximal target T that is smaller than num1 + num2, such that  for any T\' < T  we need to pay 1 move to get the pair to. This is simply  num1 + num2 - 1\n*    punishFromUp[ num1 + num2 - 1] += 1\n   \npoint4 :  the maximal target T that is smaller than num1 + num2, such that  for any T\' < T  we need to pay 2 move to get the pair to. This is simply  min(num1, num2)\n*    punishFromUp[min(num1, num2)] += 1\n   \n   \nNow we make punishFromUp, punishFromDown represent what they intend to represent by setting \n* punishFromDown[i] = sum(punishFromDown[j] | j <= i) for each i\n* punishFromUp[i] = sum(punishFromUp[j] | j >= i) for each i\n\nThis is obviously done in 1 pass on punishFromDown, punishFromUp\n\nThats it now we just need to find the min between punishFromDown[i] + punishFromUp[i]  \n\n```\nclass Solution(object):\n    def minMoves(self, nums, limit):\n        """\n        :type nums: List[int]\n        :type limit: int\n        :rtype: int\n        """\n        punishFromDown = [0] * (2 * limit + 2)\n        punishFromUp = [0] * (2 * limit + 2)\n        \n        for i in xrange(len(nums) / 2):\n            n1, n2 = nums[i], nums[len(nums) - i - 1]\n            punishFromDown[n1 + n2 + 1] += 1\n            punishFromDown[max(n1, n2) + limit + 1] += 1\n            punishFromUp[n1 + n2 - 1] += 1\n            punishFromUp[min(n1, n2)] += 1\n        \n        for l in xrange(1,  2 * limit + 1):\n            punishFromDown[l] = punishFromDown[l - 1] + punishFromDown[l]\n            punishFromUp[2 * limit - l] = punishFromUp[2 * limit - l] + punishFromUp[2 * limit - l + 1]\n        \n        best = len(nums)\n        for l in xrange(1,  2 * limit + 1):\n            best = min(punishFromDown[l] + punishFromUp[l], best)\n        return best\n```\n\n\n
['Python']