Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nMaking use of changes and swapping 1s costs 2 moves if we place each changed one direct
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMaking use of changes and swapping 1s costs 2 moves if we place each changed one directly adjacent to Dylan\'s index. The only time we may not use all the changes from maxChanges is if there are some adjacent ones, and if we can use <= maxChanges along with these adjacent ones to satisfy the entire requirement. Otherwise, we always want to use all the changes.\n\nOnce we decide we use all the changes, all other k - maxChanges ones come from moving neighbors. For this we need a sliding window over the array of ones positions of length k - maxChanges. The minimum cost from each sliding window will be the distances to the middle element. We can calculate the cost easier using prefix sum array. Then ones[mid] - ones[l] + ones[mid] - ones[l+1] + ... becomes (mid-l+1)*ones[mid] + (p[mid+1] - p[l]) and similarly for the right side. Since we already made maxChanges changes, add 2 * maxChanges to the cost. Take the minimum across all windows.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\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 {\npublic:\n    long long minimumMoves(vector<int>& nums, int k, int maxChanges) {\n        int maxneighbors = 1;\n\n        vector<long long> ones;\n\n        for(int i = 0; i<nums.size(); i++)\n        {\n            if(nums[i] == 1)\n            {\n                ones.push_back(i);\n            }\n\n            if(i < nums.size()-1 && nums[i] + nums[i+1] == 2)\n            {\n                maxneighbors = max(maxneighbors,2);\n            }\n\n            if(i < nums.size()-2 && nums[i] + nums[i+1] + nums[i+2] == 3)\n            {\n                maxneighbors = max(maxneighbors,3);\n            }\n        }\n\n        if(ones.size() == 0)\n        {\n            return 2*k; // all from changes\n        }\n        \n        if (k <= maxneighbors)\n        {\n            return k - 1;\n        }\n\n        if (k <= maxChanges + maxneighbors)\n        {\n            return maxneighbors - 1 + 2*(k - maxneighbors); // -1 for central one\n        }\n\n        k = k - maxChanges;\n        int n = ones.size();\n\n        vector<long long> p;\n        p.push_back(0);\n\n        for(int i = 0; i<n; i++)\n        {\n            p.push_back(ones[i] + p.back());\n        }\n\n        long long cost = LLONG_MAX;\n\n        for(int i = 0; i+k-1<n; i++)\n        {\n            long long l = i;\n            long long r = i+k-1;\n            long long mid = l + (r-l)/2;\n\n            long long left_cost = (mid + 1 - l)*ones[mid] - (p[mid+1] - p[l]);\n            long long right_cost = p[r+1] - p[mid] - (r + 1 - mid)*ones[mid];\n\n            cost = min(cost, 2*maxChanges + left_cost + right_cost);\n        }\n\n        return cost;\n    }\n};\n```
['C++']