C++ Line Sweep
Core idea is to maintain ranges that indicate how many moves we have to make. it could be 0, 1 or 2 moves.\n\nSee 3224. Minimum Array Changes to Make Difference
Core idea is to maintain ranges that indicate how many moves we have to make. it could be 0, 1 or 2 moves.\n\nSee 3224. Minimum Array Changes to Make Differences Equal for something similar for more practice heh. https://leetcode.com/problems/minimum-array-changes-to-make-differences-equal/\n\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minMoves(vector<int>& nums, int limit) {\n        int n = nums.size();\n        vector<int> boundaries(2 * limit + 2, 0);  \n        int left = 0, right = n - 1;\n        \n        // Process each pair\n        while (left < right) {\n            int lower = min(nums[left], nums[right]);\n            int higher = max(nums[left], nums[right]);\n\n            // the range that takes 1 move is [lower + 1, higher + limit]\n            int mn = lower + 1;\n            int mx = higher + limit;\n\n            // starts at 2 since the range we can add is from 1 to limit, not 0.\n            boundaries[2] += 2;\n            boundaries[mn] -= 1;\n\n            // -1 for the case where no change required\n            boundaries[nums[left] + nums[right]] -= 1;\n            boundaries[nums[left] + nums[right] + 1] += 1;\n\n            // anything above the max (i.e. higher + limit) will be + 1\n            boundaries[mx + 1] += 1;\n\n            left += 1;\n            right -= 1;\n        }\n\n        // Calculate the minimum moves using a prefix sum\n        int res = INT_MAX;\n        int num_to_change = 0;\n        for (int i = 2; i < boundaries.size(); i += 1) {\n            num_to_change += boundaries[i];\n            res = min(res, num_to_change);\n        }\n\n        return res;\n    }\n};\n\n```
['Line Sweep', 'Prefix Sum', 'C++']