O(n) c++ two pointer
Intuition\nIf we will use all changes, then we just need to pick k-maxChanges 1s.\n# Approach\nTwo pointers to maintain the pick window.\n# Complexity\n- Time c
# Intuition\nIf we will use all changes, then we just need to pick `k-maxChanges` 1s.\n# Approach\nTwo pointers to maintain the pick window.\n# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(n)\n# Code\n```\nclass Solution {\npublic:\n    long long minimumMoves(vector<int>& a, int k, int maxChanges) {\n        int n = a.size();\n        vector<int>v;\n        for(int i = 0;i<n;i++){\n            if(a[i])v.push_back(i);\n        }\n        vector<long long>sum(v.size(), 0);\n        for(int i = 0;i<v.size();i++){\n            sum[i] = v[i] + (i?sum[i-1]: 0);\n        }\n        long long ret = 1LL<<60;\n        int cur = -1;\n        int l = 0, r = k - maxChanges-1;\n        for(int i = 0;i<n;i++){\n            int cnt = 0;\n            if(a[i])cnt++;\n            long long ans = 0;\n            if(cnt>=k){\n                ret=min(ret, ans);\n                continue;\n            }\n            if(i&&a[i-1]){\n                cnt++;\n                ans++;\n            }\n            if(cnt>=k){\n                ret=min(ret, ans);\n                continue;\n            }\n            if(i+1<n&&a[i+1]){\n                cnt++;\n                ans++;\n            }\n            if(cnt>=k){\n                ret=min(ret, ans);\n                continue;\n            }\n            if(maxChanges + cnt >=k){\n                ans += (k-cnt)*2;\n                ret=min(ret, ans);\n                continue;\n            }\n            ans = 2*maxChanges;\n            int need = k - maxChanges;\n            while(r+1<v.size()&&abs(v[r+1]-i)<abs(v[l]-i)){\n                l++;\n                r++;\n            }\n            while(cur+1<v.size()&&v[cur+1]<=i)cur++;\n            if(cur>=l){\n                ans += i * (long long)(cur-l+1) - (sum[cur]-(l?sum[l-1]:0));\n            }\n            if(cur<r){\n                ans += -i * (long long)(r-cur) + sum[r] - (cur==-1?0:sum[cur]);\n            }\n            ret=min(ret, ans);\n        }\n        return ret;\n    }\n};\n```
['C++']