O(n) easy solution, prefix sum + sliding window
\nclass Solution {\npublic:\n    typedef long long int ll;\n    int minMoves(vector<int>& nums, int k) {\n        int n = nums.size();\n        vector<ll> sum(n
```\nclass Solution {\npublic:\n    typedef long long int ll;\n    int minMoves(vector<int>& nums, int k) {\n        int n = nums.size();\n        vector<ll> sum(n + 1), pos(n + 1);\n        int tol1 = 0;\n        for (int i = 0; i < n; i ++) {\n            if (nums[i]) {\n                ++ tol1;\n                sum[tol1] = i + sum[tol1 - 1];\n                pos[tol1] = i;\n                \n            }\n        }\n        int ans = INT_MAX;\n        for (int i = (k + 1) / 2; i <= tol1 - k / 2; i ++) {\n            ans = min(ans, (int) (abs(sum[i] - sum[i - (k + 1) / 2] - (k + 1) / 2 * pos[i]) + (sum[i + (k) / 2] - sum[i] - k / 2 * pos[i])));\n        }\n        for (int i = 1; i <= k; i ++) {\n            ans -= (abs((k + 1) / 2 - i));\n        }\n        return ans;\n    }\n};\n```
[]